Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
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

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