Polymorphism
In
C++ Presented by:
Vishesh Kumar jha
04620602018
BCA 3rd Semester 1st Shift
jhavishesh123@gmail.com
TRINITY INSTITUTE OF PROFESSIONAL STUDIES
(Affiliated to Guru Gobind Singh Indraprastha University,Delhi)
Ranked “A+” Institution of GGSIPU, Delhi
Recognised under section 2(f) of the UGC Act, 1956
NAAC Accredited “B++” Grade Institution
Ranked “A+” Institution by SFRC, Govt. of NCT of India
POLYMORPHISM
 Polymorphism is derived from 2Greek words: poly and
morphs. The word "poly" means many and morphs
means forms.
Polymorphism is the ability to use an operator or function
in different manner,it gives the special meaning to the
function or operator and also it refers to the
codes,operations,or objects that behave differently in
different context.
Real life example of Polymorphism
Suppose if you are in class room that time you behave
like a student, when you are in market at that time you
behave like a customer, when you at your home at that
time you behave like a son or daughter, Here one person
have different-different behaviors.
Type of Polymorphism
 There are mainly five types of polymorphism 1.Ad-hoc 2.Parametric 3.Sub-typing
4.Row 5.Polytypism.
 Static polymorphism is also known as early binding and compile-time polymorphism.
In static polymorphism memory will be allocated at compile-time.
Dynamic polymorphism is also known as late binding and run-time polymorphism. In
dynamic polymorphism memory will be allocated at run-time.
Polymorphism
Static
Function
Overloading
Operator
Overloading
Dynamic
Virtual
Functions
Method Overloading
Whenever same method name is exiting 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 overloading.
In next example method "sum()" is present in Addition class with same namebut
with different signature or arguments.
Function Overloading Example
class Addition {
public: void sum(int a, int b)
{
//output :-a+b : 30
//output :-a+b+c :60
cout<<"a+b :"<<a+b; }
void sum(int a, int b, int c)
{
cout<<"a+b+c :"<<a+b+c; }
};
int main() {
Addition obj;
obj.sum(10, 20);
cout<<endl;
obj.sum(10, 20,30);
}
Operator Overloading
The process of making an operator to exhibit different behaviors in different
instances is known as operator overloading.
Only predefined operator can be overloaded.
Some operators which can’t be overloaded such as scope resolution
operator , sizeof operator,class member acess and conditional operator etc.
Types Of Operator Overloading
Unary operator overloading.
These Operators have only single operand.
Examples:- ++,--,~,!
Binary operator overloading.
These operators can have two or more operands.
Examples:-+,-,*,/,%,^,=,==,+=,&,&& etc
Operator Overloading Example
void main()
{
clrscr();
test p1;
p1.gt(-10,-90,-23);
p1.dp();
cout<<"Values after operator overloading"<<endl;
-p1;
p1.dp();
getch();
}
#include<iostream.h>
#include<conio.h>
class test{
int a,b,c;
public:
void get(int x,int y,int z);
void operator -();
void dp();
};
void test::gt(int x,int y,int z)
{a=x;
b=y;
c=z;}
void test::operator-()
{ a=-a;
b=-b;
c=-c;}
void test::dp()
{cout<<"Value of a="<<a<<endl;
cout<<"Value of b="<<b<<endl;
cout<<"Value of c="<<c<<endl;
}
Virtual Function
A virtual function is a member function that is declared as virtual within abase
class and redefined by a derived class.
To create virtual function, precede the base version of function’s declaration with
the keyword virtual.
 Here we use a pointer to the base class to refer to all the derived objects.
The function name and type signature should be same for both base and derived
version of function.
Virtual Function Example
class Base {
public: virtual void show() {
cout<<"Content of base class.n"; }};
class derived:publicA
{ public:
void show() {
cout<<"Content of derived class.n"; }};
int main() {
Base *a //Base class pointer
derivedp1; //Derived class
a= &p1;
a->show(); //Late Binding Occurs
getch();
return 0; }
Polymorphism In c++

