SlideShare a Scribd company logo
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
Ad

Recommended

C++ concept of Polymorphism
C++ concept of Polymorphism
kiran Patel
 
Polymorphism in C++
Polymorphism in C++
Rabin BK
 
Oops concepts || Object Oriented Programming Concepts in Java
Oops concepts || Object Oriented Programming Concepts in Java
Madishetty Prathibha
 
polymorphism
polymorphism
Imtiaz Hussain
 
Polymorphism In Java
Polymorphism In Java
Spotle.ai
 
Java basics and java variables
Java basics and java variables
Pushpendra Tyagi
 
Wrapper class
Wrapper class
kamal kotecha
 
Storage class in c
Storage class in c
kash95
 
String and string buffer
String and string buffer
kamal kotecha
 
Polymorphism
Polymorphism
Arif Ansari
 
Java(Polymorphism)
Java(Polymorphism)
harsh kothari
 
Core java concepts
Core java concepts
Ram132
 
Introduction to class in java
Introduction to class in java
kamal kotecha
 
Java String Handling
Java String Handling
Infoviaan Technologies
 
Java if else condition - powerpoint persentation
Java if else condition - powerpoint persentation
Maneesha Caldera
 
Pure virtual function and abstract class
Pure virtual function and abstract class
Amit Trivedi
 
C++ OOPS Concept
C++ OOPS Concept
Boopathi K
 
Super Keyword in Java.pptx
Super Keyword in Java.pptx
KrutikaWankhade1
 
Advance OOP concepts in Python
Advance OOP concepts in Python
Sujith Kumar
 
Inheritance in java
Inheritance in java
yash jain
 
Core Java Tutorials by Mahika Tutorials
Core Java Tutorials by Mahika Tutorials
Mahika Tutorials
 
07. Virtual Functions
07. Virtual Functions
Haresh Jaiswal
 
Object-oriented Programming in Python
Object-oriented Programming in Python
Juan-Manuel Gimeno
 
Methods in Java
Methods in Java
Jussi Pohjolainen
 
Inheritance in oops
Inheritance in oops
Hirra Sultan
 
Typecasting in c
Typecasting in c
Tushar Shende
 
OOP Assignment 03.pdf
OOP Assignment 03.pdf
ARSLANMEHMOOD47
 
Static Data Members and Member Functions
Static Data Members and Member Functions
MOHIT AGARWAL
 
11.C++Polymorphism [Autosaved].pptx
11.C++Polymorphism [Autosaved].pptx
AtharvPotdar2
 
Exciting JavaScript - Part I
Exciting JavaScript - Part I
Eugene Lazutkin
 

More Related Content

What's hot (20)

String and string buffer
String and string buffer
kamal kotecha
 
Polymorphism
Polymorphism
Arif Ansari
 
Java(Polymorphism)
Java(Polymorphism)
harsh kothari
 
Core java concepts
Core java concepts
Ram132
 
Introduction to class in java
Introduction to class in java
kamal kotecha
 
Java String Handling
Java String Handling
Infoviaan Technologies
 
Java if else condition - powerpoint persentation
Java if else condition - powerpoint persentation
Maneesha Caldera
 
Pure virtual function and abstract class
Pure virtual function and abstract class
Amit Trivedi
 
C++ OOPS Concept
C++ OOPS Concept
Boopathi K
 
Super Keyword in Java.pptx
Super Keyword in Java.pptx
KrutikaWankhade1
 
Advance OOP concepts in Python
Advance OOP concepts in Python
Sujith Kumar
 
Inheritance in java
Inheritance in java
yash jain
 
Core Java Tutorials by Mahika Tutorials
Core Java Tutorials by Mahika Tutorials
Mahika Tutorials
 
07. Virtual Functions
07. Virtual Functions
Haresh Jaiswal
 
Object-oriented Programming in Python
Object-oriented Programming in Python
Juan-Manuel Gimeno
 
Methods in Java
Methods in Java
Jussi Pohjolainen
 
Inheritance in oops
Inheritance in oops
Hirra Sultan
 
Typecasting in c
Typecasting in c
Tushar Shende
 
OOP Assignment 03.pdf
OOP Assignment 03.pdf
ARSLANMEHMOOD47
 
Static Data Members and Member Functions
Static Data Members and Member Functions
MOHIT AGARWAL
 
String and string buffer
String and string buffer
kamal kotecha
 
Core java concepts
Core java concepts
Ram132
 
Introduction to class in java
Introduction to class in java
kamal kotecha
 
