SlideShare ist ein Scribd-Unternehmen logo
1 von 21
POLYMORPHISM
PRINCE
BSc.IT
Dedicated to: Dr. A P J Abdul Kalam
Contents…
• What is Polymorphism?
• How many ways of implementation?
• Linking or Binding.
• Compile time Polymorphism.
• Runtime Polymorphism.
What is Polymorphism?
• In real world there are so many cases where
we see a single name is shared for
multiple jobs. This phenomena is termed as
POLYMORPHISM in OOPs.
How many ways of
implementation?
• OOP languages like C++, Java etc.
provide facility to implement
polymorphism.
• Let us start with C++_
Linking or Binding.
• To call a
function is
termed as
Linking or
Binding.
#include”iostream.h”
class demo
{
public:
void print()
{cout<<“ntHello ! PRINCE ”;}
};
void main()
{demo obj;
obj.print(); // linking fn print
}
Compile time
Polymorphism.
• Also known as Static Linking or Early binding.
• The compiler calls a function at the time of
compile.
• In C++ it is implemented as below_
• Overloading.
• Function
• Operator
• Templates.
Function Overloading
• More than one
functions have
same name with
different
signatures in the
same scope shows
overloading of
functions.
#include<iostream.h>
class demo
{
public:
void sum()
{
int x,y;
cout<<“Enter 2 no.: ”;cin>>x>>y;
cout<<“Sum: ”<<x+y;
}
void sum( int x, int y)
{
cout<<“Sum: ”<<x+y;
}
};
Function Overloading
• When two functions have same prototypes the
second is termed as redeclaration of the first.
• Two functions have same name and signature
with different return types, the second is
errorneous declaration.
• The signatures may be different in no. of
arguments or return types of arguments.
• Overloading of constructor is an example of
function overloading.
Function Overloading
• Constructor overloading:
#include<iostream.h>
class demo
{
int x;
float y;
public:
demo()
{ x=10; y=23.5; cout<<“constructor 1”;}
demo( int a, float b)
{ x=a; y=b; cout<<“constructor 2”; }
};
Operator Overloading
• The concept of defining functions of all the
existing operators on user defined types
(classes) is known as overloading of operators.
• Implemented_
• Through member functions.
• Through friend functions
Operator Overloading
• New operators are not defined .
• Precedence and associativity are never
changed.
• Followings are never overloaded_
• :: scope resolution operator
• sizeof() operator
• ?: conditional operator
• .* pointers
Operator Member fn Friend fn
Unary No argument 1 argument
Binary 1argument 2 arguments
Operator Overloading
• Implementation through Member function:
SYNTAX:
return type operator <op> (0 or 1argument)
{
// definition;
}
Operator Overloading
• Implementation through Friend function:
• First declare the prototype inside the class.
SYNTAX:
friend return type operator <op> (type of arguments);
• Definition outside the class
SYNTAX:
return type operator <op>(arguments)
{
//definition
}
Operator Overloading
• Example:
#include<iostream.h>
class complex
{
int x, y;
public:
void get()
{ cout<<“Enter x & y:”;
cin>>x>>y;
}
void show()
{if(y<0)
cout<<x<<“-”<<y<<“i”;
else
cout<<x<<“+”<<y<<“i”;
}
complex operator + (complex c)
{
complex t;
t.x=x+ c.x;
t.y=y+c.y;
return t;
}
};
void main()
{
complex c1, c2, c3;
c1.get();c2.get();
c1.show();c2.show();
c3=c1+c2;
c3.show();
}
Operator Overloading
• Example: by friend function
#include<iostream.h>
class complex
{
int x,y;
public:
void get()
{
cout<<“Enter x & y:”;
cin>>x>>y;
}
void show
{
cout<<x<<“+”<<y<<“i”;
}
friend complex operator +(complex, complex);
};
complex operator + (complex c1, complex c2)
{
complex s;
s.x=c1.x+c2.x;
s.y=c1.y+c2.y;
return s;
};
Void main()
{
complex ca, cb, cd;
ca.get();cb.get();
cd=ca+cb;
cout<<“S=”<<x<<“+”<<y<<“is”;}
Run time
Polymorphism.
• Also known as Dynamic Linking or Late
binding.
• The compiler calls a function at the run time.
• In C++ it is implemented as below_
• Virtual function
Virtual function
• In Inheritance the base class pointer is compatible with all the
objects of the derived class, but its reverse is not true.
• At the time of pointing a derived class object, the base class
pointer points only those member functions which are
declared as same as in the base class(prototype).
• It is determined at run time through VIRTUAL FUNCTION.
Virtual function
• A Virtual function is_
• Declared within the base class.
• May be redefined in the derived class.
• Neither static nor friend.
• Syntax:
virtual return type identifier ( args…);
Virtual function
• A Pure virtual function is_
• Only declared within the base class.
• The derived class must add its own definition.
• If a derived class failed to override the pure virtual function
the compile will generate error.
• Syntax:
virtual return type identifier(args…) = 0;
Virtual function
• A class containing at least one PVF is Abstract class.
• There is no object is declared of an Abstract class.
class demo
{
public:
virtual void print() = 0; //PVF
};
class derived : public demo
{
public:
void print()
{cout<<“HELLO! PRINCE ”;}
};
void main()
{
demo *p;
derived obj;
p = &obj;
p->print();
}
/* output
HELLO! PRINCE 
*/
THE END
Thanks for watching.
Mail me: krprince8888@gmail.com

