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

Week 13

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Week 13

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 74

Inheritance

• Inheritance is a relationship between classes wherein one class is derived


from another class.

• The derived class is called child class, and the class from which it is derived is
called parent class.

• Inheritance is also called as parent–child relationship between classes such


that child class gives a specialized implementation of parent class.

• Reusability of the code is the key principle of inheritance because the


properties defined in the parent class are borrowed by the child class.
• Inheritance is one of the most important features of object oriented
programming, wherein the classes are organized in a hierarchical form.

• The class at the top of hierarchy is called parent class, whereas the class at
the bottom of the hierarchy is called child class.

• Advantage of inheritance is that the members (variables and functions)


defined by the parent class are also made available into the child class.

• This saves the programmers efforts to redevelop logic in child class,


which is already implemented in parent class.
• parent class is also called Base class/Super class/ Generalized class and
the child class is also called Derived class/Sub class/Specialized class
Principle of inheritance
As an example, let’s assume that we need to store data about students and
professors in a particular institute in computer memory.

• The requirement is that for every person(a student or a professor)we


need to store name, date of birth, and gender.

• The only attribute difference is that professor has two additional


attributes salary and prof_id which student does not, and student has
additional attributes marks and roll_no, which professor does not.

• This means that for every student object, we need to store following
information in computer memory:
1. Name 2. Date of Birth(dob) 3. Gender 4. Roll Number(roll_no) 5. Marks
Also, for every Professor object, we plan to store following information in computer
memory: 1. Name 2. Date of Birth 3. Gender 4. Professor Identifier(prof_id) 5. Salary

Classes Student and Professor


Instead of duplicating this code in both the classes, we can create a common
parent class named as Person which defines the common members.

Inheritance
Creating a Parent–Child Relationship between Classes

Syntax of inheritance
When the <accesType> is specified as private while creating the child class , the
inheritance is then called ‘private inheritance’

In case of private
inheritance, the
public/protected members
of parent class A will be
inherited as private
members in child class B
Private inheritance
When the <accesType> is specified as protected while creating the child class B,
the inheritance is then called ‘protected inheritance’

In case of protected
inheritance, the
public/protected
members of parent
class A will be inherited
as protected members
in child class B
Protected inheritance
When the <accesType> is specified as public while creating the child class
B, the inheritance is then called ‘public inheritance’

In case of public
inheritance, the public
members of parent class A
will be inherited as public
members in child class B
Public inheritance
• As an example, let us create a class Abc with integer members a, b, and c,
such that the variable a is private, variable b is protected, and variable c is
public.

• Also, we create public functions setA() and getA() inside class Abc to
initialize and return the value of private variable a and public functions
setB(), and getB() to initialize and return the value of protected member b.

• Note that the setter and getter functions for variable c will not be required
because c is a public member of class Abc, and it can be directly accessed in
child as well as non-child class of Abc.

• We create two classes C1 and C2, such that C1 is a child class of Abc, and C2
is a non-child as shown in the Figure
Classes Abc, C1, and C2
To understand the concept of access specifiers in C++, let us write a code that
sets the value of variable res in objects of C1 and C2, as the addition of members
a, b and c defined in class Abc
Hence we have defined C1 as follows in the program:
The object of class C1 will inherit the members and member functions of class Abc
The statement
C1 o1; creates an object of class C1, named as o1.

As seen, the members/member functions of parent class Abc are also the part
of child object o1.

This means that we will now be able to access members of class Abc using the
o1, subject to the controls imposed by access specifiers.

The statements
o1.setA(10); o1.setB(20);

invoke public functions like setA() and setB() using object o1. These are public
functions invoked to initialize the members a and b.
Object of class C1
As setA() is a member function of class Abc, when o1 invokes the setA() function
the control of execution is actually transferred inside class Abc.
Hence the following statement:
o1.setA(10); will set the value of member a of object o1 as 10

Invoking setA() function using object o1


