SlideShare ist ein Scribd-Unternehmen logo
1 von 12
POLYMORPHISM IN C++
Haldia Institute of Technology
Presented By : -
Name Roll no.
Purabi Biswas 14/CS/70
Sanjit Shaw B14/CS/127
Shubham Singhanaia B14/CS/127
(Computer Science & Engineering)
POLYMORPHISM
The process of representing one Form in multiple forms is
known as Polymorphism. Here one form represent original
form or original method always resides in base class and
multiple forms represents overridden method which resides in
derived classes.
Polymorphism is derived from 2 Greek words: poly and
morphs. The word "poly" means many and morphs means
forms.
Real life example of Polymorphism
Suppose if you are in class room that time you behave
like a student, when you are in market at that time you
behave like a customer, when you at your home at that
time you behave like a son or daughter, Here one person
have different-different behaviors.
Type of Polymorphism
 Static polymorphism is also known as early binding and compile-time polymorphism.
In static polymorphism memory will be allocated at compile-time.
 Dynamic polymorphism is also known as late binding and run-time polymorphism. In
dynamic polymorphism memory will be allocated at run-time.
Polymorphism
Static
Function
Overloading
Operator
Overloading
Dynamic
Virtual
Functions
Method Overloading
Whenever same method name is exiting multiple times in the same class with
different number of parameter or different order of parameters or different types of
parameters is known as method overloading.
In next example method "sum()" is present in Addition class with same name but
with different signature or arguments.
Function Overloading Example
class Addition {
public: void sum(int a, int b) {
cout<<"a+b :"<<a+b; } //output :- a+b : 30
void sum(int a, int b, int c) {
cout<<"a+b+c :"<<a+b+c; } //output :- a+b+c : 60
};
int main() {
Addition obj;
obj.sum(10, 20);
cout<<endl;
obj.sum(10, 20, 30); }
Operator Overloading
The process of making an operator to exhibit different behaviors in different
instances is known as operator overloading.
Only predefined operator can be overloaded.
Types Of Operator Overloading
Unary operator overloading.
These Operators have only single operand.
Examples:- ++,--,~,!
Binary operator overloading.
These operators can have two or more operands.
Examples:-+,-,*,/,%,^,=,==,+=,&,&& etc
Operator Overloading Example
class complex{ int main(){
private:: complex c1,c2,c3;
int a,b; c1.set_data(3,4);
public:void set_data(int x,int y){ c2.set_data(5,6);
a=x;b=y;} c3=c1.add+(c2);
void show_data(){ c3.show_data();
cout<<“n a=“<<a<<“b=“<<b;} return 0;}
complex add+(complex c){
complex temp ;
temp.a=a+c.a;
temp.b=b+c.b;
return temp;}};
Virtual Function
A virtual function is a member function that is declared as virtual within a base
class and redefined by a derived class.
 To create virtual function, precede the base version of function’s declaration with
the keyword virtual.
 Here we use a pointer to the base class to refer to all the derived objects.
 The method name and type signature should be same for both base and derived
version of function.
Using Virtual Keyword Example
class A {
public: virtual void show() {
cout<<"Content of base class.n"; }};
class B : public A {
public: void show() {
cout<<"Content of derived class.n"; } };
int main() {
A b,*bptr; //Base class pointer
B d; //Derived class object
bptr = &b;
bptr->show(); //Late Binding Occurs
Bptr=&d;
Bptr->show();
return 0; }
Special Thanks To:-
Mrs. Rajrupa Metia
(Asst. Professor)
Computer Science & Engineering

Weitere Àhnliche Inhalte

Was ist angesagt?

class and objects
class and objectsclass and objects
class and objects
Payel Guria
 
Virtual base class
Virtual base classVirtual base class
Virtual base class
Tech_MX
 
Templates in C++
Templates in C++Templates in C++
Templates in C++
Tech_MX
 
Classes, objects in JAVA
Classes, objects in JAVAClasses, objects in JAVA
Classes, objects in JAVA
Abhilash Nair
 
Operators in C++
Operators in C++Operators in C++
Operators in C++
Sachin Sharma
 

Was ist angesagt? (20)