Weitere ähnliche Inhalte

Was ist angesagt?

Object Oriented Programming Concepts
Object Oriented Programming ConceptsObject Oriented Programming Concepts
Object Oriented Programming Concepts
thinkphp
 

Was ist angesagt? (20)

Functions in C++
Functions in C++Functions in C++
Functions in C++
 
Functions in c++
Functions in c++Functions in c++
Functions in c++
 
Polymorphism and its types
Polymorphism and its typesPolymorphism and its types
Polymorphism and its types
 
Function overloading ppt
Function overloading pptFunction overloading ppt
Function overloading ppt
 
Keywords, identifiers ,datatypes in C++
Keywords, identifiers ,datatypes in C++Keywords, identifiers ,datatypes in C++
Keywords, identifiers ,datatypes in C++
 
Unary operator overloading
Unary operator overloadingUnary operator overloading
Unary operator overloading
 
Stack
StackStack
Stack
 
Operator Overloading
Operator OverloadingOperator Overloading
Operator Overloading
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
 
Pointer to function 1
Pointer to function 1Pointer to function 1
Pointer to function 1
 
Method overloading
Method overloadingMethod overloading
Method overloading
 
INLINE FUNCTION IN C++
INLINE FUNCTION IN C++INLINE FUNCTION IN C++
INLINE FUNCTION IN C++
 
Polymorphism in c++(ppt)
Polymorphism in c++(ppt)Polymorphism in c++(ppt)
Polymorphism in c++(ppt)
 
Operators in C++
Operators in C++Operators in C++
Operators in C++
 
classes and objects in C++
classes and objects in C++classes and objects in C++
classes and objects in C++
 
Constructors in C++
Constructors in C++Constructors in C++
Constructors in C++
 
virtual function
virtual functionvirtual function
virtual function
 
Object Oriented Programming Concepts
Object Oriented Programming ConceptsObject Oriented Programming Concepts
Object Oriented Programming Concepts
 
Polymorphism in Java by Animesh Sarkar
Polymorphism in Java by Animesh SarkarPolymorphism in Java by Animesh Sarkar
Polymorphism in Java by Animesh Sarkar
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
 

Ähnlich wie Polymorphism Using 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
CHAITALIUKE1
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
Amir Ali
 

Ähnlich wie Polymorphism Using C++ (20)

11.C++Polymorphism [Autosaved].pptx
11.C++Polymorphism [Autosaved].pptx11.C++Polymorphism [Autosaved].pptx
11.C++Polymorphism [Autosaved].pptx
 
Exciting JavaScript - Part I
Exciting JavaScript - Part IExciting JavaScript - Part I
Exciting JavaScript - Part I
 
polymorphism.pdf
polymorphism.pdfpolymorphism.pdf
polymorphism.pdf
 
2nd puc computer science chapter 8 function overloading
 2nd puc computer science chapter 8   function overloading 2nd puc computer science chapter 8   function overloading
