SlideShare a Scribd company logo
POLYMORPHISM
What is polymorphism?
 The process of representing one form in multiple forms is known as polymorphism.
 Polymorphism is derived from two words: “poly” and “morphs” means many and forms.
Real life example:
When in school we behave like student.
When in bus we behave like passenger.
When in home we behave like son.
Here one person having different behaviours.
Types of polymorphism:
Compile Time Polymorphism:
 Compile-time polymorphism is a process in which the method is called when
compile time. It is of two types.
Function Overloading
Operator Overloading
Function Overloading:
 Function overloading is a feature of object-oriented programming where two or
more functions can have the same name but different parameters. When a function
name is overloaded with different jobs it is called Function Overloading.
Ways to Overload a Function:
By changing number of Arguments.
By having different datatypes of argument.
When an overloaded function is called, the c++ compiler selects the proper function
by examining the number, types and order of arguments.
Example:
#include <iostream>
using namespace std;
int sum(int a,int b)
{
return a+b;
}
int sum(int a,int b,int c)
{
return a+b+c;
}
int main()
{
int a=5,b=10,c=15;
cout<<sum(a,b)<<endl;
cout<<sum(a,b,c);
return 0;
}
Output:
15
30
Operator Overloading:
 Operator Overloading is the method by which we can change the function of
some specific operators to do some different task.
 It allows the same operator name or symbol to be used for multiple
