p

a
n
c
h
a
m
u
k
e
s
h
.
b
l
o
g
s
p
o
t
.
c
o
m
UNIT II
Function:
DEF: Function is a collection of some executable statements . where in a program to use the concept of
function there should be
a. Function declaration.
b. Function definition.
c. Function call in the main function.
Program 1:
Aim: write a c++program which performs the sum and multiplication of two variables using functions.
Program:
#include<iostream.h>
#include<conio.h>
class IT
{
private :
int a , b ;
IT ( )
{
a=100;
b=20;
}
void add( );
void multi ( );
}OBJ ;
void IT : : add( )
{
cout <<“ sum of the two variables of A and B is :” <<a +b ;
}
void IT : : multi ( )
{
cout <<“ multiplication of two variables of A and B : “ <<a * b;
}
void main( )
{
clrscr( );
OBJ .add ( ) ;
OBJ .multi ( ) ;
getch( );
}
Output:
Sum of the two variables of A and B is: 120
Multiplication of two variables of A and B: 2000
Explanation:
A class name called CSE has 2 Public variables ‘ a ’ and ‘ b’ and Object named ‘ OBJ ‘ was created to
access the information of the class . Values for ‘a’ and ‘b’ wereinitialized by using constructor . And I
want to perform addition of those 2 number using a function ‘add( ) ‘ , ‘ multi ( )‘ which is defined outside
the class by using scope resolution operator So, I called the functions in the main class .
p
a
n
c
h
a
m
u
k
e
s
h
.
b
l
o
g
s
p
o
t
.
c
o
m
Program 2:
Aim: write a c++program which performs the all Arithmetic operations defining two variables using
functions (use switch case).
Program:
#include<iostream.h>
#include<conio.h>
class IT
{
private :
int a , b ;
IT ( )
{
a=100;
b=20;
}
void addition( );
void subtraction ( );
void multiplication();
void division()
}OBJ ;
void IT : : addition ( )
{
cout <<“ sum of the two variables of A and B is :” <<a +b ;
}
void IT : : multiplication ( )
{
cout <<“ multiplication of two variables of A and B : “ <<a * b;
}
void IT : : substraction( )
{
cout <<“ subtraction of the two variables of A and B is :” <<a - b ;
}
void IT : : division ( )
{
cout <<“ division of two variables of A and B : “ <<a / b;
}
void main( )
{
clrscr( );
char choice;
cout <<“ enter your choice to perform the Arithmetic operations “ ;
cin >>choice;
swirch ( choice )
{
case ‘ +‘ :
OBJ .addition ( ) ;
break ;
case ‘ – ‘ :
OBJ .multiplication ( ) ;
p
a
n
c
h
a
m
u
k
e
s
h
.
b
l
o
g
s
p
o
t
.
c
o
m
break ;
case ‘ * ‘ :
OBJ .substraction( ) ;
break ;
case ‘ / ‘ :
OBJ .division ( ) ;
break;
default :
cout <<“you entered wrong choice “;
}
getch( );
}
Output:
enter your choice to perform the Arithmetic operations :
Sum of the two variables of A and B is: 120
FUNCTION OVERLOADING:
DEF: Function Overloading is a concept inwhich one can use many function having the same function
name but can pass different number of parameters or different types of parameters .
EX: void sum ;
void sum( int ) ;
void sum (int , int )
int sum ( ) ;
int sum( int ,int );
float division ( float , float );
char sum ( char ,char );
Program 3:
Aim: write a c++program which performs the function overloading of a function called CALC( ) ?
Program:
#include<conio.h>
#include<iostream.h>
class arithmetic
{
public:
void calc(int num1)
{
cout<<”\n\n Square of a given number:”<<num1*num1<<endl;
}
void calc(int num1,int num2)
{
cout<<”\n\nMultiplication of given number is: “<<num1*num2<<endl;
}
};
Void main()
{
clrscr();
p
a
n
c
h
a
m
u
k
e
s
h
.
b
l
o
g
s
p
o
t
.
c
o
m
arithmetic a;
a.calc(5);
a.calc(6,7);
getch();
}
Output :
Square of the given number is : 25
Multiplication of given number is: 42
The code depicts function overloading. There are two functions with the same name calc. In the
main function, when the function calc is invoked using the object a, depending up on the type and
number of parameters, the compiler binds the call to the function. Hence, when calc(5) is called,
the compiler checks for the function matching the parameter type. So calc(int num l) will be
invoked and parameter will be passed to the function at runtime and output displayed. Similarly,
when calc(6,7) is called, it looks for the same function with two integers as parameter and bind
the respective function to the call.
Program 4:Aim: write a c++program which performs thefunction overloading of a function called sum( ) ?
Program:
#include<iostream.h>
#include<conio.h>
class IT
{
private :
int a , b ,c;
void sum( );
void sum ( int ,int );
int sum (int );
int sum(int ,int ,int ) ;
}OBJ ;
void IT : : sum( )
{
cout <<“ no parameters are passed to perform the addition “;
}
void IT : : sum (int a, int b )
{
cout <<“ addition of two variables of A and B : “ <<a +b;
}
int IT: : sum (int a )
{
return ( a +a );
}
int IT : : sum (int a, int b ,int c)
{
return (a +b +c) ;
}
p
a
n
c
h
a
m
u
k
e
s
h
.
b
l
o
g
s
p
o
t
.
c
o
m
void main( )
{
clrscr( );
OBJ . sum( ) ;
OBJ . sum( 100 , 200 ) ;
cout <<“ sum of one integer is :” <<OBJ . sum( 125 ) ;
cout <<“ sum of three integer numbers A,B and C are :” <<OBJ . sum( 100 , 200,300 ) ;
getch( );
}
Output:
sum of two integer numbers are: no parameters are passed to perform the addition Multiplication of
addition of two variables of A and B : 300
sum of one integer is : 250
sum of three integer numbers A,B and C are : 600
Program 4:
Aim: write a c++program which performs the function overloading of a function called sum( ) ?
Program:
#include<iostream.h>
#include<conio.h>
class IT
{
public :
int sum(int ,int );
double division( double , double );
char character(char ,char );
long multiplication( long ,long )
}OBJ ;
int IT: : sum(int a,int b )
{
return (a+b);
}
double IT : : division(double a, double b )
{
return ( a / b);
}
char IT: : character (char a, char b)
{
return ( a+b );
}
long IT : : multiplication (long a , long b)
{
return (a * b) ;
}
void main( )
{
clrscr( );
cout <<“ sum of two integer numbers are :” <<OBJ . sum( 100 , 200 ) ;
cout <<“ division of two double numbers are :” <<OBJ . division( 123 , 25 ) ;
p
a
n
c
h
a
m
u
k
e
s
h
.
b
l
o
g
s
p
o
t
.
c
o
m
cout <<“ sum of two characters are :” <<OBJ . character( ‘a’ ,‘b’ ) ;
cout <<“ sum of two long numbers are :” <<OBJ . multiplication( 1000 , 2000 ) ;
getch( );
}
Output:
Sum of the two integer variables : 300
Division of two double numbers are : 5.460
Sum of two characters are :
Multiplication of two long variables : 2000000
Program 5:
Aim: write a c++program which performs the function overloading of a different datatypes on the
function?
Program:
#include<iostream.h>
#include<conio.h>
class IT
{
public :
float sum(int ,int );
double division( float , int );
int character(char ,char );
}OBJ ;
float IT : : sum(int a,int b )
{
return (a+b);
}
double IT : : division(float a, int b )
{
return ( a / b);
}
int IT: : character (char a, char b)
{
return ( a+b );
}
void main( )
{
clrscr( );
cout <<“ sum of two integer numbers are converted to float :” <<OBJ . sum( 100 , 200 ) ;
cout <<“ division of one double and one integer numbers are , converted to double :” <<OBJ . division(
123.100 , 25 ) ;
cout <<“ sum of two characters are converted to integer :” <<OBJ . character( ‘r’ ,‘t’ ) ;
getch( );
}
Output:
sum of two integer numbers are converted to float : : 300.000
Division of one double and one integer numbers are converted to double : 148.001
Sum of two characters are converted to integers : 154
p
a
n
c
h
a
m
u
k
e
s
h
.
b
l
o
g
s
p
o
t
.
c
o
m
GENERIC PROGRAMMING :
DEF: Generic programming is a technique that allows the programmer to write the code for any datatype
element . So, the TEMPLATE is a concept used as a tool to implement the generic programming.
Template:
DEF: Template are used for the reusability of the code . There are two categories of templates
a. Function Template .
b. Class Template .
Function Template:
DEF: Function Templates are used to perform identical operations for each type of data compactly and
conveniently .
Syntax:
template <class name_of_datatype >
name_of_datatype function_name (name_of_datatype id1, ……… name_of_datatype idn)
program 6:
Aim: write a c++program which performs the function overloading of a function called sum( ) which can
accept the different datatypes by using generic programming concept ( templates) ?
Program:
#include<iostream.h>
#include<conio.h>
template <class X>
X sum (X a, X b )
{
return (a +b);
}
void main( )
{
clrscr( );
cout <<“ sum of two integer numbers using templates :” <<sum( 125,100 ) ;
cout <<“ sum of two double numbers using tempaltes :” <<OBJ . sum( 1.2345 , 9.3214 ) ;
cout <<“ sum of two characters using templates :” <<sum( ‘m’,’u’ ) ;
cout <<“ sum of two long numbers using templates :” <<sum( 125100, 987650 ) ;
getch( );
}
Output:
sum of two integer numbers using templates : 225
sum of two double numbers using templates : 10.5559
sum of two characters using templates :
sum of two long numbers using templates : 1112750