Invoking setB() function using object o1
The initialization of the member c can be done directly from the main() function
because the member c is the public member inherited by the object o1.

Hence, we can initialize the value of member c directly from main() using the
statement below:

o1.c=30;

The state of child object o1 with the values of a, b, and c can be seen from the
following figure
State of object o1
Invoking function add()to perform the addition of a, b, and c and store in res. The
outside world can directly invoke add() because it is a public member of class C1

Access to members b and c in a member function of child class


The function of child class invokes the public function getA() defined inside the
parent class

Access to member a inside the child class


After execution of the statement
res= getA()+b+c; member res will store the result as the addition of a, b, and c

State of object o1
Now, let us create a non-child class C2 with a member res and a member function
add().

Obviously, the object of class C2 will not inherit any members or member functions of
class Abc because C2 is not a child class of class Abc.

This means that the object of class C2 will only contain a member res and a member
function add()

Object of class C2
The statement
C2 o2; creates object of class C2 named as o2.

The requirement is that we need to set the value of member res present inside
the non-child object o2 as the addition of values of members a, b, and c of
class Abc.

As these members are not present in the object memory of o2, we will have to
create a separate object of class Abc using the statement below:

Abc a1;

Object a1 will contain members a, b, and c as shown in the Figure .We will make
use of these member variables to perform addition
Object of class Abc
The statements
a1.setA(100); a1.setB(200); a1.c=300; initialize the value of members a, b
and c for object a1.

State of object a1
The statement
o2.add(a1);
passes object a1 as an argument to add() function.

Hence the members of object a1 can be accessed using object t (which is


the name of the formal argument) inside add()

The members a and b will not be accessible in the non-child class C2; this is
because these members are created as private and protected members of
class Abc,

Therefore, the only way to access the values a and b in the body of function
add() is by invoking the functions getA()and getB() using object t
Object o2 invoking the add() function
After execution of the statement

res=t.getA()+t.getB()+t.c;

member res of object o2 will store the result as the addition of members a, b, and
c of object a1

State of object o2
Types of Inheritance Depending on the structure in which the hierarchy is
created, inheritance can be classified into following types:

1. Single level Inheritance

2. Hierarchical Inheritance

3. Multi-level Inheritance

4. Multiple Inheritance

5. Hybrid Inheritance
Single level inheritance

This is a relationship of inheritance created using single parent and a single


child class as shown in Figure

Let us consider that we need to store data about students


of a particular university in the computer memory with
following attributes for each student

1. Name of the student (character array: name)


2. Date of birth (object: dob)
3. Gender (character variable: gender)
4. Roll Number (integer variable: roll_no)
5. Marks obtained (integer variable: marks)
• Out of the five members, first three (name, dob, and gender) would be
relevant to class Professor. These are called as generic members because
there is a chance to reuse them at a later stage.

• Whereas, the other two members (roll_no and marks) are specific to class
Student, and there is no chance to reuse them. Such members are called as
specialized members.

• If we think from the perspective to facilitate reusability in future, it would be a


good idea to define the generic members in a parent class (named as Person)
and keep only the specialized members in the child class Student,

• Such an inheritance with a single parent class and a single child class is called
single level inheritance
Single level inheritance
Hierarchical inheritance

This is a relationship of inheritance created with single parent class having


more than one child classes as shown in Figure
• As an example to understand ‘hierarchical inheritance’,

• let us modify the previous design to store data about professors of the
university along with students.

• Hence, we now create two child classes Student and Professor of class Person.

• Each of the child classes can define specialized members which are not
common among the peer classes, like class Student defines roll_no and marks
and Professor defines prof_id (to store unique identifier for each professor)
and salary (to store salary of the professor).
hierarchical inheritance
Multi-level inheritance

This is a relationship of inheritance created when a class is derived from a


child class of some other class.
• As an example to understand ‘multi-level inheritance’, let us modify the design
to store data about ex-students of the university.