Java if else condition - powerpoint persentation
Java if else condition - powerpoint persentation
Maneesha Caldera
 
Pure virtual function and abstract class
Pure virtual function and abstract class
Amit Trivedi
 
C++ OOPS Concept
C++ OOPS Concept
Boopathi K
 
Super Keyword in Java.pptx
Super Keyword in Java.pptx
KrutikaWankhade1
 
Advance OOP concepts in Python
Advance OOP concepts in Python
Sujith Kumar
 
Inheritance in java
Inheritance in java
yash jain
 
Core Java Tutorials by Mahika Tutorials
Core Java Tutorials by Mahika Tutorials
Mahika Tutorials
 
Object-oriented Programming in Python
Object-oriented Programming in Python
Juan-Manuel Gimeno
 
Inheritance in oops
Inheritance in oops
Hirra Sultan
 
Static Data Members and Member Functions
Static Data Members and Member Functions
MOHIT AGARWAL
 

Similar to Polymorphism Using C++ (20)

11.C++Polymorphism [Autosaved].pptx
11.C++Polymorphism [Autosaved].pptx
AtharvPotdar2
 
Exciting JavaScript - Part I
Exciting JavaScript - Part I
Eugene Lazutkin
 
polymorphism.pdf
polymorphism.pdf
riyawagh2
 
polymorphism for b.tech iii year students
polymorphism for b.tech iii year students
Somesh Kumar
 
2nd puc computer science chapter 8 function overloading
2nd puc computer science chapter 8 function overloading
Aahwini Esware gowda
 
Virtual Function
Virtual Function
Lovely Professional University
 
Function different types of funtion
Function different types of funtion
svishalsingh01
 
Comparison between runtime polymorphism and compile time polymorphism
Comparison between runtime polymorphism and compile time polymorphism
CHAITALIUKE1
 
Introduction to c first week slides
Introduction to c first week slides
luqman bawany
 
full defination of final opp.pptx
full defination of final opp.pptx
rayanbabur
 
Java For Automation
Java For Automation
Abhijeet Dubey
 
Louis Loizides iOS Programming Introduction
Louis Loizides iOS Programming Introduction
Lou Loizides
 
iOS Programming Intro
iOS Programming Intro
Lou Loizides
 
Java For beginners and CSIT and IT students
Java For beginners and CSIT and IT students
Partnered Health
 
OOPS & C++(UNIT 4)
OOPS & C++(UNIT 4)
Dr. SURBHI SAROHA
 
Polymorphism
Polymorphism
Amir Ali
 
Functions in c++
Functions in c++
Maaz Hasan
 
Example for Virtual and Pure Virtual function.pdf
Example for Virtual and Pure Virtual function.pdf
rajaratna4
 
object oriented programming language.pptx
object oriented programming language.pptx
syedabbas594247
 
Polymorphism 140527082302-phpapp01
Polymorphism 140527082302-phpapp01
Engr.Tazeen Ahmed
 
11.C++Polymorphism [Autosaved].pptx
11.C++Polymorphism [Autosaved].pptx
AtharvPotdar2
 
Exciting JavaScript - Part I
Exciting JavaScript - Part I
Eugene Lazutkin
 
polymorphism.pdf
polymorphism.pdf
riyawagh2
 
polymorphism for b.tech iii year students
polymorphism for b.tech iii year students
Somesh Kumar
 
2nd puc computer science chapter 8 function overloading
2nd puc computer science chapter 8 function overloading
Aahwini Esware gowda
 
Function different types of funtion
Function different types of funtion
svishalsingh01
 
Comparison between runtime polymorphism and compile time polymorphism
Comparison between runtime polymorphism and compile time polymorphism
CHAITALIUKE1
 
Introduction to c first week slides
Introduction to c first week slides
luqman bawany
 
full defination of final opp.pptx
full defination of final opp.pptx
rayanbabur
 
Louis Loizides iOS Programming Introduction
Louis Loizides iOS Programming Introduction
Lou Loizides
 
iOS Programming Intro
iOS Programming Intro
Lou Loizides
 
Java For beginners and CSIT and IT students
Java For beginners and CSIT and IT students
Partnered Health
 
Polymorphism
Polymorphism
Amir Ali
 
Functions in c++
Functions in c++
Maaz Hasan
 
Example for Virtual and Pure Virtual function.pdf
Example for Virtual and Pure Virtual function.pdf
rajaratna4
 
