You are on page 1of 34

INHERITANCE – Lect 1

BY : MUHAMMAD IRTAZA SALEEM


Protected Attributes can not be
accessed outside of class.
However, protected attributes can
be inherited.

Private Attributes can NEVER be


inherited.
What is Inheritance in Actual ?
Why Inheritance ?

No need to re-invent the wheel


Concept of Derived and Base Class
Inheritance is one of the key features of Object-
oriented programming in C++.
It allows us to create a new class (derived class)
from an existing class (base class).
It is possible to inherit attributes and methods
from one class to another
The derived class inherits the features from the
base class and can have additional features of its
own.
Example 1
Example - 2
Let’s put an END to PROTECTED MYSTERY
Note: Private Attributes are NEVER Inherited into the derived class
Types of Inheritance
1. Single Inheritance
2. Multiple Inheritance
3. Multi Level Inheritance
4. Hybrid Inheritance
5. Hierarchical Inheritance
Parent Class

Derived Class
Class A – Parent Class
Class B – Child Class (Derived Class)

All protected and public attributes and methods of


Class A have Been inherited in B class “Publically”
Parent Class

Derived Class
For Class C, Class A and B are – Parent
Class
Class C – Child Class (Derived Class)

All protected and public attributes and


methods of
Class A and B have Been inherited in C
class “Publically”
Parent Class of B

Derived Class from A,


Parent Class of C

Derived Class from B


For Class B, Class A is – Parent Class
Class B – Child Class (Derived Class)

For Class C, Class B is – Parent Class


Class C – Child Class (Derived Class)

All protected and public attributes and


methods of
Class A have been inherited in Class B
publically.
And Class B’s have Been inherited in C class
“Publically”.
What if you inherit something Private?

Class B inherits all


the public and
protected attributes
of Class A as private
to Class B.
Class B inherits all the public
and protected attributes of
Class A as private to Class B.

Class C inherits all the public


and protected attributes of
Class B.
Note: attributes of A class are
as Private in Class B. so they
won’t be inherited.
*Error in Code. Why ?
Coding
is-a relationship
Inheritance is an is-a relationship. We use inheritance only if an is-a relationship is present between
the two classes.
Here are some examples:
A car is a vehicle.
Orange is a fruit.
A surgeon is a doctor.
A dog is an animal.

Syntax –
Class derived-class: access-specifier base-class
Access Specifiers with derived Classes
class Animal { };
 class Dog : public Animal { };
class Dog: private Animal {};
class Dog: protected Animal {};
The various ways we can derive classes are known as access modes. These access
modes have the following effect:
1. Public – If a derived class is declared in Public mode, then the members of the
base class are inherited by the derived class just as they are.
2. Private – In this case, all the members of the base class become private
members in the derived class.
3. Protected – The public members of the base class become protected members
in the derived class.
- The Private Members are always inherited as Private members.
Polymorphism
Polymorphism in C++
Polymorphism means, having many or multiple forms.
 In simple words, we can define polymorphism as the ability of a
message to be displayed in more than one form.
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.
So 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.
Types of Polymorphism
1 – Compile Time
2 – Run Time
Compile Time - Function 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.
Compile Time – Operator Overloading
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.
Remember the last lab ? Adding the
Complex Numbers ?

You made individual functions ?

Why couldn’t we use the ‘+’ sign to add


those Complex Numbers?
Complex 3 = Complex1.add(Complex2);

Complex 3 = Complex1 + Complex2;


C++ Operator Overloading
In C++, we can change the way operators work for user-defined types like objects and structures. This
is known as operator overloading.

What are we expecting ??

Complex val1(6,1);
Complex val2(2,5);
Complex val3;

val3= val1+val2;
Basic Syntax
Complex Class Operator
& Constructor Overloading
Main Function & Output
Run Time Polymorphism

You might also like