program 7:
Aim: write a c++program which performs the function overloading of a function called BIG( ) which can
accept the different datatypes by using generic programming concept ( templates) ?
Program:
#include<iostream.h>
#include<conio.h>
p
a
n
c
h
a
m
u
k
e
s
h
.
b
l
o
g
s
p
o
t
.
c
o
m
template <class X>
X BIG(X a, X b )
{
if ( a >b)
{
return (a);
}
else
{
return (b) ;
}}
void main( )
{
clrscr( );
cout <<“ Biggest of two integer numbers using templates :” <<sum( 125,100 ) ;
cout <<“ Biggest of two double numbers using tempaltes :” <<OBJ . sum( 1.2345 , 9.3214 ) ;
cout <<“ Biggest of two characters using templates :” <<sum( ‘m’,’u’ ) ;
cout <<“ Biggest of two long numbers using templates :” <<sum( 125100, 987650 ) ;
getch( );
}
Output:
Biggest of two integer numbers using templates : 225
Biggest of two double numbers using templates : 9.3214
Biggest of two characters using templates : u
Biggest of two long numbers using templates : 987650
Class Templates :
DEF: Class Templates are used to write a class whose members use template parameters as datatypes.
Syntax:
template <class name_of_datatype>
class class_name
{
…….
……..
};
program 8:
Aim: write a c++program which performs generic programming concept ( templates) on class ?
Program:
#include<iostream.h>
#include<conio.h>
template <class X>
class ADD
{
X a,b;
p
a
n
c
h
a
m
u
k
e
s
h
.
b
l
o
g
s
p
o
t
.
c
o
m
Public :
X ADD (X a, X b);
};
template <class X>
X IT <X>: : ADD (X a, X b )
{
return (a +b);
}
void main( )
{
clrscr( );
ADD <int>obj1(125,100) ;
ADD <double>obj2(1.234,1.234);
cout <<“ sum of two integer numbers using templates :” <<obj1.ADD( ) ;
cout <<“ sum of two double numbers using tempaltes :” <<OBJ . ADD( ) ;
getch( );
}
Output:
sum of two integer numbers using templates : 225
sum of two double numbers using templates : 2.246