object oriented programming language.pptx
object oriented programming language.pptx
syedabbas594247
 
Polymorphism 140527082302-phpapp01
Polymorphism 140527082302-phpapp01
Engr.Tazeen Ahmed
 
Ad

More from PRINCE KUMAR (11)

RIP vs OSPF
RIP vs OSPF
PRINCE KUMAR
 
Graphs
Graphs
PRINCE KUMAR
 
Fddi
Fddi
PRINCE KUMAR
 
Binary search tree
Binary search tree
PRINCE KUMAR
 
basics of php
basics of php
PRINCE KUMAR
 
php
php
PRINCE KUMAR
 
Connectivity devices
Connectivity devices
PRINCE KUMAR
 
Tcp ip tutorial
Tcp ip tutorial
PRINCE KUMAR
 
OSI layers
OSI layers
PRINCE KUMAR
 
NETWORKS & TOPOLOGY
NETWORKS & TOPOLOGY
PRINCE KUMAR
 
BUILDING WEBSITES ON WORDPRESS
BUILDING WEBSITES ON WORDPRESS
PRINCE KUMAR
 
Ad

Recently uploaded (20)

Paper 108 | Thoreau’s Influence on Gandhi: The Evolution of Civil Disobedience
Paper 108 | Thoreau’s Influence on Gandhi: The Evolution of Civil Disobedience
Rajdeep Bavaliya
 
Great Governors' Send-Off Quiz 2025 Prelims IIT KGP
Great Governors' Send-Off Quiz 2025 Prelims IIT KGP
IIT Kharagpur Quiz Club
 
2025 June Year 9 Presentation: Subject selection.pptx
2025 June Year 9 Presentation: Subject selection.pptx
mansk2
 
Filipino 9 Maikling Kwento Ang Ama Panitikang Asiyano
Filipino 9 Maikling Kwento Ang Ama Panitikang Asiyano
sumadsadjelly121997
 
Aprendendo Arquitetura Framework Salesforce - Dia 02
Aprendendo Arquitetura Framework Salesforce - Dia 02
Mauricio Alexandre Silva
 
Photo chemistry Power Point Presentation
Photo chemistry Power Point Presentation
mprpgcwa2024
 
How to Manage Different Customer Addresses in Odoo 18 Accounting
How to Manage Different Customer Addresses in Odoo 18 Accounting
Celine George
 
LAZY SUNDAY QUIZ "A GENERAL QUIZ" JUNE 2025 SMC QUIZ CLUB, SILCHAR MEDICAL CO...
LAZY SUNDAY QUIZ "A GENERAL QUIZ" JUNE 2025 SMC QUIZ CLUB, SILCHAR MEDICAL CO...
Ultimatewinner0342
 
GREAT QUIZ EXCHANGE 2025 - GENERAL QUIZ.pptx
GREAT QUIZ EXCHANGE 2025 - GENERAL QUIZ.pptx
Ronisha Das
 
English 3 Quarter 1_LEwithLAS_Week 1.pdf
English 3 Quarter 1_LEwithLAS_Week 1.pdf
DeAsisAlyanajaneH
 
A Visual Introduction to the Prophet Jeremiah
A Visual Introduction to the Prophet Jeremiah
Steve Thomason
 
Peer Teaching Observations During School Internship
Peer Teaching Observations During School Internship
AjayaMohanty7
 
OBSESSIVE COMPULSIVE DISORDER.pptx IN 5TH SEMESTER B.SC NURSING, 2ND YEAR GNM...
OBSESSIVE COMPULSIVE DISORDER.pptx IN 5TH SEMESTER B.SC NURSING, 2ND YEAR GNM...
parmarjuli1412
 
SCHIZOPHRENIA OTHER PSYCHOTIC DISORDER LIKE Persistent delusion/Capgras syndr...
SCHIZOPHRENIA OTHER PSYCHOTIC DISORDER LIKE Persistent delusion/Capgras syndr...
parmarjuli1412
 
List View Components in Odoo 18 - Odoo Slides
List View Components in Odoo 18 - Odoo Slides
Celine George
 
YSPH VMOC Special Report - Measles Outbreak Southwest US 6-14-2025.pptx
YSPH VMOC Special Report - Measles Outbreak Southwest US 6-14-2025.pptx
Yale School of Public Health - The Virtual Medical Operations Center (VMOC)
 
IIT KGP Quiz Week 2024 Sports Quiz (Prelims + Finals)
IIT KGP Quiz Week 2024 Sports Quiz (Prelims + Finals)
IIT Kharagpur Quiz Club
 