operations.
Operators that cannot be overload:
 :: - Scope resolution operator
 - Dot operator (Member Selector)
 * - Pointer (Member selector through a pointer to a function.
 ?: - Ternary operator
.
Advantages of operator overloading:
• It makes the program easier to understand for people, especially
in a larger code-base.
• It enables programmers to use notation closer to the target domain.
Example:
#include <iostream>
using namespace std;
class ComplexNumber{
private:
int real;
int imaginary;
public:
ComplexNumber(int real, int imaginary){
this->real = real;
this->imaginary = imaginary;
}
void print(){
cout<<real<<" + i"<<imaginary;
}
ComplexNumber operator+ (ComplexNumber c2){
ComplexNumber c3(0,0);
c3.real = this->real+c2.real;
c3.imaginary = this->imaginary + c2.imaginary;
return c3;
}
};
int main() {
ComplexNumber c1(3,5);
ComplexNumber c2(2,4);
ComplexNumber c3 = c1 + c2;
c3.print();
return 0;
}
Function overriding
• Function overriding in C++ is a feature that allows us to use a function in
the child class that is already present in its parent class.
• Function overriding helps to improve the readability of the code.
Advantage of Function Overriding:
• It improves code readability and allows code reusability.
• It save memory space, consistency, and readability.
• It speeds up the execution of the program.
#include <iostream>
using namespace std;
class Base {
public:
void print() {
cout << "Base Class" << endl;
}
};
class Derived : public Base {
public:
void print() {
cout << "Derived Class" << endl;
}
};
int main() {
Derived derived1;
derived1.print();
return 0;
}
Example:
Output:
Derived Class

More Related Content

Similar to oops.pptx (20)

PDF
Ch-4-Operator Overloading.pdf
esuEthopi
 
PPT
3d7b7 session4 c++
Mukund Trivedi
 
PDF
M11 operator overloading and type conversion
NabeelaNousheen
 
PDF
Unit3_OOP-converted.pdf
PowerfullBoy1
 
PDF
Polymorphism and Type Conversion.pdf pot
e13225064
 
PPTX
3. Polymorphism.pptx
ChhaviCoachingCenter
 
PDF
polymorphism.pdf
riyawagh2
 
PDF
Operator overloading
Pranali Chaudhari
 
PPTX
Operator overloading2
zindadili
 
PPTX
Polymorphism Using C++
PRINCE KUMAR
 
PPTX
Presentation on polymorphism in c++.pptx
vishwadeep15
 
PPTX
Function overloading and overriding
Rajab Ali
 
PDF
Polymorphism
ajeela mushtaq
 
PPTX
functions over-loading, over-hiding and over-riding
zaidurrehman5
 
PPTX
Operator Overloading & Function Overloading
Meghaj Mallick
 
PPT
14 operator overloading
Docent Education
 
PPTX
Operator Overloading
Juginder Pal Singh
 
PDF
Lec 8.pdf a
aliashraf9689
 
PPTX
Operator overloading
ramya marichamy
 
Ch-4-Operator Overloading.pdf
esuEthopi
 
3d7b7 session4 c++
Mukund Trivedi
 
M11 operator overloading and type conversion
NabeelaNousheen
 
Unit3_OOP-converted.pdf
PowerfullBoy1
 
Polymorphism and Type Conversion.pdf pot
e13225064
 
3. Polymorphism.pptx
ChhaviCoachingCenter
 
polymorphism.pdf
riyawagh2
 
Operator overloading
Pranali Chaudhari
 
Operator overloading2
zindadili
 
Polymorphism Using C++
PRINCE KUMAR
 
Presentation on polymorphism in c++.pptx
vishwadeep15
 
Function overloading and overriding
Rajab Ali
 
Polymorphism
ajeela mushtaq
 
functions over-loading, over-hiding and over-riding
zaidurrehman5
 
Operator Overloading & Function Overloading
Meghaj Mallick
 
14 operator overloading
Docent Education
 
Operator Overloading
Juginder Pal Singh
 
Lec 8.pdf a
aliashraf9689
 
Operator overloading
ramya marichamy
 

Recently uploaded (20)

DOCX
Unit 5: Speech-language and swallowing disorders
JELLA VISHNU DURGA PRASAD
 
PDF
Antianginal agents, Definition, Classification, MOA.pdf
Prerana Jadhav
 
DOCX
pgdei-UNIT -V Neurological Disorders & developmental disabilities
JELLA VISHNU DURGA PRASAD
 
PPTX
Cybersecurity: How to Protect your Digital World from Hackers
vaidikpanda4
 
PPT
DRUGS USED IN THERAPY OF SHOCK, Shock Therapy, Treatment or management of shock
Rajshri Ghogare
 
PPTX
Gupta Art & Architecture Temple and Sculptures.pptx
Virag Sontakke
 
PPTX
How to Track Skills & Contracts Using Odoo 18 Employee
Celine George
 
PPTX
Dakar Framework Education For All- 2000(Act)
santoshmohalik1
 
PDF
My Thoughts On Q&A- A Novel By Vikas Swarup
Niharika
 
PPTX
Artificial Intelligence in Gastroentrology: Advancements and Future Presprec...
AyanHossain
 
PPTX
ENGLISH 8 WEEK 3 Q1 - Analyzing the linguistic, historical, andor biographica...
OliverOllet
 
PDF
The Minister of Tourism, Culture and Creative Arts, Abla Dzifa Gomashie has e...
nservice241
 
PDF
Module 2: Public Health History [Tutorial Slides]
JonathanHallett4
 
PDF
Tips for Writing the Research Title with Examples
Thelma Villaflores
 
PPTX
Python-Application-in-Drug-Design by R D Jawarkar.pptx
Rahul Jawarkar
 
PPTX
Applications of matrices In Real Life_20250724_091307_0000.pptx
gehlotkrish03
 
PPTX
Cleaning Validation Ppt Pharmaceutical validation
Ms. Ashatai Patil
 
PPTX
20250924 Navigating the Future: How to tell the difference between an emergen...
McGuinness Institute
 
PPTX
Electrophysiology_of_Heart. Electrophysiology studies in Cardiovascular syste...
Rajshri Ghogare
 
PPTX
Unlock the Power of Cursor AI: MuleSoft Integrations
Veera Pallapu
 
Unit 5: Speech-language and swallowing disorders
JELLA VISHNU DURGA PRASAD
 
Antianginal agents, Definition, Classification, MOA.pdf
Prerana Jadhav
 
pgdei-UNIT -V Neurological Disorders & developmental disabilities
JELLA VISHNU DURGA PRASAD
 
Cybersecurity: How to Protect your Digital World from Hackers
vaidikpanda4
 
DRUGS USED IN THERAPY OF SHOCK, Shock Therapy, Treatment or management of shock
Rajshri Ghogare
 
Gupta Art & Architecture Temple and Sculptures.pptx
Virag Sontakke
 
How to Track Skills & Contracts Using Odoo 18 Employee
Celine George
 
Dakar Framework Education For All- 2000(Act)
santoshmohalik1
 
My Thoughts On Q&A- A Novel By Vikas Swarup
Niharika
 
Artificial Intelligence in Gastroentrology: Advancements and Future Presprec...
AyanHossain
 
ENGLISH 8 WEEK 3 Q1 - Analyzing the linguistic, historical, andor biographica...
OliverOllet
 
The Minister of Tourism, Culture and Creative Arts, Abla Dzifa Gomashie has e...
nservice241
 
Module 2: Public Health History [Tutorial Slides]
JonathanHallett4
 
Tips for Writing the Research Title with Examples
Thelma Villaflores
 
Python-Application-in-Drug-Design by R D Jawarkar.pptx
Rahul Jawarkar
 
Applications of matrices In Real Life_20250724_091307_0000.pptx
gehlotkrish03
 
Cleaning Validation Ppt Pharmaceutical validation
Ms. Ashatai Patil
 
20250924 Navigating the Future: How to tell the difference between an emergen...
McGuinness Institute
 
Electrophysiology_of_Heart. Electrophysiology studies in Cardiovascular syste...
Rajshri Ghogare
 
Unlock the Power of Cursor AI: MuleSoft Integrations
Veera Pallapu
 

oops.pptx

  • 2. What is polymorphism?  The process of representing one form in multiple forms is known as polymorphism.  Polymorphism is derived from two words: “poly” and “morphs” means many and forms. Real life example: When in school we behave like student. When in bus we behave like passenger. When in home we behave like son. Here one person having different behaviours.
  • 4. Compile Time Polymorphism:  Compile-time polymorphism is a process in which the method is called when compile time. It is of two types. Function Overloading Operator Overloading
  • 5. Function Overloading:  Function overloading is a feature of object-oriented programming where two or more functions can have the same name but different parameters. When a function name is overloaded with different jobs it is called Function Overloading. Ways to Overload a Function: By changing number of Arguments. By having different datatypes of argument. When an overloaded function is called, the c++ compiler selects the proper function by examining the number, types and order of arguments.
  • 6. Example: #include <iostream> using namespace std; int sum(int a,int b) { return a+b; } int sum(int a,int b,int c) { return a+b+c; } int main() { int a=5,b=10,c=15; cout<<sum(a,b)<<endl; cout<<sum(a,b,c); return 0; } Output: 15 30
  • 7. Operator Overloading:  Operator Overloading is the method by which we can change the function of some specific operators to do some different task.  It allows the same operator name or symbol to be used for multiple operations. Operators that cannot be overload:  :: - Scope resolution operator  - Dot operator (Member Selector)  * - Pointer (Member selector through a pointer to a function.  ?: - Ternary operator .
  • 8. Advantages of operator overloading: • It makes the program easier to understand for people, especially in a larger code-base. • It enables programmers to use notation closer to the target domain.
  • 9. Example: #include <iostream> using namespace std; class ComplexNumber{ private: int real; int imaginary; public: ComplexNumber(int real, int imaginary){ this->real = real; this->imaginary = imaginary; } void print(){ cout<<real<<" + i"<<imaginary; } ComplexNumber operator+ (ComplexNumber c2){ ComplexNumber c3(0,0); c3.real = this->real+c2.real; c3.imaginary = this->imaginary + c2.imaginary; return c3; } }; int main() { ComplexNumber c1(3,5); ComplexNumber c2(2,4); ComplexNumber c3 = c1 + c2; c3.print(); return 0; }
  • 10. Function overriding • Function overriding in C++ is a feature that allows us to use a function in the child class that is already present in its parent class. • Function overriding helps to improve the readability of the code. Advantage of Function Overriding: • It improves code readability and allows code reusability. • It save memory space, consistency, and readability. • It speeds up the execution of the program.
  • 11. #include <iostream> using namespace std; class Base { public: void print() { cout << "Base Class" << endl; } }; class Derived : public Base { public: void print() { cout << "Derived Class" << endl; } }; int main() { Derived derived1; derived1.print(); return 0; } Example: Output: Derived Class