2. Lesson Objectives
Definition of Inheritance
Hierarchy of Inheritance
Type of Inheritance
C++ Inheritance Implementation
3. Introduction
Inheritance denotes an is-a relationship.
The natural phenomenon where one concept can be
viewed as a sub-concept of another
Mammal
Human
More General class
More Specific class
is-a
4. Inheritance
What do the sub-class inherit from the Main class? All
Features!
Can there be sub-class have feature not in the main
class? Yes!
Human inherits all features in
the mammal class in addition to
his own features!
Mammal
Mammal Glands
Vertebral Column
Human
Mammalian glans
Vertebral column
is-a
5. Examples of Inheritance
A car is a vehicle.
Earth is a planet
Orange is a fruit.
A surgeon is a doctor.
A dog is an animal.
6. Hierarchy of Inheritances
Mammal
Mammal Glands
Vertebral Column
Human
Vertical orientation
Dog
Non Vertical orientation
Homo
Sapien
Homo
Erectus
Dalmatian Doodle
7. Inheritance: Adopted in OOP
The process of creating new classes, from existing
ones.
The existing class is called base/Super classes
The new class is called derived/Sub classes
A Derived class has the potential to inherit all the
capabilities of the base class in addition to its own.
8. Inheritance - Illustration
A Derived class has the potential to inherit all the
capabilities of the base class in addition to its own.
Base Class
Feature A
Feature B
Subclass
Feature A
Feature B
Feature C
Inherits From
9. Advantages of Inheritance
Permits code reusability
Once a class has been written to perform a given function, it
can always be adapted to perform other functions through
inheritance instead of modifying its code.
Reusing existing code saves time and money and increases a
program’s reliability.
Reusability ensures the ease of distributing class libraries.
10. Specifying the Derived Class
Inheritance is specified using the colon (:) as
follows:
class Subclass: Baseclass
If Baseclass has already been declared and accessible
11. Type of Inheritance (Access )
When deriving a class from a base class, the base class may
be inherited through public, protected or private inheritance.
The type of inheritance is specified by the access- specifier.
We hardly use protected or private inheritance,
but public inheritance is commonly used. While using
different type of inheritance, following rules are applied:
12. Type of Inheritance
Public Inheritance: When deriving a class from
a public base class, public members of the base class
become public members of the derived class
and protected members of the base class
become protected members of the derived class.
A base class's private members are never accessible directly
from a derived class, but can be accessed through calls to
the public and protected members of the base class.
13. Type of Inheritance
Protected Inheritance: When deriving from a protected base
class, public and protected members of the base class
become protected members of the derived class.
Private Inheritance: When deriving from a private base
class, public and protected members of the base class
become private members of the derived class
14. C++ Inheritance
General Format for implementing the concept of Inheritance:
class derived_classname: access specifier baseclassname
For example, if the base class is MyClass and the derived class is
sample it is specified as:
class sample: public MyClass
The above makes sample have access to both public and protected
variables of base class MyClass
16. C++ Inheritance
Inheritance Example:
class sample: public MyClass
{
public:
sample(void) { s1=0; }
void f1(int n1)
{ s1=n1*10;}
void output(void)
{
MyClass::output(); cout << s1; }
private:
int s1;
17. C++ Inheritance
Inheritance Example:
int main(void)
{ sample s;
s.f(10);
s.output();
s.f1(20);
s.output();
}
The output of the above program is
50
200
18. Types of Inheritance (Hierarchy)
1. Single class Inheritance:
Single inheritance is the one where you have a single base class and a
single derived class.
Class Employee
Class Manager
It is a Base class (super)
it is a sub class (derived)
19. Types of Inheritance (Hierarchy)
2. Multilevel Inheritance:
In Multi level inheritance, a class inherits its properties from
another derived class.
Class A
Class B
it is a Base class
(super) of B
it is a sub class (derived) of
A and base class of class C
Class C derived class(sub) of
class B
20. Types of Inheritance (Hierarchy)
3. Multiple Inheritances:
In Multiple inheritances, a derived class inherits from multiple
base classes. It has properties of both the base classes.
Class A Class B Base class
Class C Derived class
25. Brain Teaser
• After her death, a mother left her sons her entire £5000
fortune.
• The eldest son received £3000.
• The rest was shared equally between whomever
remained.
• How much did they each get?
26. Brain Teaser
• A man left an inheritance of $10,000 to three relatives
and their wives. Together the wives received $3960.
Janine received $100 more than Cathy, and Maria
received $100 more than Janine. Joe received the same
amount as his wife, Hary got half as much again as his
wife, and Tom received twice as much as his wife.
• How much did each receive?
Editor's Notes
If more than one class is inherited from the base class, it's known as hierarchical inheritance. In hierarchical inheritance, all features that are common in child classes are included in the base class.
For example, Physics, Chemistry, Biology are derived from Science class. Similarly, Dog, Cat, Horse are derived from Animal class.
When we make a class as friend, all its member functions automatically become friend functions.
When we make a class as friend, all its member functions automatically become friend functions.
When we make a class as friend, all its member functions automatically become friend functions.
When we make a class as friend, all its member functions automatically become friend functions.
When we make a class as friend, all its member functions automatically become friend functions.
When we make a class as friend, all its member functions automatically become friend functions.
When we make a class as friend, all its member functions automatically become friend functions.
When we make a class as friend, all its member functions automatically become friend functions.
When we make a class as friend, all its member functions automatically become friend functions.
When we make a class as friend, all its member functions automatically become friend functions.
When we make a class as friend, all its member functions automatically become friend functions.
When we make a class as friend, all its member functions automatically become friend functions.
When we make a class as friend, all its member functions automatically become friend functions.
Answer
There is just one other son who takes the remaining £2000.
The word elder means there are two. If there were more than two it would have been eldest.
Answer
Carthy, Janine and Maria received $1220, $1320, and $1420 respectively. Joe received the same as his wife Carol, $1220. Hary received half again what his wife Janine did, $1980. Tommy got twice as much as his wife Maria, $2840.
The word elder means there are two. If there were more than two it would have been eldest.