program 9:
Aim: write a c++program which performs the function overloading of a function called BIG( ) which can
accept the different datatypes by using generic programming concept ( templates) ?
Program:
#include<iostream.h>
#include<conio.h>
template <class X>
class BIG
{
X a,b;
Public :
X BIG(X a, X b);
}
template <class X>
X CSE <X>: : BIG (X a, X b )
{
if ( a >b)
{
return (a);
}
else
{
return (b) ;
}}
void main( )
{
clrscr( );
BIG <int>obj1(125,100);
BIG <char>obj2(‘a’,’b’);
p
a
n
c
h
a
m
u
k
e
s
h
.
b
l
o
g
s
p
o
t
.
c
o
m
cout <<“ Biggest of two integer numbers using templates :” <<obj1.BIG( ) ;
cout <<“ Biggest of two characters using templates :” <<obj2. BIG( ) ;
getch( );
}
Output:
Biggest of two integer numbers using templates : 225
Biggest of two characters using templates : b
Advantages of C++Class Templates:
 One C++Class Template can handle different types of parameters.
 Compiler generates classes for only the used types. If the template is instantiated for int
type, compiler generates only an int version for the c++template class.
 Templates reduce the effort on coding for different data types to a single set of code.
 Testing and debugging efforts are reduced.
INHERITANCE:
DEF: Inheritance is acquiring the properties of Base class to Derived class . The derived class
inherits all the capabilities of the base class but can add members and refinements of its
own. The base class is unchanged by this process. Inheritance has important advantages.
Most importantly, it permits code reusability.
A derived class is defined by specifying its relationship with the base class in addition
to its own details.
The general form of defining a derived class is:
class derived-class-name : visibility-mode base-class-name
{
………… //
………… II members of derived class
…………II
};
The colon indicates that the derived-class-name is derived from the base-class-name.
The visibility mode is optional and, if present, may be either private or public. The
default visibility-mode is private. Visibility mode specifies whether the features of the
base class are privately derived or publicly derived.
Examples:
class ABC: private XVZ //private derivation
{
members of ABC
};
class ABC : public XVZ //public derivation 3
{
members of ABC
};
class ABC : XVZ //private derivation by default
{
members of ABC
};
p
a
n
c
h
a
m
u
k
e
s
h
.
b
l
o
g
s
p
o
t
.
c
o
m
When a derived class privately inherits a base class, 'public members' of the base class
become 'private members' of the derived class and therefore the publicmembers of
the base class can only be accessed by the member functions of the derived class.
They are inaccessible to the objects of the derived class. Remember, a public member
of a class can be accessed by its own objects using the dot operator. The result is that
no member of the base class is accessible to the objects of the derived class.
On the other hand, when the base class is publicly inherited, 'public members' of the
base class become 'public members' of the derived class and therefore they are
accessible to the objects of the derived class. In both the cases, the private members
are not inherited and therefore, the private members of a base class will never become
the members of its derived class.
In inheritance, some of the base class data elements and member functions are
'inherited' into the derived class. We can add our own data and member functions and
thus extend the functionality of the base class. Inheritance, when used to modify and
extend the capabilities of the existing classes, becomes a very powerful tool for
incremental program development.
There are four types of inheritances
a. Single inheritance.
b. Multilevel inheritance.
c. Multiple inheritance.
d. Hybrid inheritance.
Type of inheritance
Base class
member
access
specifier
public
inheritance
protected
inheritance
private
inheritance
Public
public in derived class.
Can be accessed directly by any
non-static member functions,
friend functions and non-
member functions.
protected in derived class.
Can be accessed directly by all
non-static member functions
and friend functions.
private in derived class.
Can be accessed directly by all
non-static member functions
and friend functions.
Protected
protected in derived class.
Can be accessed directly by all
non-static member functions
and friend functions.
protected in derived class.
Can be accessed directly by all
non-static member functions
and friend functions.
private in derived class.
Can be accessed directly by all
non-static member functions
and friend functions.
Private
Hidden in derived class.
Can be accessed by non-static
member functions and friend
functions through public or
protected member functions
of the base class.
Hidden in derived class.
Can be accessed by non-static
member functions and friend
functions through public or
protected member functions
of the base class.
Hidden in derived class.
Can be accessed by non-static
member functions and friend
functions through public or
protected member functions
of the base class.
p
a
n
c
h
a
m
u
k
e
s
h
.
b
l
o
g
s
p
o
t
.
c
o
m
visibility of members undergo modificationswhen they are inherited.
Table: Visibility of inherited members
Single Inheritance :
DEF: Single Inheritance is acquiring the properties from one base class to derived class .
BASE CLASS
Program 10:
Aim: write a c++program which performs the sum and multiplication of two private variables declared in
two different class using the concept of single inheritance ?
Program:
#include<iostream.h>
#include<conio.h>
class BASE
{
protected :
int a , b ;
BASE ( )
{
a=100;
b=20;
}
};
DERIVED CLASS
p
a
n
c
h
a
m
u
k
e
s
h
.
b
l
o
g
s
p
o
t
.
c
o
m
class DERIVED : public BASE
{
public :
void ADD( );
void MULTI ( );
};
void DERIVED : : ADD( )
{
cout <<“ sum of the two variables of A and B is :” <<a +b ;
}
void DERIVED : : MULTI ( )
{
cout <<“ multiplication of two variables of A and B : “ <<a * b;
}
void main( )
{
clrscr( );
DERIVED OBJ ;
OBJ .ADD ( ) ;
OBJ .MULTI ( ) ;
getch( );
}
Output:
Sum of the two variables of A and B is: 120
Multiplication of two variables of A and B: 2000
Multilevel Inheritance :
DEF: Multilevel Inheritance is that which acquires the properties from already existing Derived class to
another Derived class .
BASE CLASS
Program 11:
DERIVED 1 CLASS
DERIVED 2 CLASS
p
a
n
c
h
a
m
u
k
e
s
h
.
b
l
o
g
s
p
o
t
.
c
o
m
Aim: write a c++program which performs the sum and multiplication of two private variables declared in
two different class using the concept of multilevel inheritance ?
Program:
#include<iostream.h>
#include<conio.h>
class BASE
{
protected :
int a ;
BASE ( )
{
a=100;
}};
class DERIVED1 : public BASE
{
protected:
int b;
public:
DERIVED1( )
{
b=20;
}};
class DERIVED2 : public DERIVED1
{
public :
void ADD( );
void MULTI ( );
}};
void DERIVED2 : : ADD( )
{
cout <<“ sum of the two variables of A and B is :” <<a +b ;
}
void DERIVED2 : : MULTI ( )
{
cout <<“ multiplication of two variables of A and B : “ <<a * b;
}
void main( )
{
clrscr( );
DERIVED2 OBJ ;
OBJ .ADD ( ) ;
OBJ .MULTI ( ) ;
getch( );
}
Output:
Sum of the two variables of A and B is: 120
Multiplication of two variables of A and B: 2000
p
a
n
c
h
a
m
u
k
e
s
h
.
b
l
o
g
s
p
o
t
.
c
o
m
Multiple Inheritance :
DEF: Multiple Inheritance occurs when one derived class acquires the properties from two Base classes .
Program 12:
Aim: write a c++program which performs the sum and multiplication of twoprivate variables using the
concept of multiple inheritance ?
Program:
#include<iostream.h>
#include<conio.h>
class BASE1
{
protected :
int a ;
BASE1 ( )
{
a=100;
}};
class BASE2
{
protected:
int b;
public :
BASE2 ( )
{
b=20;
}};
class DERIVED : public BASE1 ,public BASE2
{
public :
void ADD( );
void MULTI ( );
}};
void DERIVED : : ADD( )
{
cout <<“ sum of the two variables of A and B is :” <<a +b ;
}
void DERIVED : : MULTI ( )
BASE 1 CLASS BASE 2 CLASS
DERIVED CLASS
p
a
n
c
h
a
m
u
k
e
s
h
.
b
l
o
g
s
p
o
t
.
c
o
m
{
cout <<“ multiplication of two variables of A and B : “ <<a * b;
}
void main( )
{
clrscr ( );
DERIVED OBJ ;
OBJ .ADD ( ) ;
OBJ .MULTI ( ) ;
getch( );
}
Output:
Sum of the two variables of A and B is: 120
Multiplication of two variables of A and B: 2000
Hybrid Inheritance :
DEF: Hybrid Inheritance occurs when one derived class acquires the properties from two Base classes .
Program 13:
Aim: write a c++program which performs the sum and multiplication of two private variables using the
concept of hybrid inheritance ?
Program:
#include<iostream.h>
#include<conio.h>
class BASE1
{
protected :
int a ;
public:
BASE 2 CLASS
DERIVED CLASS
BASE 1 CLASS
DERIVED CLASS
p
a
n
c
h
a
m
u
k
e
s
h
.
b
l
o
g
s
p
o
t
.
c
o
m
BASE1 ( )
{
a=100;
}};
class DERIVED1 : public BASE1
{
protected :
int b;
public:
DERIVED1( )
{
b=a;
}};
class BASE2
{
protected:
int c;
public :
BASE2 ( )
{
c=20;
}};
class DERIVED2 : public DERIVED!, public BASE2
{
public :
void ADD( );
void MULTI ( );
}};
void DERIVED2 : : ADD( )
{
cout <<“ sum of the two variables of A and B is :” <<b +c ;
}
void DERIVED2 : : MULTI ( )
{
cout <<“ multiplication of two variables of A and B : “ << b * c;
}
void main( )
{
clrscr ( );
DERIVED2 OBJ ;
OBJ .ADD ( ) ;
OBJ .MULTI ( ) ;
getch( );
}
Output:
Sum of the two variables of A and B is: 120
Multiplication of two variables of A and B: 2000
p
a
n
c
h
a
m
u
k
e
s
h
.
b
l
o
g
s
p
o
t
.
c
o
m
VIRTUAL BASE CLASS :
Consider one scenario :there are two derived classes DERIVED 1 and DERIVED 2 , which are derived
from the base class BASE . Now there is one more derived class DERIVED 3 which directly inherits
DERIVED 1 and DERIVED 2 . This also mean that base is inherited twicw one through DERIVED 1 and
another DERIVED 2 .
This will create an ambiguity when DERIVED 3 will try to use any member of the base class two copies of
the base are included in DERIVED 3 . To resolve this problem , the two directly derived classes from one
base class are made virtual . This mechanism is called Virtual Base Class . By this property only one copy
of the base class will be copied in DERIVED 3 .
Program 14:
Aim: write a c++program which performs the concept of virtual base class ?
Program:
#include<iostream.h>
#include<conio.h>
class BASE1
{
protected :
int a ;
public :
BASE1( )
{
a=100;
}};
class DERIVED1 : virtual public BASE1
BASE
DERIVED 1
DERIVED 2