2nd puc computer science chapter 8 function overloading
 
Virtual Function
Virtual FunctionVirtual Function
Virtual Function
 
Function different types of funtion
Function different types of funtionFunction different types of funtion
Function different types of funtion
 
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
 
Introduction to c first week slides
Introduction to c first week slidesIntroduction to c first week slides
Introduction to c first week slides
 
full defination of final opp.pptx
full defination of final opp.pptxfull defination of final opp.pptx
full defination of final opp.pptx
 
Java For Automation
Java   For AutomationJava   For Automation
Java For Automation
 
Louis Loizides iOS Programming Introduction
Louis Loizides iOS Programming IntroductionLouis Loizides iOS Programming Introduction
Louis Loizides iOS Programming Introduction
 
iOS Programming Intro
iOS Programming IntroiOS Programming Intro
iOS Programming Intro
 
Java For beginners and CSIT and IT students
Java  For beginners and CSIT and IT studentsJava  For beginners and CSIT and IT students
Java For beginners and CSIT and IT students
 
OOPS & C++(UNIT 4)
OOPS & C++(UNIT 4)OOPS & C++(UNIT 4)
OOPS & C++(UNIT 4)
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
 
Functions in c++
Functions in c++Functions in c++
Functions in c++
 
Example for Virtual and Pure Virtual function.pdf
Example for Virtual and Pure Virtual function.pdfExample for Virtual and Pure Virtual function.pdf
Example for Virtual and Pure Virtual function.pdf
 
object oriented programming language.pptx
object oriented programming language.pptxobject oriented programming language.pptx
object oriented programming language.pptx
 
Polymorphism 140527082302-phpapp01
Polymorphism 140527082302-phpapp01Polymorphism 140527082302-phpapp01
Polymorphism 140527082302-phpapp01
 
cbybalaguruswami-e-180803051831.pptx
cbybalaguruswami-e-180803051831.pptxcbybalaguruswami-e-180803051831.pptx
cbybalaguruswami-e-180803051831.pptx
 

Mehr von PRINCE KUMAR

Mehr von PRINCE KUMAR (11)

RIP vs OSPF
RIP vs OSPFRIP vs OSPF
RIP vs OSPF
 
Graphs
GraphsGraphs
Graphs
 
Fddi
FddiFddi
Fddi
 
Binary search tree
Binary search treeBinary search tree
Binary search tree
 
basics of php
 basics of php basics of php
basics of php
 
php
phpphp
php
 
Connectivity devices
Connectivity devicesConnectivity devices
Connectivity devices
 
Tcp ip tutorial
Tcp ip tutorialTcp ip tutorial
Tcp ip tutorial
 
OSI layers
OSI layersOSI layers
OSI layers
 
NETWORKS & TOPOLOGY
NETWORKS & TOPOLOGYNETWORKS & TOPOLOGY
NETWORKS & TOPOLOGY
 
BUILDING WEBSITES ON WORDPRESS
BUILDING WEBSITES ON WORDPRESSBUILDING WEBSITES ON WORDPRESS
BUILDING WEBSITES ON WORDPRESS
 

Kürzlich hochgeladen

Kürzlich hochgeladen (20)

Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxCOMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
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
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
 
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptxExploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the Classroom
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptx
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
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Ữ Â...
 
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxOn_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 

