Lab Manual 07 -Inheritance and Its Types (1)
Lab Manual 07 -Inheritance and Its Types (1)
Lab- 07 Manual
Lab Instructor: Riaz Ahmad
Department of Computer Science
Email: [email protected]
Lab: 07 Table of Contents
Following are the topics that we will cover in this Lab.
Introduction
Objective of the Experiment
Concept Map
Inheritance
Generalization
Specialization
Constructor and Destructor
Examples
Different types of inheritance
Inheritance types implementation Examples
Practice Tasks
Modeled an average
Able to partially Partially able to identify Not completely able
solution for a given
Average understand the objects and their to explain
problem using object-
problem relationships experimental work
oriented principles
Able to
Thoroughly Identify the Modeled a proper explain/defend the
Fully Understand objects & their solution for a given assigned task and all
Very Good
the problem relationships to build problem using object- the questions have
object-oriented solution oriented principles been answered
correctly
Objective:
To cover
Inheritance
Single Inheritance
Multiple Inheritance
Multi-Level Inheritance
Hybrid Inheritance
Hierarchical Inheritance
Scope:
The student should know the following at the end of this lab:
Problem understanding
Problem Solving
Theory: [CLO 1]
Introduction [CLO 1]
While considering the advantages of commonality between classes, in C++, you are able to
manage your classes in a way that a class (child/sub class) can extend the functionality by
inheriting the attributes and behaviors of an existing class commonly known as base/super class.
In simple words, you can define a class with certain data fields and/or member functions and
then identify other class(es) that can share these data fields and functions. In typical C++
inheritance, a class or classes known as derived class(es), subclass(es) or child class(es) is/are
able to inherit certain attributes and behavior of pre-existing class(es) that are known as base
class(es), superclass(es), or parent class(es). In practice, base classes are more general while
derived classes are specialized version of base classes and due to this reason, it is said that
inheritance maintains generalization and specialization. With inheritance, you can greatly
save the time and lessen the effort to write duplicate code.
Concerning inheritance syntax in C++, if you have a base class named person inherited by a
derived class named student then you have to write code in the following way.
In case of derived class’s object instantiation, default/no-argument constructor for the base
class(es) are automatically called first and are followed by calling derived class’s constructors.
If base class(es) is/are without default/no-argument constructor, then it is must to call explicitly
any base class’s constructor even if there is no need to call a constructor.
Inheritance represents “is-a” relationship in OOP i.e. any specific object is a type of a
more general class of object. For example, Train is a type of Vehicle
By design, a derived class should inherit all attributes and behaviors of its base class(es)
While declaring its own data fields a derived class can extend the features of its base
class(es)
While defining new functionality, a derived class can modify the behavior provided by
the base class(es)
Inheritance in OOP saves a lot of work and time. You can save additional work as some of the
object definitions(class) already exists. Time saving is due to the reason that much of the code
has already been written and tested.
If a class B inherits from class A then it contains all the characteristics (information structure and behavior
of class A.
The parent class is called base class and the child class is called derived class Besides inherited
characteristics, derived class may have its own unique characteristics
Examples:
Here,
Student IS A Person
Teacher IS A Person
Doctor IS A Person
Here,
Circle IS A Shape
Line IS A Shape
Triangle IS A Shape
Generalization
In OO models, some classes may have common characteristics. We extract these features into a
new class and inherit original classes from this new class. There are many objects with common
characteristics in object model. The common characteristics (attributes and behaviour) of all
these objects are combined in a single general class. Base class encapsulates the idea of
commonality of derived classes. Base class is general class representing common behaviour of
all derived classes.
This concept is known as Generalization.
It reduces the redundancy and gives us reusability, using generalization our solution becomes less
complex.
In generalization there should be “Is a Kind of Relationship” (also called “Is A relationship”)
between base and child classes.
Examples: Line,Circle,Triangle
Common attributes
Color vertices
Common behaviour
Set Color, Move
Sub-typing (Extension)
Sub-typing means that derived class is behaviorally compatible with the base class Derived class has all the
characteristics of base class plus some extra characteristics Behaviorally compatible means that base class can be
replaced by the derived class.
Note: Every object of derived class has an anonymous object of base class
Constructors
The anonymous object of base class must be initialized using constructor of base class
When a derived class object is created the constructor of base class is executed before
the constructor of derived class
Note:
The base class’s constructor is called before the derived class’s constructor. The destructors are
called in reverse order, with the derived class’s destructor being called first.
If default constructor of base class does not exist then the compiler will try to generate a
default constructor for base class and execute it before executing constructor of derived
class
If the user has given only an overloaded constructor for base class, the compiler will not
generate default constructor for base class
Example:
class Parent{
public:
Parent(int i)
{}
};
class Child : public
Parent{ public:
Child(){}
} Child_Object; //ERROR
Default constructor:
Default constructor is such constructor which either has no parameter or if it has some parameters these
have default values. The benefit of default constructor is that it can be used to create class object without
passing any argument.
We can avoid this error by calling base class non-default constructor in derived class
constructor initializer list by ourself.
Examples:
class
Parent{ pub
lic:
Parent(int i){…};
};
class
Person{ pu
blic:
int age;
char *name;
...
public:
Person();
};
Example
class Student: public
Person{ private:
int semester;
...
public:
Student(int a):age(a) //error
{
}
};
Reason
• It will be an assignment not an initialization
int main()
{
B b(4,5);
cout<<" B class";
b.Display();
b.show();
return 0;
}
return 0;
}
cout<<"\nThe B
class"; b.Display();
b.show();
cout<<" \n\n C
class"; c.Display();
c.show()
; return
0;
}
Hybrid Inheritance:
public: public:
C(int j,int i):A(i) D(int j,int k,int i,int l,int m):B(k,i),C(l,m)
{ {
c=j; d=j;
} }
void Display() void Display()
{ {
A::Display(); cout<<"\nData from B to A";
cout<<"\nThe Value of c:"<<c; B::Display();
cout<<"\n\nData from C to A";
} C::Display();
}; cout<<"\nThe Value of d:"<<d;
}
};
int main(){
D d(3,4,5,6,7);
d.Disp
lay();
} return
0;
Employee class has two data fields i.e. a name (of type string) and specific
empID (of type integer)
Both classes (HourlyEmployee and PermanentEmployee) have an attribute
named hourlyIncome
Both classes (HourlyEmployee and PermanentEmployee) have three-argument
constructor to initialize the hourlyIncome as well as data fields of the base class
Class HourlyEmployee has a function named calculate_the_hourly_income to
calculate the income of an employee for the actual number of hours he or she
worked. One hour income is Rs. 150
Similarly, PermanentEmployee class has function named calculate_the_income
to calculate the income of an employee that gets paid the salary for exact 240
hours, no matter how many actual hours he or she worked. Again, one hour
salary is Rs. 150.
Implement all class definitions with their respective constructors to initialize all data
members and functions to compute the total income of an employee. In the main()
function, create an instance of both classes (i.e. HourlyEmployee and
PermanentEmployee) and test the working of functions that calculate total income of
an employee.
Derive two classes from the BankAccount class i.e. CurrentAccount and the
SavingsAccount. Both classes (CurrentAccount and SavingsAccount) inherit all
attributes/behaviors from the BankAccount class. In addition, followings are
required to be the part of both classes