Oop 0
Oop 0
Preprocessor Directive
• The first line of the program
# include<iostream.h>
• The second statement causes the program to wait for the user
to type in an integer number. The resulting number is placed
in the variable temp.
Example: Output/ Input using cout and cin objects
#include<iostream.h>
void main()
{
int x;
float f;
char c;
char str[20] ;
cout << “\n Enter the value of x: “; Sample output:
cin >> x ; Enter the value of x: 5
cout << “\n Enter the value of f:” ; Enter the value of f: 4.5
cin >> f
Enter the value of c: y
cout << “\n Enter the value of c:”;
cin >> c; Enter the value of str: hello
cout << “\n Enter a string: “;
cin >> str ; The value of x = 5
cout << “\n The value of x = “ << x; The value of f = 4.5
cout << “\n The value of f = “ << f;
The value of c = y
cout << “\n The value of c = “ << c;
cout << “\n The value of str = “ << str; The value of str = hello
}
Cascading of << and >> operators
• When the operator (<<) is used repeatedly in the cout statement, it is
called cascading of extraction or output operator.
• The insertion operator (>>) can be cascaded with cin in the same way,
allowing the user to enter a series of values:
Example:
#include<iostream.h>
void main()
{
int x, float f, char c, char str[20];
cout << “\n Enter the value of x, f, c and str ” ;
cin >> x >> f >> c >> str” ; // Cascading of input operator
// Cascading of output operator
cout << “\n x = ” << x << “ f = ” << f << “ c= ” << c << “ str= ” << str ;
}
Sample Output:
Enter the value of x, f, c and str: 3 4.5 a hello
x = 3 f = 4.5 c = a str = hello
Flexibility in the declaration of the variables
• C requires that all variables be declared before the first
executable statement. As against this, C++ allows
definition of variables at the point where they are used. The
following example illustrates this:
#include<iostream.h>
void main()
{
int x , y ;
cout << “\n Enter the value of x: ” ;
cin >> x ;
cout << “\n Enter the value of y: ” ;
cin >> y;
int z ;
z = x + y;
cout << “\n The sum of the numbers is:” << z ;
}
Function Prototypes:
• A function prototype is one of the major improvement added to C++
function.
• The compiler uses the prototype to ensure that the types of actual
arguments that you pass in a function call are the same as the types
of the formal arguments in the function definition.
void main()
{
int a = 4, b = 6;
int c = sum(a, b); // Calling function
cout << “\n The sum is: “<< c ;
}
// function definition
int sum(int x, int y)
{
int z = x + y ;
return z ;
}
Function overloading
• Overloading refers to the use of the same thing for different
purposes. C++ also permits overloading of functions.
Thus, one can use the same function name to create
functions that perform a variety of different tasks. This is
known as the function overloading in OOP.
void main()
{
int i1=5, i2 =6;
int i3 = sum(i1, i2); //Calling function1
int i4 = sum(i1, i2, i3); // Calling function2
float f1 = 4.5;
float f2 = sum(i1, f1); // Calling function3
char c1 =‘a’, c2 = ‘b’;
int c3 = sum(c1, c2); // Calling function4
cout << “i3 = “<< i3 ;
cout << “i4 = “ << i4 ;
cout << “f2 = “ << f2;
cout << c3 = “ << c3;
}
Cont.
// Function Definitions
#include<iostream.h>
void main()
{
int i = 10;
int &j = i ; // j is a reference to i
cout << “\n i = ” << i << “j = ” << j ; Output:
j = 20; i = 10 j = 10
cout << “\n i = ” << i << “j = ” << j ; i = 20 j = 20
i++; i = 21 j = 21
cout << “\n i = ” << i << “j = ” << j ; i = 22 j = 22
j++;
cout << “\n i= ” << I << “j = ” << j ;
}
Cont.
int a = 5 ; int a = 5 ;
p=a; p = &a
A variable can have multiple references. Changing the value
of one of them effects a change in all others.
#include<iostream.h>
void main()
{
int i = 5 ;
int &j = i ;
int &k = i ;
int &l = i ;
cout << “\n i = ” << i << “j = ” << j << “k = ”<< k << “l = ” << l;
k = 20;
cout << “\n i = ” << i << “j = ” << j << “k = ”<< k << “l = ” << l;
}
Output:
i=5 j=5 k=5 l=5
I = 20 j = 20 k = 20 l = 20
Cont.
}
void update3(int& c)
{
c = c + 1;
cout << “\n c = “ << c ;
}
const qualifier
• If the keyword const, precedes the data type of
a variable, the value of the variable will not
change throughout the program.
• The new operator allocates memory from free store (in the
C++ lexicon, heap is called free store).
• The new operator, when used with the pointer to a data type,
a structure, or an array, allocates memory for the item and
assigns the address of that memory to the pointer. The delete
operator does the reverse.
Using new and delete operators
The general form of using new operator is:
Eg:
int *p = new int ;
float *q = new float ;
One can also initialize the memory using the new operator. This is
done as follows:
pointer_variable = new data_type(value); // Here, value
specifies // the initial
value
Eg:
int *p = new int(25);
delete [ ] pointer-variable ;
Eg:
int *p = new int[5]; //Creates a memory space for an array of 5 integers.
When creating multi-dimensional arrays with new, all the array sizes must
be supplied.
Eg:
array_ptr = new int[3][5][4] ; // legal
array_ptr = new int[m][5][4] ; // legal
array_ptr = new int[3]]5][ ] ; // illegal
array_ptr = new int[ ][5][4] ; // illegal
Cont.
int *p ;
int *p ;
p=new int[5]; //Allocating space
p = (int*)malloc(sizeof(int)*5); /* Allocating space
// for 5 integer elements
------ for 5 integer elements */
------
------
------
free(p);
delete [ ] p ;
Example: Allocating space for single-dimensional array of integers
#include<iostream.h>
void main( )
{
int n ;
cout << "\n Enter the size of array: ";
cin >> n ;
int *p = new int [n]; // Allocate the space for n integer elements
cout << "\n Enter the elements in the array: \n ";
for(int i = 0; i < n; i++)
{ Sample Output:
cout << "\n p[" << i << "] = " ;
Enter the size of array: 4
cin >> *(p+i);
} Enter the elements in the array:
for(i = 0; i < n; i++) p[0] = 6
{
cout << "\n The elements are: " ; p[1] = 4
cout << *(p+i) ; p[2] = 3
}
p[3] = 5
delete [ ] p; // Deallocate the space
} The elements are: 6 4 3 5
Example: Allocating space for two-dimensional array of integers
#include<iostream.h>
void main()
{
int row, col ;
cout << "\n How many rows to enter: ";
cin >> row ;
cout << "\n How many columns to enter: " ;
cin >> col ;
int **a = new int *[row];
for(int i = 0; i < row; i++)
{
a[i] = new int[col];
for(int j = 0; j < col ; j++)
{
cout << "\n Enter the element at a[" << i << "] [" << j << "]
= ";
cin >> *(*(a+i) + j);
}
}
Cont. Sample Output:
cout << “\n The array is:\n” How many rows to enter: 3
for(i = 0; i < row; i++) How many columns to enter: 3
{
Enter the element at a[0][0] = 3
for(int j = 0; j < col; j++)
{ Enter the element at a[0][1] = 2
cout <<" " << *( *(a+i) + Enter the element at a[0][2] = 1
j);
} Enter the element at a[1][0] = 6
cout << endl; Enter the element at a[1][1] = 3
}
Enter the element at a[1][2] = 4
for(i =0; i < row; i++)
delete[ ] a[i]; Enter the element at a[2][0] = 9
delete[ ] a; Enter the element at a[2][1] = 5
}
Enter the element at a[2][2] = 7
The array is:
3 2 1
6 3 4
9 5 7