DERIVED 3
p
a
n
c
h
a
m
u
k
e
s
h
.
b
l
o
g
s
p
o
t
.
c
o
m
{
protected :
int b;
public:
DERIVED1( )
{
b=a;
}};
class DERIVED2: virtual public BASE1
{
protected:
int c;
public :
BASE2 ( )
{
c=a;
}};
class DERIVED3 : public DERIVED1 ,public DERIVED2
{
public :
void ADD( );
void MULTI ( );
}};
void DERIVED3 : : ADD( )
{
cout <<“ sum of the three variables of A and B is :” <<a+b +c ;
}
void DERIVED3 : : MULTI ( )
{
cout <<“ multiplication of three variables of A and B : “ << a*b * c;
}
void main( )
{
clrscr ( );
DERIVED3 OBJ ;
OBJ .ADD ( ) ;
OBJ .MULTI ( ) ;
getch( );
}
Output:
Sum of the two variables of A and B is: 120
Multiplication of two variables of A and B: 2000
p
a
n
c
h
a
m
u
k
e
s
h
.
b
l
o
g
s
p
o
t
.
c
o
m
FUNCTION OVERRIDING :
DEF: Function overriding is redefining a functiondefinition in a derived class which is already defined in
the base class .

Program 10:
Aim: write a c++program which performs function overriding ?
Program:
#include<iostream.h>
#include<conio.h>
class BASE
{
protected :
int a , b ;
public :
BASE ( )
{
a=100;
b=20;
}
int ADD( )
{
return (a+b);
}};
class DERIVED : public BASE
{
public :
int ADD( )
{
return (a - b);
}};
void main( )
{
clrscr( );
DERIVED OBJ ;
cout <<“ BASE class returns addition of two numbers “ <<OBJ .ADD ( ) ;
cout <<“ DERIVED class returns subtraction of two numbers “ <<OBJ .ADD ( ) ;
getch( );
}
Output:
BASE class returns addition of two numbers: 120
DERIVED class returns subtraction of two numbers: 80
Difference between function overloading and function overriding ?
1. In function overloading different number of parameters can be passed whereas in function
overriding the number of parameters that are passed are the same .
2. The function overloading may have different return types , but in function overriding the return
type of the base and derived member function should be same .
3. function overloading is defining multiple member functions with the same name but with different
signature . overriding is a method that allows the derived class to redefine the behavior of the
member functions , here the signature should be same in both base and derived classes .
p
a
n
c
h
a
m
u
k
e
s
h
.
b
l
o
g
s
p
o
t
.
c
o
m
RUNTIME POLYMORPHISM USING VIRTUAL FUNCTIONS :
VIRTUAL FUNCTION:
DEF: virtual function is a member function that is defined within a base class and redefined in the
derived class .
DEF: Polymorphism is the ability to take more than one form .So, Runtime polymorphism is that which
takes place at the time of execution of the program .here it uses the concept of Function overriding to
achieve the polymorphism because , there the function can be redefined in the derived which is already
defined in the base class .
Program 10:
Aim: write a c++program which demonstrates the use of function overriding to achieve the runtime
polymorphism by using virtual function ?
Program:
#include<iostream.h>
#include<conio.h>
class BASE
{
protected :
int a , b ;
public :
BASE ( )
{
a=100;
b=20;
}
virtual int ADD( )
{
return (a+b);
}};
class DERIVED : public BASE
{
public :
int ADD( )
{
return (a - b);
}};
void main( )
{
clrscr( );
DERIVED OBJ ;
cout <<“ BASE class returns addition of two numbers “ <<OBJ .ADD ( ) ;
cout <<“ DERIVED class returns subtraction of two numbers “ <<OBJ .ADD ( ) ;
getch( );
}
Output:
BASE class returns addition of two numbers: 120
DERIVED class returns subtraction of two numbers: 80
p
a
n
c
h
a
m
u
k
e
s
h
.
b
l
o
g
s
p
o
t
.
c
o
m
PURE VIRTUAL FUNCTION:
DEF: Pure virtual function is a member function that is declared within a base class with a value ‘zero’
i.e’s 0 and redefined in the derived class .
So, in base class the function will be assigned with a value ‘zero’ with out any body definition , but it
should be defined in the derived class , or else it will displaying a runtime error .
Program 10:
Aim: write a c++program which demonstrates the use of pure virtual functions ?
Program:
#include<iostream.h>
#include<conio.h>
class BASE
{
protected :
int a , b ;
public :
BASE ( )
{
a=100;
b=20;
}
virtual int ADD( )=0;
};
class DERIVED : public BASE
{
public :
int ADD( )
{
return (a - b);
}};
void main( )
{
clrscr( );
DERIVED OBJ ;
cout <<“ DERIVED class returns subtraction of two numbers “ <<OBJ .ADD ( ) ;
getch( );
}
Output:
DERIVED class returns subtraction of two numbers: 80