• Ex-Students would represent the set of students who have successfully


completed their course, and hence every object of class Ex_student would have a
‘final grade’ along with ‘year of passing’.

• For an ex-student we would also require to store name, dob, gender, roll_no, and
marks, which are actually the members present when he/she was a student.

• Hence, we create class Ex_Student as a child class of Student because


Ex_Student requires all the members and member functions of class Student
along with a few additional members
An example of multi-level inheritance
Multiple inheritance

This is a relationship of inheritance created using multiple parent classes and


single child class
• To understand this type of inheritance, let us imagine a person in the real world
who is both student as well as professor in the same university.

• We designate this category as assistant-professors and create a class named as


AssistantProf to represent them.

• Class AssistantProf should have member’s prof_id, salary because he/she is a


professor for undergraduate courses and also have members roll_no, marks
because he/she is also a student for the postgraduate course.

• AssistantProf requires members of Student as well as Professor, it must have two


parent classes.

• This example illustrates multiple inheritance because AssitantProf is a single child


of two parent classes Student and Professor.
An example of multiple inheritance
Hybrid inheritance

• Combining different inheritance types into a single design is called as hybrid


inheritance.

• The hierarchy is the mixture of multi-level inheritance, multiple inheritance and


hierarchical inheritance

• The pattern represents that a Person can either be a Student or a Professor.

• And a Person who is both Student as well as Professor is called as an


AssistantProf.
Hybrid inheritance
To understand IS-A and HAS-A relationship between classes, let us create a
C++ program to implement the hierarchy, which is defined in Figure
• The hierarchy depicts that there are two types of Person objects, a Person can
either be a Student or a Professor.

• There are some members such as name, gender, and date of birth, which are
relevant for both Student as well as Professor,

• hence we define these members in a parent class Person and create Student
and Professor as child classes of Person.

• Note that one of the members of class Person is used to represent ‘date of
birth’ (dob) which in itself is a composite attribute.

• To store date of birth, we actually need to store three different members dd


(day), mm (month), and yy (year). Hence, we create another class named as
Date which contains the members dd, mm, and yy
IS-A and HAS-A relationship
The first statement in main() function
Student s1;
creates an object of class Student named as s1.

This object can store data about one Student in the real world.

As Student is a child class of Person, the object memory of s1 will contain


members name, dob, and gender inherited from Person along with the
specialized member variables (roll_no and marks)

The member functions inputPerson(), outputPerson(), and calculateAge()


will also be inherited by class Student and can be accessed directly within
the child class as they are defined as public functions in Person.
Object s1
The statement
s1.inputStudent();
invokes the function inputStudent(); using object s1. Since the function is defined
in Student the control of execution is transferred within the scope of class Student

The statement
s1.outputStudent(); will print all the member values of Student object.

The function outputStudent() calls outputPerson() to print name and gender, then
calls calculateAge() to print the age of the Student.

calculateAge() invokes the getDateDiff() function in class Date which gives the
difference between the current date and the date of birth.
Multi-level Inheritance: Calculator

As an example of multi-level inheritance, let us create a C++ program to design


different types of calculators

Basic calculator: This calculator provides only arithmetic functions like addition,
subtraction, multiplication and division with two variables.

Trigonometric calculator: This calculator provides all the features of basic calculator
along with the additional functions to calculate sine, cosine, and tangent of an angle.
we will create the Trigonometric calculator class as a child class of class Basic
Scientific calculator:
This calculator provides all the features of Basic and Trigonometric class along
with the additional functions to calculate natural logarithm and log to the base 10.

It is sufficient to create Scientific calculator as a child class of Trigonometric alone.

This is because the Trigonometric calculator anyways inherits the features of Basic
calculator which will be passed on to Scientific calculator.

Hence we just create the Scientific calculator class as a child class of Trigonometric
calculator.
Hierarchy of classes
C++ syntax to create hierarchy of Basic, Trigonometric and Scientific classes

You might also like