Polymorphism In c++

  • 1.
    Polymorphism In C++ Presented by: VisheshKumar jha 04620602018 BCA 3rd Semester 1st Shift jhavishesh123@gmail.com TRINITY INSTITUTE OF PROFESSIONAL STUDIES (Affiliated to Guru Gobind Singh Indraprastha University,Delhi) Ranked “A+” Institution of GGSIPU, Delhi Recognised under section 2(f) of the UGC Act, 1956 NAAC Accredited “B++” Grade Institution Ranked “A+” Institution by SFRC, Govt. of NCT of India
  • 2.
    POLYMORPHISM  Polymorphism isderived from 2Greek words: poly and morphs. The word "poly" means many and morphs means forms. Polymorphism is the ability to use an operator or function in different manner,it gives the special meaning to the function or operator and also it refers to the codes,operations,or objects that behave differently in different context.
  • 3.
    Real life exampleof Polymorphism Suppose if you are in class room that time you behave like a student, when you are in market at that time you behave like a customer, when you at your home at that time you behave like a son or daughter, Here one person have different-different behaviors.
  • 4.
    Type of Polymorphism There are mainly five types of polymorphism 1.Ad-hoc 2.Parametric 3.Sub-typing 4.Row 5.Polytypism.  Static polymorphism is also known as early binding and compile-time polymorphism. In static polymorphism memory will be allocated at compile-time. Dynamic polymorphism is also known as late binding and run-time polymorphism. In dynamic polymorphism memory will be allocated at run-time. Polymorphism Static Function Overloading Operator Overloading Dynamic Virtual Functions
  • 5.
    Method Overloading Whenever samemethod name is exiting 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 overloading. In next example method "sum()" is present in Addition class with same namebut with different signature or arguments.
  • 6.
    Function Overloading Example classAddition { public: void sum(int a, int b) { //output :-a+b : 30 //output :-a+b+c :60 cout<<"a+b :"<<a+b; } void sum(int a, int b, int c) { cout<<"a+b+c :"<<a+b+c; } }; int main() { Addition obj; obj.sum(10, 20); cout<<endl; obj.sum(10, 20,30); }
  • 7.
    Operator Overloading The processof making an operator to exhibit different behaviors in different instances is known as operator overloading. Only predefined operator can be overloaded. Some operators which can’t be overloaded such as scope resolution operator , sizeof operator,class member acess and conditional operator etc. Types Of Operator Overloading Unary operator overloading. These Operators have only single operand. Examples:- ++,--,~,! Binary operator overloading. These operators can have two or more operands. Examples:-+,-,*,/,%,^,=,==,+=,&,&& etc
  • 8.
    Operator Overloading Example voidmain() { clrscr(); test p1; p1.gt(-10,-90,-23); p1.dp(); cout<<"Values after operator overloading"<<endl; -p1; p1.dp(); getch(); } #include<iostream.h> #include<conio.h> class test{ int a,b,c; public: void get(int x,int y,int z); void operator -(); void dp(); }; void test::gt(int x,int y,int z) {a=x; b=y; c=z;} void test::operator-() { a=-a; b=-b; c=-c;} void test::dp() {cout<<"Value of a="<<a<<endl; cout<<"Value of b="<<b<<endl; cout<<"Value of c="<<c<<endl; }
  • 9.
    Virtual Function A virtualfunction is a member function that is declared as virtual within abase class and redefined by a derived class. To create virtual function, precede the base version of function’s declaration with the keyword virtual.  Here we use a pointer to the base class to refer to all the derived objects. The function name and type signature should be same for both base and derived version of function.
  • 10.
    Virtual Function Example classBase { public: virtual void show() { cout<<"Content of base class.n"; }}; class derived:publicA { public: void show() { cout<<"Content of derived class.n"; }}; int main() { Base *a //Base class pointer derivedp1; //Derived class a= &p1; a->show(); //Late Binding Occurs getch(); return 0; }