0% found this document useful (0 votes)
18 views

Polymorphism & Inheritance

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

Polymorphism & Inheritance

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 24

Polymorphism in C++

&
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

Complex operator + (Complex const &obj) {


Complex res;
res.real = real + obj.real;
res.imag = imag + obj.imag;
return res;
}
void print()
{
cout << real << " + i" << imag << endl;
}
};
int main()
{
Output:
Complex c1(10, 5), c2(2, 4); 12 + i9
Complex c3 = c1 + c2; // An example call to "operator+"
c3.print();
• What is the difference between operator functions and
normal functions?
Operator functions are same as normal functions. The only
differences are, name of an operator function is always
operator keyword followed by symbol of operator and
operator functions are called when the corresponding
operator is used.
• Almost all operators can be overloaded except few.
Following is the list of operators that cannot be overloaded.
• . (dot) member access specifier
• :: (scope resolution operator)
• ?: (conditional operator
• sizeof (size operator)
• Runtime polymorphism: This type of
polymorphism is achieved by Function
Overriding.
Function overriding on the other hand occurs
when a derived class has a definition for one
of the member functions of the base class.
That base function is said to be overridden.
Inheritance in C++

• The capability of a class to derive properties and


characteristics from another class is
called Inheritance. Inheritance is one of the most
important feature of Object Oriented
Programming.
Sub Class: The class that inherits properties from
another class is called Sub class or Derived Class.
Super Class:The class whose properties are
inherited by sub class is called Base Class or Super
class.
• Implementing inheritance in C++: For creating
a sub-class which is inherited from the base
class we have to follow the below syntax.
Syntax:
class subclassName : accessMode baseClassName
• {
• //body of subclass
• };
/ C++ program to demonstrate //main function
implementation int main()
// of Inheritance {

#include <iostream> Child obj1;


using namespace std;
// An object of class child has all data
//Base class members
class Parent // and member functions of class parent
obj1.id_c = 7;
{
obj1.id_p = 91;
public:
cout << "Child id is " << obj1.id_c << endl;
int id_p;
cout << "Parent id is " << obj1.id_p <<
}; endl;

// Sub class inheriting from Base return 0;


Class(Parent) } Output:
class Child : public Parent Child id is 7
Parent id is 91
{
public:
int id_c;
};
INHERITANCE
• Modes of Inheritance
• Public mode: If we derive a sub class from a public base class. Then the public
member of the base class will become public in the derived class and protected
members of the base class will become protected in derived class.

• 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

• C++ supports five types of inheritance:


• Single inheritance
• Multiple inheritance
• Hierarchical inheritance
• Multilevel inheritance
• Hybrid inheritance
1. Single inheritance #include <iostream>
using namespace std;
is defined as the class Account {
public:
inheritance in which a float salary = 60000;
derived class is };
class Programmer: public Account
inherited from the only {
one base class. public:
float bonus = 5000;
Where 'A' is the base class, };
int main() {
and 'B' is the derived Programmer p1;
class. A
cout<<"Salary: "<<p1.salary<<endl;

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.

Syntax of the Derived class:


class D : visibility B-1, visibility B-2, ?
{
// Body of the class;
}
class C : public A,public B
#include <iostream> {
using namespace std; public:
void display()
class A
{
{
cout << "The value of a is : " <<a<< endl;
protected:
int a;
cout << "The value of b is : " <<b<< endl;
public: cout<<"Addition of a and b is : "<<a+b;
void get_a(int n) }
{ };
a = n; int main()
} {
}; C c;
c.get_a(10);
class B
c.get_b(20);
{
c.display();
protected:
int b;
public: return 0;
void get_b(int n) }
{ Output:
b = n; The value of a is : 10
} The value of b is : 20
Addition of a and b is : 30
};
Ambiquity Resolution in Inheritance
class C : public A, public B
#include <iostream>
{
using namespace std;
void view()
class A
{
{
display();
public:
}
void display()
};
{ int main()
cout << "Class A" << endl; {
} C c;
}; c.display();
class B return 0;
{ }
public:
void display()
{
cout << "Class B" << endl;
} Output:
}; error: reference to 'display' is ambiguous
display();
The above issue can be resolved by using the class resolution operator with
the function. In the above example, the derived class code can be
rewritten as:

class C : public A, public B


{
void view()
{
A :: display(); // Calling the display() function of class A.
B :: display(); // Calling the display() function of class B.

}
};
Hybrid Inheritance Hierarchical Inheritance

Hybrid inheritance is a combination Hierarchical inheritance is defined as the


of more than one type of process of deriving more than one class from a
inheritance. base class.

You might also like