This document discusses polymorphism in C++. It defines polymorphism as the ability of an object to take on many forms. It describes static polymorphism through function overloading and overriding. Dynamic polymorphism is achieved through virtual functions and runtime binding using pointers to base class objects. Pure virtual functions define abstract classes that cannot be instantiated, while interface classes provide a common interface without inheriting behavior.
3. 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
Introduction
5. Same function name is given to different function
Differ point:
The number of parameters
The data type of parameters
The order of appearance
5
Function Overloading
7. 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
Function Overriding
8. Presence of Inheritence
Must have exactly the same declaration in both base and
derived class
8
Requirement for Overriding
11. • 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
Virtual function
int main(){
Animal *a;
Cat c;
a = &c;
a -> sound();
return 0;
}
rtual void
class Animal{
public:
vi sound(){
cout<<"Base sound"<<endl;
}
};
class Cat:public Animal{
public:
void sound(){
cout<<"Meow"<<endl;
}
};
11
12. • 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
13. • 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