class and objects
class and objectsclass and objects
class and objects
 
Java Exception handling
Java Exception handlingJava Exception handling
Java Exception handling
 
RECURSION IN C
RECURSION IN C RECURSION IN C
RECURSION IN C
 
Virtual base class
Virtual base classVirtual base class
Virtual base class
 
friend function(c++)
friend function(c++)friend function(c++)
friend function(c++)
 
Templates in C++
Templates in C++Templates in C++
Templates in C++
 
Constructor and Types of Constructors
Constructor and Types of ConstructorsConstructor and Types of Constructors
Constructor and Types of Constructors
 
Programming in c Arrays
Programming in c ArraysProgramming in c Arrays
Programming in c Arrays
 
Presentation on C++ Programming Language
Presentation on C++ Programming LanguagePresentation on C++ Programming Language
Presentation on C++ Programming Language
 
Classes, objects in JAVA
Classes, objects in JAVAClasses, objects in JAVA
Classes, objects in JAVA
 
oops concept in java | object oriented programming in java
oops concept in java | object oriented programming in javaoops concept in java | object oriented programming in java
oops concept in java | object oriented programming in java
 
Java(Polymorphism)
Java(Polymorphism)Java(Polymorphism)
Java(Polymorphism)
 
Object Oriented Programming Using C++
Object Oriented Programming Using C++Object Oriented Programming Using C++
Object Oriented Programming Using C++
 
Control statements
Control statementsControl statements
Control statements
 
C by balaguruswami - e.balagurusamy
C   by balaguruswami - e.balagurusamyC   by balaguruswami - e.balagurusamy
C by balaguruswami - e.balagurusamy
 
Operator.ppt
Operator.pptOperator.ppt
Operator.ppt
 
Files and streams
Files and streamsFiles and streams
Files and streams
 
Loops in C Programming Language
Loops in C Programming LanguageLoops in C Programming Language
Loops in C Programming Language
 
Operators in C++
Operators in C++Operators in C++
Operators in C++
 
File handling in c
File handling in cFile handling in c
File handling in c
 

Andere mochten auch

Polymorphism
PolymorphismPolymorphism
Polymorphism
Kumar Gaurav
 
Seminar on polymorphism
Seminar on polymorphismSeminar on polymorphism
Seminar on polymorphism
023henil
 
A survey of distributed deadlock detection algorithms
A survey of distributed deadlock detection algorithmsA survey of distributed deadlock detection algorithms
A survey of distributed deadlock detection algorithms
anaykh1992
 
C++ polymorphism
C++ polymorphismC++ polymorphism
C++ polymorphism
Ganesh Hogade
 
Business plan: social networking website
Business plan: social networking website Business plan: social networking website
Business plan: social networking website
Dr. Trilok Kumar Jain
 

Andere mochten auch (20)

Polymorphism in c++ ppt (Powerpoint) | Polymorphism in c++ with example ppt |...
Polymorphism in c++ ppt (Powerpoint) | Polymorphism in c++ with example ppt |...Polymorphism in c++ ppt (Powerpoint) | Polymorphism in c++ with example ppt |...
Polymorphism in c++ ppt (Powerpoint) | Polymorphism in c++ with example ppt |...
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
 
Static and Dynamic polymorphism in C++
Static and Dynamic polymorphism in C++Static and Dynamic polymorphism in C++
Static and Dynamic polymorphism in C++
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
 
Dynamic Polymorphism in C++
Dynamic Polymorphism in C++Dynamic Polymorphism in C++
Dynamic Polymorphism in C++
 
polymorphism
polymorphism polymorphism
polymorphism
 
Seminar on polymorphism
Seminar on polymorphismSeminar on polymorphism
Seminar on polymorphism
 
A survey of distributed deadlock detection algorithms
A survey of distributed deadlock detection algorithmsA survey of distributed deadlock detection algorithms
A survey of distributed deadlock detection algorithms
 
[OOP - Lec 07] Access Specifiers
[OOP - Lec 07] Access Specifiers[OOP - Lec 07] Access Specifiers
[OOP - Lec 07] Access Specifiers
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
 
Chapter 4
Chapter 4Chapter 4
Chapter 4
 