ABSTRACT CLASSES :
DEF: Abstract class is that which has at least one pure virtual function .
So, in base class the function will be assigned with a value ‘zero’ with out any body definition , but it
should be defined in the derived class , or else it will displaying a runtime error .
Program 10:
Aim: write a c++program which demonstrates the use of pure virtual functions to get a Abstract class ?
Program:
#include<iostream.h>
#include<conio.h>
p
a
n
c
h
a
m
u
k
e
s
h
.
b
l
o
g
s
p
o
t
.
c
o
m
class BASE
{
protected :
int a , b ;
public :
BASE ( )
{
a=100;
b=20;
}
virtual int ADD( )=0;
};
class DERIVED : public BASE
{
public :
int ADD( )
{
return (a - b);
}};
void main( )
{
clrscr( );
DERIVED OBJ ;
cout <<“ DERIVED class returns subtraction of two numbers “ <<OBJ .ADD ( ) ;
getch( );
}
Output:
DERIVED class returns subtraction of two numbers: 80
this Pointer:
The this pointer is predefined in member functions to point to the object of which the
function is a member. The this pointer is useful in returning the object of which the
function is a member. The member functions of every object have access to a sort of
magic pointer named this, which points to the object itself. Thus any member
function can find out the address of the object of which it is a member.
#include<iostream.h>
class where
{
private:
char charray[10];
public:
void reveal( )
{ cout<<”\n My object’s address is”<<this; }
};
void main( )
{
where w1,w2, w3; // make three objects
w1.reveal( )
p
a
n
c
h
a
m
u
k
e
s
h
.
b
l
o
g
s
p
o
t
.
c
o
m
w2.reveal( )
w3.reveal( )
}
The main( ) program in this example creates three objects of type where. It then asks
each object to print its address, using the reveal( ) member function. This function
prints out the value of thispointer.


