OOSP - Basics
OOSP - Basics
Using C++
By
Hemraj Saini
Associate Professor
Department of Computer Science and
Engineering
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;
The value of str = hello
cout << “\n The value of str = “ << str;
}
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(f1, i1); // 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.
#include<iostream.h>
void update1(int);
void update2(int*);
void update3(int&);
void main()
{
int i =j = k = 5;
update1(i) ; // Call by value
update2(&j); // Call by pointer
update3(k); // Call by reference
cout << “\n i = “ << i ;
cout << “\n j = “ << j ;
cout << “\n k = “ << k ;
}
Cont.
void update1(int i);
{
i = i + 1;
cout << “\n i = “ << i ;
}
void update2(int* j)
{
*j = *j + 1;
cout << “\n *j = “ << *j ;
}
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.
}
new and delete operators
• The dynamic memory allocation in C is done by using the
functions: malloc() and calloc().
• 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.