Oopsecondgrouppresentation 180726073512-converted (1)

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
1 sur 15

Recommandé

Polymorphism in C++Polymorphism in C++
Polymorphism in C++Rabin BK
519 vues15 diapositives
polymorphism pptpolymorphism ppt
polymorphism pptsheetal singh
1.5K vues15 diapositives
PolymorphismPolymorphism
PolymorphismNochiketa Chakraborty
7.5K vues13 diapositives

Contenu connexe

Tendances(20)

Virtual functionVirtual function
Virtual function
Burhan Ahmed565 vues
virtual functionvirtual function
virtual function
VENNILAV6506 vues
PolymorphismPolymorphism
Polymorphism
Prof .Pragati Khade 47 vues
PolymorphismPolymorphism
Polymorphism
Arif Ansari1.8K vues
Java Building BlocksJava Building Blocks
Java Building Blocks
Cate Huston5.3K vues
Java(Polymorphism)Java(Polymorphism)
Java(Polymorphism)
harsh kothari7.3K vues
Scala the-good-partsScala the-good-parts
Scala the-good-parts
Fuqiang Wang1.9K vues
PolymorphismPolymorphism
Polymorphism
Kumar 1.8K vues
Intro to functional programmingIntro to functional programming
Intro to functional programming
Assaf Gannon544 vues
Xtext WebinarXtext Webinar
Xtext Webinar
Heiko Behrens1.9K vues
Functions in PythonFunctions in Python
Functions in Python
Kamal Acharya3.2K vues
JAVA PolymorphismJAVA Polymorphism
JAVA Polymorphism
Mahi Mca1.6K vues
Kotlin's Interoperability with Java Kotlin's Interoperability with Java
Kotlin's Interoperability with Java
Big Nerd Ranch103 vues
Thinking Like a ProgrammerThinking Like a Programmer
Thinking Like a Programmer
Cate Huston1.3K vues
9054799 dzone-refcard267-kotlin9054799 dzone-refcard267-kotlin
9054799 dzone-refcard267-kotlin
Zoran Stanimirovic150 vues
Functions in PythonFunctions in Python
Functions in Python
Shakti Singh Rathore452 vues

Similaire à Oopsecondgrouppresentation 180726073512-converted (1)

polymorphism.pdfpolymorphism.pdf
polymorphism.pdfriyawagh2
19 vues21 diapositives
Polymorphism Using C++Polymorphism Using C++
Polymorphism Using C++PRINCE KUMAR
508 vues21 diapositives

Similaire à Oopsecondgrouppresentation 180726073512-converted (1)(20)

polymorphism.pdfpolymorphism.pdf
polymorphism.pdf
riyawagh219 vues
Polymorphism Using C++Polymorphism Using C++
Polymorphism Using C++
PRINCE KUMAR508 vues
OOPS & C++(UNIT 4)OOPS & C++(UNIT 4)
OOPS & C++(UNIT 4)
SURBHI SAROHA11 vues
Java   For AutomationJava   For Automation
Java For Automation
Abhijeet Dubey87 vues
Lecture6.pptLecture6.ppt
Lecture6.ppt
ammu2417546 vues
AjaxworldAjaxworld
Ajaxworld
deannalagason546 vues
Lecture-10_PHP-OOP.pptxLecture-10_PHP-OOP.pptx
Lecture-10_PHP-OOP.pptx
ShaownRoy15 vues
Polymorphism pptPolymorphism ppt
Polymorphism ppt
arunsingh660803 vues
Polymorphism & inheritence pptPolymorphism & inheritence ppt
Polymorphism & inheritence ppt
arunsingh660119 vues
Kotlin introKotlin intro
Kotlin intro
Elifarley Cruz610 vues
C++ Interview QuestionsC++ Interview Questions
C++ Interview Questions
Kaushik Raghupathi7.4K vues
PolymorphismuploadPolymorphismupload
Polymorphismupload
Mukhtar_Hunzai1.2K vues
polymorphism OOP.pptxpolymorphism OOP.pptx
polymorphism OOP.pptx
AssadLeo19 vues

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