C++ polymorphism
C++ polymorphismC++ polymorphism
C++ polymorphism
 
E wallet
E walletE wallet
E wallet
 
Business plan: social networking website
Business plan: social networking website Business plan: social networking website
Business plan: social networking website
 
E wallet- final
E wallet- finalE wallet- final
E wallet- final
 
Slide 2 data models
Slide 2 data modelsSlide 2 data models
Slide 2 data models
 
Inheritance in C++
Inheritance in C++Inheritance in C++
Inheritance in C++
 
E wallet by amin
E wallet by aminE wallet by amin
E wallet by amin
 
Underwater wireless communication
Underwater wireless communicationUnderwater wireless communication
Underwater wireless communication
 
E wallet
E walletE wallet
E wallet
 

Ähnlich wie Polymorphism in c++(ppt)

Comparison between runtime polymorphism and compile time polymorphism
Comparison between runtime polymorphism and compile time polymorphismComparison between runtime polymorphism and compile time polymorphism
Comparison between runtime polymorphism and compile time polymorphism
CHAITALIUKE1
 
polymorphism ppt
polymorphism pptpolymorphism ppt
polymorphism ppt
sheetal singh
 
Oopsecondgrouppresentation 180726073512-converted (1)
Oopsecondgrouppresentation 180726073512-converted (1)Oopsecondgrouppresentation 180726073512-converted (1)
Oopsecondgrouppresentation 180726073512-converted (1)
Hassan Hashmi
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
Arif Ansari
 

Ähnlich wie Polymorphism in c++(ppt) (20)

vdocument in_polymorphism-in-cppt_python.pdf
vdocument in_polymorphism-in-cppt_python.pdfvdocument in_polymorphism-in-cppt_python.pdf
vdocument in_polymorphism-in-cppt_python.pdf
 
Polymorphism In c++
Polymorphism In c++Polymorphism In c++
Polymorphism In c++
 
Comparison between runtime polymorphism and compile time polymorphism
Comparison between runtime polymorphism and compile time polymorphismComparison between runtime polymorphism and compile time polymorphism
Comparison between runtime polymorphism and compile time polymorphism
 
Java Polymorphism
Java PolymorphismJava Polymorphism
Java Polymorphism
 
oops.pptx
oops.pptxoops.pptx
oops.pptx
 
Polymorphism ppt
Polymorphism pptPolymorphism ppt
Polymorphism ppt
 
Polymorphism & inheritence ppt
Polymorphism & inheritence pptPolymorphism & inheritence ppt
Polymorphism & inheritence ppt
 
polymorphism.pdf
polymorphism.pdfpolymorphism.pdf
polymorphism.pdf
 
Oop concepts
Oop conceptsOop concepts
Oop concepts
 
Presentation on polymorphism in c++.pptx
Presentation on polymorphism in c++.pptxPresentation on polymorphism in c++.pptx
Presentation on polymorphism in c++.pptx
 
java poly ppt.pptx
java poly ppt.pptxjava poly ppt.pptx
java poly ppt.pptx
 
Polymorphism in c++
Polymorphism in c++Polymorphism in c++
Polymorphism in c++
 
Polymorphism and its types
Polymorphism and its typesPolymorphism and its types
Polymorphism and its types
 
polymorphism ppt
polymorphism pptpolymorphism ppt
polymorphism ppt
 
What Swift can teach us all
What Swift can teach us allWhat Swift can teach us all
What Swift can teach us all
 
Oopsecondgrouppresentation 180726073512-converted (1)
Oopsecondgrouppresentation 180726073512-converted (1)Oopsecondgrouppresentation 180726073512-converted (1)
Oopsecondgrouppresentation 180726073512-converted (1)
 
Minimal Introduction to C++ - Part III - Final
Minimal Introduction to C++ - Part III - FinalMinimal Introduction to C++ - Part III - Final
Minimal Introduction to C++ - Part III - Final
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
 
2CPP10 - Polymorphism
2CPP10 - Polymorphism2CPP10 - Polymorphism
2CPP10 - Polymorphism
 
Object Oriented Programming - 7.2. Polymorphism
Object Oriented Programming - 7.2. PolymorphismObject Oriented Programming - 7.2. Polymorphism
Object Oriented Programming - 7.2. Polymorphism
 

