A6 Inheritance
A6 Inheritance
Page 72
Inheritance in Object Oriented Programming
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
class Person
{
//code statements go here
};
class Student: public Person
{
//code statements go here
};
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.
Page 73
Inheritance in Object Oriented Programming
3. Concept Map
Inheritance is one of the most important building blocks of OOP. The concept of reusable classes is helpful to manage
objects. You can create new classes (derived classes) that are able to inherit certain attributes and behavior of their
ancestor or base class(es). Prior to working with inheritance, it is important to consider the following aspects of
inheritance.
• 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.
Design a class named Person and its two derived classes named Student and Teacher.
Draw UML diagram for each class and show inheritance relationship among all classes.
Page 74
Inheritance in Object Oriented Programming
In the main() function make three objects of class Trigon while considering the shapeName as “threeangle” for
each object. In addition, it is required to invoke all the functions of Trigon class.
5.2. Task-2
The BankAccount class has the following attributes i.e.
• accountNumber, accountHolderName, and balance
• A parameterized constructor to initialize data fields with user-defined values
Create two derived classes i.e. CheckingAccount and SavingsAccount. Both these classes have
• A three argument constructor to initialize base class’s data fields
• Two functions i.e. deposit(amount) to deposit certain amount and witdraw(amount) to withdraw certain
amount
The withdraw function of CheckingAccount class has an overdraft limit but SavingsAccount cannot be overdraft.
In the main() function, create an object of both SavingAccount and CheckingAccount class. Moreover, test the
functionality of their respective deposit(amount) and withdraw(amount) functions while passing different amounts
as parameter.
6.1 Tools
Visual Studio 2012.
In the source file, which is created in the project “Inheritance” write following C++ code:
Page 75
Inheritance in Object Oriented Programming
.
Figure 1: Base and Derived Classes
6.3.2 Compilation
After writing the code, compile your code according to the guidelines mentioned. Remove any errors and warnings
that are present in your code.
Page 76
Inheritance in Object Oriented Programming
7. Practice Tasks
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
• Appropriate constructors to initialize data fields of base class
• A function named amountWithdrawn(amount) to withdraw certain amount while taken into account the
following conditions
o While withdrawing from current account, the minimum balance should not decrease Rs. 5000
o While withdrawing from savings account, the minimum balance should not decrease Rs. 10,000
• amountDeposit(amount) to deposit amount in the account
In the main() function, create instances of derived classes (i.e. CurrentAccount and SavingsAccount) and invoke
their respective functions to test their working.
Page 77
Inheritance in Object Oriented Programming
7.3 Outcomes
After completing this lab, student will be able to understand the implementation and design of Inheritance in C++.
7.4 Testing
8. Further Reading
8.1. Books
• Object-Oriented Programming Using C++; Joyce Farrell, Fourth Edition
8.2. Slides
• The slides and reading material will be provided by course instructor.
Page 78