Polymorphism Using C++

  • 2. Contents… • What is Polymorphism? • How many ways of implementation? • Linking or Binding. • Compile time Polymorphism. • Runtime Polymorphism.
  • 3. What is Polymorphism? • In real world there are so many cases where we see a single name is shared for multiple jobs. This phenomena is termed as POLYMORPHISM in OOPs.
  • 4. How many ways of implementation? • OOP languages like C++, Java etc. provide facility to implement polymorphism. • Let us start with C++_
  • 5. Linking or Binding. • To call a function is termed as Linking or Binding. #include”iostream.h” class demo { public: void print() {cout<<“ntHello ! PRINCE ”;} }; void main() {demo obj; obj.print(); // linking fn print }
  • 6. Compile time Polymorphism. • Also known as Static Linking or Early binding. • The compiler calls a function at the time of compile. • In C++ it is implemented as below_ • Overloading. • Function • Operator • Templates.
  • 7. Function Overloading • More than one functions have same name with different signatures in the same scope shows overloading of functions. #include<iostream.h> class demo { public: void sum() { int x,y; cout<<“Enter 2 no.: ”;cin>>x>>y; cout<<“Sum: ”<<x+y; } void sum( int x, int y) { cout<<“Sum: ”<<x+y; } };
  • 8. Function Overloading • When two functions have same prototypes the second is termed as redeclaration of the first. • Two functions have same name and signature with different return types, the second is errorneous declaration. • The signatures may be different in no. of arguments or return types of arguments. • Overloading of constructor is an example of function overloading.
  • 9. Function Overloading • Constructor overloading: #include<iostream.h> class demo { int x; float y; public: demo() { x=10; y=23.5; cout<<“constructor 1”;} demo( int a, float b) { x=a; y=b; cout<<“constructor 2”; } };
  • 10. Operator Overloading • The concept of defining functions of all the existing operators on user defined types (classes) is known as overloading of operators. • Implemented_ • Through member functions. • Through friend functions
  • 11. Operator Overloading • New operators are not defined . • Precedence and associativity are never changed. • Followings are never overloaded_ • :: scope resolution operator • sizeof() operator • ?: conditional operator • .* pointers Operator Member fn Friend fn Unary No argument 1 argument Binary 1argument 2 arguments
  • 12. Operator Overloading • Implementation through Member function: SYNTAX: return type operator <op> (0 or 1argument) { // definition; }
  • 13. Operator Overloading • Implementation through Friend function: • First declare the prototype inside the class. SYNTAX: friend return type operator <op> (type of arguments); • Definition outside the class SYNTAX: return type operator <op>(arguments) { //definition }
  • 14. Operator Overloading • Example: #include<iostream.h> class complex { int x, y; public: void get() { cout<<“Enter x & y:”; cin>>x>>y; } void show() {if(y<0) cout<<x<<“-”<<y<<“i”; else cout<<x<<“+”<<y<<“i”; } complex operator + (complex c) { complex t; t.x=x+ c.x; t.y=y+c.y; return t; } }; void main() { complex c1, c2, c3; c1.get();c2.get(); c1.show();c2.show(); c3=c1+c2; c3.show(); }
  • 15. Operator Overloading • Example: by friend function #include<iostream.h> class complex { int x,y; public: void get() { cout<<“Enter x & y:”; cin>>x>>y; } void show { cout<<x<<“+”<<y<<“i”; } friend complex operator +(complex, complex); }; complex operator + (complex c1, complex c2) { complex s; s.x=c1.x+c2.x; s.y=c1.y+c2.y; return s; }; Void main() { complex ca, cb, cd; ca.get();cb.get(); cd=ca+cb; cout<<“S=”<<x<<“+”<<y<<“is”;}
  • 16. Run time Polymorphism. • Also known as Dynamic Linking or Late binding. • The compiler calls a function at the run time. • In C++ it is implemented as below_ • Virtual function
  • 17. Virtual function • In Inheritance the base class pointer is compatible with all the objects of the derived class, but its reverse is not true. • At the time of pointing a derived class object, the base class pointer points only those member functions which are declared as same as in the base class(prototype). • It is determined at run time through VIRTUAL FUNCTION.
  • 18. Virtual function • A Virtual function is_ • Declared within the base class. • May be redefined in the derived class. • Neither static nor friend. • Syntax: virtual return type identifier ( args…);
  • 19. Virtual function • A Pure virtual function is_ • Only declared within the base class. • The derived class must add its own definition. • If a derived class failed to override the pure virtual function the compile will generate error. • Syntax: virtual return type identifier(args…) = 0;
  • 20. Virtual function • A class containing at least one PVF is Abstract class. • There is no object is declared of an Abstract class. class demo { public: virtual void print() = 0; //PVF }; class derived : public demo { public: void print() {cout<<“HELLO! PRINCE ”;} }; void main() { demo *p; derived obj; p = &obj; p->print(); } /* output HELLO! PRINCE  */
  • 21. THE END Thanks for watching. Mail me: krprince8888@gmail.com