Professional Documents
Culture Documents
C++POLYMORHISM Notes
C++POLYMORHISM Notes
Polymorphism
Static Dynamic
Whenever same method name is existing multiple times in the same class with
different number of parameter or different order of parameters or different
types of parameters is known as method/function overloading.
A compiler calls correct function out of the others on the basis of their number of
parameters or datatypes in arguments.
FUNCTION OVERLOADING
• Conditions for function overloading are:-
• Functions to be overloaded must have the same name.
• All functions must have different arguments( either a different number of
parameters or different type of parameters ).
• In next example method "sum()" is present in addition class with same name but
with different signature or arguments.
EXAMPLE
#include<iostream>
int main()
{
using namespace std;
class shape shape s;
{ cout<<"area of square="<<s.area(10)<<endl;
cout<<"area of rectangle="<<s.area(10,20)<<endl;
public: return 0;
int area(int dim)
{ }
return dim*dim;
}
int area(int len,int wdt)
{
return len*wdt;
}
};
#include <iostream>
using namespace std;
class rectangle
{ int main()
public:
void printarea(int x, int y) {
{ rectangle t;
cout << x * y << endl;
} t.printarea(2,4);
void printarea(int x) t.printarea(2,5.1);
{
cout << x * x << endl; t.printarea(10);
} t.printarea(2.3);
void printarea(int x, double y)
{ return 0;
cout << x * y << endl; }
}
void printarea(double x)
{
cout << x * x << endl;
}
};
RUNTIME
POLYMORPHISM
• Function overriding is an example of Runtime or dynamic polymorphism.
• Function overriding enables us to have an equivalent function in child class
which is already defined in the parent class.
• Function Overriding: When child class declares a method, which is already
present in the parent class then this is called function overriding, here child
class overrides the parent class.
• In case of function overriding we have two definitions of the same function,
one is parent class and one in child class.
• The call to the function is determined at runtime to decide which definition
of the function is to be called, thats the reason it is called runtime
polymorphism.
FUNCTION OVERRIDING
• Conditions for function overriding
• Functions of both parent and child class must have the
same name.
• Functions must have the same argument list and return
type.
• A function declared static cannot be overridden.
• If a function cannot be inherited, it cannot be overridden.
FUNCTION OVERRIDING
• In case of function overriding we have two definitions of the
same function, one is parent class and one in child class.
• The call to the function is determined at runtime to decide which
definition of the function is to be called, thats the reason it is
called runtime polymorphism.
• Write a C++ program that accepts the dimensions of a
rectangle and a rhombus. The program should then use an
overloaded function named area to determine area of the
respective figures. Output the dimensions and areas of the
figures.
Write a C++ program that will carry out the following:
• define a class named circular that has data members named radius and height, a member
function named set which is used to initialize the value of length and height and a function named
volume,
• implements a derived class from circular named cylinder whose radius and height are 14 cm and
10 cm respectively;
• implements a derived class from circular named cone whose radius and height are 14cm and 5
cm respectively;