You are on page 1of 3

Programming Fundamental LAB-1 CMP-2122

Objective:

To make students familiar with cout and variable declaration

1. Theory:

C++ is a middle-level programming language developed by Bjarne Stroustrup starting in 1979 at Bell
Labs. C++ runs on a variety of platforms, such as Windows, Mac OS, and the various versions of UNIX.
C++ is a famous for its object-oriented programming (OOP) approach, and is an extension of the C
language. It is therefore possible to code C++ in a "C style" or "object-oriented style." In certain
scenarios, it can be coded in either way and is thus an effective example of a hybrid language. C++ is
considered to be an intermediate-level language, as it encapsulates both high- and low-level language
features.
The main highlight of C++ is a collection of predefined classes, which are data types that can be
instantiated multiple times. The language also facilitates declaration of user-defined classes. Classes can
further accommodate member functions to implement specific functionality. Multiple objects of a
particular class can be defined to implement the functions within the class. Objects can be defined as
instances created at run time. These classes can also be inherited by other new classes which take in the
public and protected functionalities by default.
In this course we will stay focused on the basics of programming rather than OOP.
The basic syntax of the program:
//Header file
Main()

//code

A terminator ';' is required at the end of each line. For the comment entry in programming code symbol
'// 'is used. For multiple line comment /* at the start of the comment and */ is used at the end.
First program:
#include <iostream.h>
main ( )
{
cout << This is my first program ;
}
Task#1: Write output of the code

_________________________________________________________________________________
Task#2: Display your name at the output screen and write code below

1
Programming Fundamental LAB-1 CMP-2122

_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________

Task#3: Write a program in C++ which prints following output.


$********************************************************$
$********************************************************$
$* Welcome to University of Sargodha *$
$* Department of CS and IT *$
$********************************************************$
$********************************************************$

_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________

2. Variable declaration:
int x, float y, char z;
This is a declaration statement. All variables in a C++ program must be declared. A declaration specifies
the type of a variable. The word int indicates that the variable is an integer, float and char are used for
floating point values and characters respectively. The compiler will issue an error if a programmer
attempts to use an undeclared variable. Once declared, a particular variable cannot be redeclared in the
same context. A variable may not change its type during its lifetime.
x = 10; // = is known as assignment operator
This is an assignment statement. An assignment statement associates a value with a variable.
Variables can be reassigned different values as needed
#include <iostream>
int main() {
int x;
x = 10;
std::cout << x << '\n';

2
Programming Fundamental LAB-1 CMP-2122

x = 20;
std::cout << x << '\n';
x = 30;
std::cout << x << '\n';
}
Task#4: write output of the code above.
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________

Task#5: write output of the code given below.


#include <iostream>

using namespace std;

int main ()
{
int a, b;
a = 10;
b = 4;
a = b;
b = 7;
cout << "a :";
cout << a;
cout << " b :";
cout << b;
return 0;
}
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________

You might also like