Week 13
Week 13
• The derived class is called child class, and the class from which it is derived is
called parent class.
• 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
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
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
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.
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:
2. Hierarchical Inheritance
3. Multi-level Inheritance
4. Multiple Inheritance
5. Hybrid Inheritance
Single level inheritance
• 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.
• Such an inheritance with a single parent class and a single child class is called
single level inheritance
Single level inheritance
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
• 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.
• 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.
This object can store data about one Student in the real world.
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
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.
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