Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
SlideShare a Scribd company logo
POLYMORPHISM
Presented by: The Camouflage
Gagan Puri
Bikram Bhurtel
Rabin BK
BSc.CSIT3rd 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
3
Introduction
4
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
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
7
Function Overriding
Presence of Inheritence
Must have exactly the same declaration in both base and
derived class
8
Requirement for Overriding
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
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
• 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
14
• 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
Queries
15

More Related Content

Oopsecondgrouppresentation 180726073512-converted (1)

  • 1. POLYMORPHISM Presented by: The Camouflage Gagan Puri Bikram Bhurtel Rabin BK BSc.CSIT3rd Semester
  • 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
  • 4. 4
  • 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
  • 6. 6
  • 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
  • 14. References 14 • 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