ECONOMICS, DISASTER MANAGEMENT, ROAD SAFETY - STUDY MATERIAL [10TH]
ECONOMICS, DISASTER MANAGEMENT, ROAD SAFETY - STUDY MATERIAL [10TH]
SHERAZ AHMAD LONE
 
ENGLISH_Q1_W1 PowerPoint grade 3 quarter 1 week 1
ENGLISH_Q1_W1 PowerPoint grade 3 quarter 1 week 1
jutaydeonne
 
The Man In The Back – Exceptional Delaware.pdf
The Man In The Back – Exceptional Delaware.pdf
dennisongomezk
 
Paper 108 | Thoreau’s Influence on Gandhi: The Evolution of Civil Disobedience
Paper 108 | Thoreau’s Influence on Gandhi: The Evolution of Civil Disobedience
Rajdeep Bavaliya
 
Great Governors' Send-Off Quiz 2025 Prelims IIT KGP
Great Governors' Send-Off Quiz 2025 Prelims IIT KGP
IIT Kharagpur Quiz Club
 
2025 June Year 9 Presentation: Subject selection.pptx
2025 June Year 9 Presentation: Subject selection.pptx
mansk2
 
Filipino 9 Maikling Kwento Ang Ama Panitikang Asiyano
Filipino 9 Maikling Kwento Ang Ama Panitikang Asiyano
sumadsadjelly121997
 
Aprendendo Arquitetura Framework Salesforce - Dia 02
Aprendendo Arquitetura Framework Salesforce - Dia 02
Mauricio Alexandre Silva
 
Photo chemistry Power Point Presentation
Photo chemistry Power Point Presentation
mprpgcwa2024
 
How to Manage Different Customer Addresses in Odoo 18 Accounting
How to Manage Different Customer Addresses in Odoo 18 Accounting
Celine George
 
LAZY SUNDAY QUIZ "A GENERAL QUIZ" JUNE 2025 SMC QUIZ CLUB, SILCHAR MEDICAL CO...
LAZY SUNDAY QUIZ "A GENERAL QUIZ" JUNE 2025 SMC QUIZ CLUB, SILCHAR MEDICAL CO...
Ultimatewinner0342
 
GREAT QUIZ EXCHANGE 2025 - GENERAL QUIZ.pptx
GREAT QUIZ EXCHANGE 2025 - GENERAL QUIZ.pptx
Ronisha Das
 
English 3 Quarter 1_LEwithLAS_Week 1.pdf
English 3 Quarter 1_LEwithLAS_Week 1.pdf
DeAsisAlyanajaneH
 
A Visual Introduction to the Prophet Jeremiah
A Visual Introduction to the Prophet Jeremiah
Steve Thomason
 
Peer Teaching Observations During School Internship
Peer Teaching Observations During School Internship
AjayaMohanty7
 
OBSESSIVE COMPULSIVE DISORDER.pptx IN 5TH SEMESTER B.SC NURSING, 2ND YEAR GNM...
OBSESSIVE COMPULSIVE DISORDER.pptx IN 5TH SEMESTER B.SC NURSING, 2ND YEAR GNM...
parmarjuli1412
 
SCHIZOPHRENIA OTHER PSYCHOTIC DISORDER LIKE Persistent delusion/Capgras syndr...
SCHIZOPHRENIA OTHER PSYCHOTIC DISORDER LIKE Persistent delusion/Capgras syndr...
parmarjuli1412
 
List View Components in Odoo 18 - Odoo Slides
List View Components in Odoo 18 - Odoo Slides
Celine George
 
IIT KGP Quiz Week 2024 Sports Quiz (Prelims + Finals)
IIT KGP Quiz Week 2024 Sports Quiz (Prelims + Finals)
IIT Kharagpur Quiz Club
 
ECONOMICS, DISASTER MANAGEMENT, ROAD SAFETY - STUDY MATERIAL [10TH]
ECONOMICS, DISASTER MANAGEMENT, ROAD SAFETY - STUDY MATERIAL [10TH]
SHERAZ AHMAD LONE
 
ENGLISH_Q1_W1 PowerPoint grade 3 quarter 1 week 1
ENGLISH_Q1_W1 PowerPoint grade 3 quarter 1 week 1
jutaydeonne
 
The Man In The Back – Exceptional Delaware.pdf
The Man In The Back – Exceptional Delaware.pdf
dennisongomezk
 

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  */