You are on page 1of 15

POLYMORPHISM

Introduction
Static polymorphism
Overloading
Overriding
Dynamic polymorphism
Virtual function and binding
References

2
Introduction
 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

3
4
Function Overloading
 Same function name is given to different function
 Differ point:
 The number of parameters
 The data type of parameters
 The order of appearance

5
6
Function Overriding
 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

7
Requirement for Overriding
 Presence of Inheritence
 Must have exactly the same declaration in both base and
derived class

8
Example:

Output :

9
Accessing Overridden Functions

10
Virtual function
• A type of dynamic polymorphism
• Uses pointer
• Compiler performs late binding/run time binding/dynamic
binding on this function
class Animal{ class Cat:public Animal{ int main(){
public: public: Animal *a;
virtual void sound(){ void sound(){ Cat c;
cout<<"Base sound"<<endl; cout<<"Meow"<<endl; a = &c;
} } a -> sound();
}; }; return 0;
}

• Compiler gives preference to address in pointer

11
Pure Virtual function
• 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)
class Animal{ class Dog{ int main(){
public: public: Cat c; Object of class
virtual void sound()=0; void sound(){ Dog d; Animal is not
}; cout<<"Woff"<<endl; c.sound(); created
class Cat{ } d.sound();
public: }; return 0;
void sound(){ }
cout<<"Meow"<<endl;
}
12
};
Interface class
• 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
class Animal{ class Dog{ int main(){
public: public: Cat c; Object of
virtual void sound()=0; void sound(){ Dog d; interface class
virtual void food()=0; cout<<"Woff"<<endl; c.sound(); Animal is not
}; } c.food(); created
class Cat{ void food(){ d.sound();
public: cout<<"Meat"<<endl; d.food();
void sound(){ } return 0;
cout<<"Meow"<<endl; }; }
}
void food(){ Inheritance of
cout<<"Milk"<<endl; the base class
} is not
necessary 13
};
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

You might also like