KEMBAR78
Polymorphism in C++ | PPTX
POLYMORPHISM
1
Presented by: The Camouflage
Gagan Puri
Bikram Bhurtel
Rabin BK
BSc.CSIT 3rd Semester
Introduction
Static polymorphism
Overloading
Overriding
Dynamic polymorphism
Virtual function and binding
References
2
Derived form the Greek word polymorphous
Real life example:- a man at a same time is a father, a husband, a
employee
In C++ how polymorphism can be achieved
Introduction
3
4
Same function name is given to different function
Differ point:
The number of parameters
The data type of parameters
The order of appearance
Function Overloading
5
6
If derived class defines same function as defined in its base class, it
is known as function overriding in C++
used as static as well as dynamic polymorphism
Uses same method name and same type signature
 Used by a child class to change behavior it inherited from its parent
Function Overriding
7
Presence of Inheritence
Must have exactly the same declaration in both base and
derived class
Requirement for Overriding
8
Output :
Example:
9
Accessing Overridden Functions
10
• A type of dynamic polymorphism
• Uses pointer
• Compiler performs late binding/run time binding/dynamic
binding on this function
• Compiler gives preference to address in pointer
11
Virtual function
int main(){
Animal *a;
Cat c;
a = &c;
a -> sound();
return 0;
}
class Animal{
public:
virtual void sound(){
cout<<"Base sound"<<endl;
}
};
class Cat:public Animal{
public:
void sound(){
cout<<"Meow"<<endl;
}
};
• A class with pure virtual function is called an abstract class
• We cannot create an object of abstract class
• If a virtual function is equal to 0, it is a pure virtual function
• virtual void sound()=0; //pure virtual function
• It has no definition and also called as do nothing function
• Once made it must be used in derived classes (compulsory else error is
thrown)
12
Pure Virtual function
class Animal{
public:
virtual void sound()=0;
};
class Cat{
public:
void sound(){
cout<<"Meow"<<endl;
}
};
class Dog{
public:
void sound(){
cout<<"Woff"<<endl;
}
};
int main(){
Cat c;
Dog d;
c.sound();
d.sound();
return 0;
}
Object of class
Animal is not
created
• Similar to abstract class
• Object of this class cannot be created
• All of its function are virtual and does not have member variables
• Derived class must implement all the functions i.e., provide definition
• We may or may not inherit the base class
13
Interface class
class Animal{
public:
virtual void sound()=0;
virtual void food()=0;
};
class Cat{
public:
void sound(){
cout<<"Meow"<<endl;
}
void food(){
cout<<"Milk"<<endl;
}
};
class Dog{
public:
void sound(){
cout<<"Woff"<<endl;
}
void food(){
cout<<"Meat"<<endl;
}
};
int main(){
Cat c;
Dog d;
c.sound();
c.food();
d.sound();
d.food();
return 0;
}
Object of
interface class
Animal is not
created
Inheritance of
the base class
is not
necessary
References
• https://www.codesdope.com/cpp-virtual-and-abstract/
• https://www.youtube.com/watch?v=SvesRBYu65k
• https://www.youtube.com/watch?v=SF8HbxDbNr0&t=309s
• https://www.studytonight.com/cpp/function-overriding.php
• https://www.programiz.com/cpp-programming/function-overriding
• https://www.careercup.com/question?id=1874672
14
Queries
15

Polymorphism in C++

  • 1.
    POLYMORPHISM 1 Presented by: TheCamouflage Gagan Puri Bikram Bhurtel Rabin BK BSc.CSIT 3rd Semester
  • 2.
  • 3.
    Derived form theGreek word polymorphous Real life example:- a man at a same time is a father, a husband, a employee In C++ how polymorphism can be achieved Introduction 3
  • 4.
  • 5.
    Same function nameis given to different function Differ point: The number of parameters The data type of parameters The order of appearance Function Overloading 5
  • 6.
  • 7.
    If derived classdefines same function as defined in its base class, it is known as function overriding in C++ used as static as well as dynamic polymorphism Uses same method name and same type signature  Used by a child class to change behavior it inherited from its parent Function Overriding 7
  • 8.
    Presence of Inheritence Musthave exactly the same declaration in both base and derived class Requirement for Overriding 8
  • 9.
  • 10.
  • 11.
    • A typeof dynamic polymorphism • Uses pointer • Compiler performs late binding/run time binding/dynamic binding on this function • Compiler gives preference to address in pointer 11 Virtual function int main(){ Animal *a; Cat c; a = &c; a -> sound(); return 0; } class Animal{ public: virtual void sound(){ cout<<"Base sound"<<endl; } }; class Cat:public Animal{ public: void sound(){ cout<<"Meow"<<endl; } };
  • 12.
    • A classwith pure virtual function is called an abstract class • We cannot create an object of abstract class • If a virtual function is equal to 0, it is a pure virtual function • virtual void sound()=0; //pure virtual function • It has no definition and also called as do nothing function • Once made it must be used in derived classes (compulsory else error is thrown) 12 Pure Virtual function class Animal{ public: virtual void sound()=0; }; class Cat{ public: void sound(){ cout<<"Meow"<<endl; } }; class Dog{ public: void sound(){ cout<<"Woff"<<endl; } }; int main(){ Cat c; Dog d; c.sound(); d.sound(); return 0; } Object of class Animal is not created
  • 13.
    • Similar toabstract class • Object of this class cannot be created • All of its function are virtual and does not have member variables • Derived class must implement all the functions i.e., provide definition • We may or may not inherit the base class 13 Interface class class Animal{ public: virtual void sound()=0; virtual void food()=0; }; class Cat{ public: void sound(){ cout<<"Meow"<<endl; } void food(){ cout<<"Milk"<<endl; } }; class Dog{ public: void sound(){ cout<<"Woff"<<endl; } void food(){ cout<<"Meat"<<endl; } }; int main(){ Cat c; Dog d; c.sound(); c.food(); d.sound(); d.food(); return 0; } Object of interface class Animal is not created Inheritance of the base class is not necessary
  • 14.
    References • https://www.codesdope.com/cpp-virtual-and-abstract/ • https://www.youtube.com/watch?v=SvesRBYu65k •https://www.youtube.com/watch?v=SF8HbxDbNr0&t=309s • https://www.studytonight.com/cpp/function-overriding.php • https://www.programiz.com/cpp-programming/function-overriding • https://www.careercup.com/question?id=1874672 14
  • 15.