Polymorphism & Inheritance
Polymorphism & Inheritance
&
Inheritance
• The word polymorphism means having many
forms. In simple words, we can define
polymorphism as the ability of a message to
be displayed in more than one form.
• Eg- A real-life example of polymorphism, a
person at the same time can have different
characteristics.
• Like a man at the same time is a father, a
husband, an employee.
• The same person posses different behavior in
different situations. This is called
polymorphism.
• Polymorphism is considered as one of the
important features of Object Oriented
Programming.
In C++ polymorphism is mainly divided
into two types:
•Compile time Polymorphism
•Runtime Polymorphism
Compile time polymorphism:
• This type of polymorphism is achieved by
function overloading or operator overloading
• Function Overloading: When there are
multiple functions with same name but
different parameters then these functions are
said to be overloaded. Functions can be
overloaded by change in number of
arguments or/and change in type of
arguments.
#include <iostream>
• Function using namespace std;
overloading is a void print(int i)
feature in C++ {
cout << " Here is int " << i << endl;
where two or }
void print(double f) {
more functions cout << " Here is float " << f << endl;
can have the }
void print(char const *c) {
same name but cout << " Here is char* " << c << endl;
}
different
int main() {
parameters. print(10);
print(10.10);
print("ten");
return 0;
} Output:
Here is int 10
Here is float 10.1
Here is char* ten
• Operator Overloading: C++ also provide option to overload operators.
• For example, we can make the operator (‘+’) for string class to
concatenate two strings.
• We know that this is the addition operator whose task is to add two
operands.
• So a single operator ‘+’ when placed between integer operands , adds
them and when placed between string operands, concatenates them.
• SYNTAX
class className
{ ... .. ...
public :
returnType operator symbol (arguments)
{ ... .. ...
} ... .. ...
};
#include<iostream>
using namespace std;
class Complex {
private:
int real, imag;
public:
Complex(int r = 0, int i =0)
{ real = r;
imag = i;
}
// This is automatically called when '+' is used with
// between two Complex objects
• Protected mode: If we derive a sub class from a Protected base class. Then both
public member and protected members of the base class will become protected in
derived class.
• Private mode: If we derive a sub class from a Private base class. Then both public
member and protected members of the base class will become Private in derived
class.
• Note : The private members in the base class cannot be directly accessed in the
derived class, while protected members can be directly accessed.
• A derived class inherits all base class methods
with the following exceptions −
• Constructors, destructors and copy
constructors of the base class.
• Overloaded operators of the base class.
• The friend functions of the base class
Types Of Inheritance
cout<<"Bonus: "<<p1.bonus<<endl;
return 0;
}
B
Output:
Salary: 60000
Bonus: 5000
• Multilevel
Inheritance
When one class inherits another
class which is further inherited by
another class, it is known as multi
A
level inheritance in C++.
Inheritance is transitive so the last
derived class acquires all the
B members of all its base classes.
C
#include <iostream> class BabyDog: public Dog
using namespace std; {
public:
class Animal {
void weep() {
public:
cout<<"Weeping...";
void eat() { }
cout<<"Eating..."<<endl; };
} int main(void) {
}; BabyDog d1;
class Dog: public Animal d1.eat();
{ d1.bark();
public: d1.weep();
return 0;
void bark(){
}
cout<<"Barking..."<<endl;
}
Output:
};
Eating...
Barking...
Weeping...
Multiple Inheritance
Multiple inheritance is the process
of deriving a new class that inherits
the attributes from two or more
classes.
}
};
Hybrid Inheritance Hierarchical Inheritance