[go: up one dir, main page]

0% found this document useful (0 votes)
12 views7 pages

A6 Inheritance

The document discusses inheritance in Object-Oriented Programming (OOP), particularly in C++, emphasizing its role in code reusability and the relationship between base and derived classes. It outlines the syntax for implementing inheritance, the objectives of a related lab experiment, and provides practice tasks to reinforce the concepts learned. Additionally, it highlights the importance of understanding constructors, destructors, and the generalization-specialization relationship in class design.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views7 pages

A6 Inheritance

The document discusses inheritance in Object-Oriented Programming (OOP), particularly in C++, emphasizing its role in code reusability and the relationship between base and derived classes. It outlines the syntax for implementing inheritance, the objectives of a related lab experiment, and provides practice tasks to reinforce the concepts learned. Additionally, it highlights the importance of understanding constructors, destructors, and the generalization-specialization relationship in class design.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Object-Oriented Programming

Inheritance in Object Oriented Programming

Page 72
Inheritance in Object Oriented Programming

Inheritance in Object Oriented Programming


1. Introduction
Object-Oriented Programming (OOP) strongly supports reusability and in this regard inheritance is perhaps the
strongest way of reusing the features of a class i.e. attributes (data member) and behaviors (functions). Following the
OOP paradigm, C++ allows the inheritance among classes so that the characteristic(s) of one class is/are inherited by
the other class(es). Hence, you can say that the derived classes receive some their attributes from which the derived
class is inherited. A major advantage of inheritance is that it allows the reusability of code. Hence, the programmer
can simply create new (child) class(es) by using other (parent) class(es).

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.

Regarding syntax, other details are given in the section 6.3.

Relevant Lecture Readings:


• Textbook: Object-Oriented Programming in C++; Author: Robert Lafore, Fourth Edition
o Pages: 372 – 428

Page 73
Inheritance in Object Oriented Programming

2. Objective of the Experiment


After completing this lab, the student should be able to:
• get basic understanding of inheritance concept in OOP
• derive class(es) from other base class(es) through inheritance
• understand the constructor and destructor chaining in inheritance hierarchy
• know how to invoke the base class’s constructors within derived class(es)

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.

4. Homework before Lab


4.1. Problem Solution Modeling
Draw the UML diagram for the following task. You are required to bring this design with you and submit to
your lab instructor.
4.2. Problem description:

Design a class named Person and its two derived classes named Student and Teacher.

The Person class has


• Attributes i.e. name, age, phone number, and e-mail address
• Behavior i.e. work something

The Student class has


• Attributes i.e. registration ID, department, number of completed credit hours
• Behavior i.e. pay course registration fee

The class named Teacher contains the following


• Attributes i.e. staff ID, salary
• Behavior i.e. teach course(s)

Draw UML diagram for each class and show inheritance relationship among all classes.

Page 74
Inheritance in Object Oriented Programming

5. Practices from Home


5.1. Task-1
Create a class named Trigon inherited from the class GeometricShape (containing a single data member named
shapeName (a string) and a member function named setShapeName(shapeName). The class Trigon is required to
hold:
• Three data members i.e. base, perpendicular, and hypotenuse of type double
• A no-arg constructor that initializes all data members with value 1.0
• A parameterized constructor to initialize all data fields with user-defined values
• The setter functions for all three data fields
• The accessor functions for all three data fields
• A function named displayArea() that shows the area of a certain Trigon object

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. Procedure & Tools

6.1 Tools
Visual Studio 2012.

6.2 Setting-up Visual Studio 2012


Setup visual studio and make a project named “Inheritance”

6.3 Walkthrough Task


In this task, you are required to write a C++ code which would elaborate inheritance in OOP. The following lines
show the output of basic inheritance concept.

6.3.1 Writing Code

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

Figure 2: Main function for classes of Figure 1

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.

6.3.3 Executing the Program


A sample output after running the program is shown below. Also run the code with other possible inputs.

Page 76
Inheritance in Object Oriented Programming

Figure 3: Final Output

7. Practice Tasks

7.1 Practice Task 1


Consider a base class named Employee and its derived classes HourlyEmployee and PermanentEmployee while
taking into account the following criteria.
• 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.

7.2 Practice Task 2


Consider a class BankAccount that has
• Two attributes i.e. accountID and balance and
• A function named balanceInquiry() to get information about the current amount in the account

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

For both Practice Tasks, lab instructor must examine

• the implementation of above mentioned classes


• the creation of instances of specified classes
• the invocation of specified functions

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

You might also like