PREVIOUS PAPER QUESTIONS :
1. What is template? Explain about function templates and class templates with
suitable examples. [16]
2.What is the difference between ‘overloading’ and “overriding”? [4]
3. (a) What is Hybrid inheritance? Write a program to illustrate the concept of
Hybrid Inheritance.
(b) What is single inheritance? Write a program to illustrate the concept of single
Inheritance. [8+8]
4. (a) What’s the deal with operator overloading?
(b) What are the benefits of operator overloading?
(c) What are some examples of operator overloading?
(d) What operators can/cannot be overloaded? [4+4+4+4]
Programs :
Ex.1 : Calculate the area of a circle of given radius. The value of pi is constant, and
should not be changed within the program, so we recognize this by declaring it as a
const.
#include <iostream.h>
#include <conio.h>
int main()
{
// Initialize constant variables
const float pi =3.1416f;
float radius =0;
float areaOfCircle =0;
cout <<"This program will compute the area of a circle." <<endl
<<"We assume that the value of pi is " <<pi <<"." <<endl;
cout <<"Please enter the radius: ";
cin >>radius;
areaOfCircle =pi * radius * radius;
cout <<endl
<<"The area of the circle equals "
<<areaOfCircle <<" square units." <<endl;
p
a
n
c
h
a
m
u
k
e
s
h
.
b
l
o
g
s
p
o
t
.
c
o
m
return 0;
}
Ex 9.1 Overloaded function plus(), with definitions provided for a pair on int,
double or string arguments. The statement d = plus(3, 4.2); fails because the
function call is ambiguous; the compiler can't be sure whether you intended to use
the int or double version of the overloaded function plus(), so it flags up the problem
as an error rather than trying to guess.
#include <iostream>
#include<string>
using namespace std;
int plus(const int& var1, const int& var2);
double plus(const double& var1, const double& var2);
string plus(const string& var1, const string& var2);
int main()
{
int n =plus(3, 4);
double d =plus(3.2, 4.2);
string s =plus("he", "llo");
string s1 ="aaa"; string s2 ="bbb";
string s3 =plus(s1, s2);
cout <<n <<endl
<<d <<endl
<<s <<endl
<<s3 <<endl;
// d =plus(3, 4.2); // Uncomment for an error
return 0;
}
int plus(const int& var1, const int& var2)
{
return var1 +var2;
}
double plus(const double& var1, const double& var2)
{
return var1 +var2;
}
string plus(const string& var1, const string& var2)
{
return var1 +var2;
}
p
a
n
c
h
a
m
u
k
e
s
h
.
b
l
o
g
s
p
o
t
.
c
o
m
Ex 12.2 Modifying the Integer class to show the effect of pass by reference
#include <iostream.h>
#include <conio.h>
class Integer
{
private:
int value;
public:
Integer():value(0){ cout <<"\nDefault constructor called."; } // Default constructor
Integer(int n):value(n){ cout <<"\nConstructor called."; } // Constructor
~Integer(){ cout <<"\nDestructor called."; } // Destructor
int getValue(){ return value; } const // Get the value of the data member
void setValue(int number){ value =number; } // Set the value of the data member
};
// Function to square the value of an Integer - argument passed by value
void squareInteger(Integer number)
{
cout <<"\nSquaring value - argument passed by value.";
number.setValue(number.getValue()*number.getValue());
}
// Function to double the value of an Integer - call by reference
void twiceInteger(Integer& number)
{
cout <<"\nDoubling value - argument passed by reference.";
number.setValue(2*number.getValue()); }
int main()
{
Integer first(5);
cout <<"\nInitial value is " <<first.getValue();
squareInteger(first);
cout <<"\nValue is now " <<first.getValue();
twiceInteger(first);
cout <<"\nValue is now " <<first.getValue() <<endl;
return 0;
}
The output from the program is as follows:
Constructor called.
Initial value is 5
Squaring value - argument passed by value.
Destructor called.
Value is now 5
p
a
n
c
h
a
m
u
k
e
s
h
.
b
l
o
g
s
p
o
t
.
c
o
m
Doubling value - argument passed by reference.
Value is now 10
Destructor called.
The first constructor message arises from the declaration of first.
The first destructor called message is due to calling squareInteger(). Since the argument
is passedby value, the compiler arranges for a copy of the argument to be made (by
calling the default copyconstructor), and when the function returns, the destructor is
called to destroy the copy. CallingtwiceInteger() does not result in a destructor call
because the argument is passed by reference, sono copy is created. The final destructor
call destroys the first object at the end of the program.
Ex: write a CPP to perform constructor and destructor ?
#include <iostream.h>
#include <conio.h>
class person
{
public:
char *name;
int age;
person (char *n = "no name", int a = 0)
{
name = new char[100]; // better than malloc!
strcpy (name, n);
age = a;
cout << "Instance initialized, 100 bytes allocated" << endl;
}
~person () // The destructor
{
delete name; // instead of free!
cout << "Instance going to be deleted, 100 bytes freed" << endl;
}
};
void main ()
{
cout << "Hello!" << endl << endl;
person a;
cout << a.name << ", age " << a.age << endl << endl;
person b ("John");
cout << b.name << ", age " << b.age << endl << endl;
b.age = 21;
cout << b.name << ", age " << b.age << endl << endl;
person c ("Miki", 45);
cout << c.name << ", age " << c.age << endl << endl;
cout << "Bye!" << endl << endl;
}
p
a
n
c
h
a
m
u
k
e
s
h
.
b
l
o
g
s
p
o
t
.
c
o
m
EX: write CPP to inheritance ?
#include<iostream.h>
#include<conio.h>
#include<new.h>
Class vector
{
public:
double x;
double y;
vector (double a = 0, double b = 0)
{
x = a;
y = b;
}
double module()
{
return sqrt (x*x + y*y);
}
double surface()
{
return x * y;
}
};
class trivector: public vector // trivector is derived from vector
{
public:
double z; // added to x and y from vector
trivector (double m=0, double n=0, double p=0): vector (m, n)
{
z = p; // Vector constructor will
} // be called before trivector
// constructor, with parameters
// m and n.
trivector (vector a) //What to do if a vector is
{ // cast to a trivector
x = a.x;
y = a.y;
z = 0;
}
double module () // define module() for trivector
{
return sqrt (x*x + y*y + z*z);
}
double volume ()
{
return this->surface() * z; // or x * y * z
}
};
void main()
{
vector a (4, 5);
trivector b (1, 2, 3);
cout << "a (4, 5) b (1, 2, 3) *r = b" << endl << endl;
cout << "Surface of a: " << a.surface() << endl;
cout << "Volume of b: " << b.volume() << endl;
p
a
n
c
h
a
m
u
k
e
s
h
.
b
l
o
g
s
p
o
t
.
c
o
m
cout << "Surface of base of b: " << b.surface() << endl;
cout << "Module of a: " << a.module() << endl;
cout << "Module of b: " << b.module() << endl;
cout << "Module of base of b: " << b.vector::module() << endl;
trivector k;
k = a; // thanks to trivector(vector) definition
// copy of x and y, k.z = 0
vector j;
j = b; // copy of x and y. b.z leaved out
vector *r;
r = &b;
cout << "Surface of r: " << r->surface() << endl;
cout << "Module of r: " << r->module() << endl;
}
EX : passes array as argument
#include <iostream.h>
#include <iomanip.h>
const int DISTRICTS =4;
const int MONTHS =3;
void display( float[DISTRICTS][MONTHS] );
void main( )
{
float sales[DISTRICTS][MONTHS] ={ { 1432.07, 234.50, 654.01 },
{ 322.00, 13838.32, 17589.88 },
{ 9328.34, 934.00, 4492.30 },
{ 12838.29, 2332.63, 32.93}
};
display( );
}
void display( float funsales[DISTRICTS][MONTHS] )
{
int d, m;
cout <<"\n\n" .
cout <<"month\n”;
cout <<" 1 2 3";
for(d=0; d<DISTRICTS; d++)
{
cout <<"\nDistrict " <<d+1;
for(m =0; m<MONTHS; m++)
cout <<setw(10) <<setprecision(2) <<funsales[d][m];
}
}

Sign up to vote on this title
UsefulNot useful