KĂŒrzlich hochgeladen

1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
QucHHunhnh
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
kauryashika82
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
ZurliaSoop
 

KĂŒrzlich hochgeladen (20)

SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
TỔNG ÔN TáșŹP THI VÀO LỚP 10 MÔN TIáșŸNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGở Â...
TỔNG ÔN TáșŹP THI VÀO LỚP 10 MÔN TIáșŸNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGở Â...TỔNG ÔN TáșŹP THI VÀO LỚP 10 MÔN TIáșŸNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGở Â...
TỔNG ÔN TáșŹP THI VÀO LỚP 10 MÔN TIáșŸNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGở Â...
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docx
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 

Polymorphism in c++(ppt)

  • 2. Haldia Institute of Technology Presented By : - Name Roll no. Purabi Biswas 14/CS/70 Sanjit Shaw B14/CS/127 Shubham Singhanaia B14/CS/127 (Computer Science & Engineering)
  • 3. POLYMORPHISM The process of representing one Form in multiple forms is known as Polymorphism. Here one form represent original form or original method always resides in base class and multiple forms represents overridden method which resides in derived classes. Polymorphism is derived from 2 Greek words: poly and morphs. The word "poly" means many and morphs means forms.
  • 4. Real life example of Polymorphism Suppose if you are in class room that time you behave like a student, when you are in market at that time you behave like a customer, when you at your home at that time you behave like a son or daughter, Here one person have different-different behaviors.
  • 5. Type of Polymorphism  Static polymorphism is also known as early binding and compile-time polymorphism. In static polymorphism memory will be allocated at compile-time.  Dynamic polymorphism is also known as late binding and run-time polymorphism. In dynamic polymorphism memory will be allocated at run-time. Polymorphism Static Function Overloading Operator Overloading Dynamic Virtual Functions
  • 6. Method Overloading Whenever same method name is exiting multiple times in the same class with different number of parameter or different order of parameters or different types of parameters is known as method overloading. In next example method "sum()" is present in Addition class with same name but with different signature or arguments.
  • 7. Function Overloading Example class Addition { public: void sum(int a, int b) { cout<<"a+b :"<<a+b; } //output :- a+b : 30 void sum(int a, int b, int c) { cout<<"a+b+c :"<<a+b+c; } //output :- a+b+c : 60 }; int main() { Addition obj; obj.sum(10, 20); cout<<endl; obj.sum(10, 20, 30); }
  • 8. Operator Overloading The process of making an operator to exhibit different behaviors in different instances is known as operator overloading. Only predefined operator can be overloaded. Types Of Operator Overloading Unary operator overloading. These Operators have only single operand. Examples:- ++,--,~,! Binary operator overloading. These operators can have two or more operands. Examples:-+,-,*,/,%,^,=,==,+=,&,&& etc
  • 9. Operator Overloading Example class complex{ int main(){ private:: complex c1,c2,c3; int a,b; c1.set_data(3,4); public:void set_data(int x,int y){ c2.set_data(5,6); a=x;b=y;} c3=c1.add+(c2); void show_data(){ c3.show_data(); cout<<“n a=“<<a<<“b=“<<b;} return 0;} complex add+(complex c){ complex temp ; temp.a=a+c.a; temp.b=b+c.b; return temp;}};
  • 10. Virtual Function A virtual function is a member function that is declared as virtual within a base class and redefined by a derived class.  To create virtual function, precede the base version of function’s declaration with the keyword virtual.  Here we use a pointer to the base class to refer to all the derived objects.  The method name and type signature should be same for both base and derived version of function.
  • 11. Using Virtual Keyword Example class A { public: virtual void show() { cout<<"Content of base class.n"; }}; class B : public A { public: void show() { cout<<"Content of derived class.n"; } }; int main() { A b,*bptr; //Base class pointer B d; //Derived class object bptr = &b; bptr->show(); //Late Binding Occurs Bptr=&d; Bptr->show(); return 0; }
  • 12. Special Thanks To:- Mrs. Rajrupa Metia (Asst. Professor) Computer Science & Engineering