Lab 10
Lab 10
Lab 10
Laboratory 10
Inheritance - II
Version: 1.0.0
Contents:
Learning Objectives
Required Resources
General Instructions
Background and Overview
o Inheritance
o Base Class
o Derived Class
o Constructor
o Destructor
Activities
o Pre-Lab Activity
Constructor and Destructor in Derived Classes
– Single Inheritance
– Single Inheritance with Arguments
– Multiple Inheritance
– Multiple Inheritance with Arguments
Task 01
o In-Lab Activity
public, protected, and private Inheritance
– Public Inheritance
– Protected Inheritance
– Private Inheritance
Relationship among objects in an Inheritance Hierarchy
– Invoking Base-Class Functions from Derived-Class Objects
– Derived-Class Member-Function Calls via Base-Class Pointers
Task 01
Task 02
Task 03
o Post-Lab Activity
Task 01
Submissions
Evaluations Metric
References and Additional Material
Lab Time and Activity Simulation Log
Learning Objectives:
Constructors in Derived Classes
Destructors in Derived Classes
Public protected and private Inheritance
Relationship among objects in Inheritance
Resources Required:
Desktop Computer or Laptop
Microsoft ® Visual Studio 2022
General Instructions:
In this Lab, you are NOT allowed to discuss your solution with your colleagues, even not
allowed to ask how is s/he doing, this may result in negative marking. You can ONLY discuss
with your Teaching Assistants (TAs) or Lab Instructor.
Your TAs will be available in the Lab for your help. Alternatively, you can send your queries
via email to one of the followings.
Teachers:
Course Instructor Prof. Dr. Syed Waqar ul Qounain swjaffry@pucit.edu.pk
Lab Instructor Azka Saddiqa azka.saddiqa@pucit.edu.pk
Base Class:
A base class is an existing class from which the other classes are determined, and properties are
inherited. It is also known as a superclass or parent class. In general, the class which acquires the base
class can hold all its members and some further data as well.
Derived Class:
A derived class is a class that is constructed from a base class or an existing class. It tends to acquire all
the methods and properties of a base class. It is also known as a subclass or child class.
Constructor:
A constructor in C++ is a special method that is automatically called when an object of a class is created.
Destructor:
A destructor is a member function that is invoked automatically when the object goes out of scope or is
explicitly destroyed by a call to delete.
Activities:
Pre-Lab Activities:
Constructor and Destructor in Derived Class:
Single Inheritance:
When an object of the derived class is created, a part of the base class is also included with that
object. The base class is initiated to include the base class part with the object of the derived class and
then the derived class part is included. So, when the derived class object is created, the constructor of
the base class is automatically executed first and then the constructor of the derived class is executed.
The base class constructors are not inheritance by derived class. however, a derived class constructor
always calls the constructor for its base class first to initialize base class data members for the objects
of the derived class. If the derived class constructor is omitted, the derived class default constructor
calls the base class constructor.
Destructors are called in the reverse order of constructor calls. So, a derived class destructor is called
its base class destructor.
Example:
Output:
Example:
Multiple Inheritance:
In multiple inheritance, the constructors of the base classes and constructors of the derived class are
automatically executed when an object of the derived class is created. The constructors of the base
classes are executed first and then the constructor of the derived class is executed. The constructors of
base classes are executed in the same order in which the base classes are specified in derived class
(i.e., from left to right).
Similarly, the destructors are executed in reverse order, i.e., derived class constructor is executed first
and then constructors of the base classes are executed (from right to left).
Example:
Output:
Example:
A base class ‘Car’ and two derived classes ‘ElectricCar’ and ‘GasolineCar’
The ‘ElectricCar’ class will have a member variable ‘batteryLife’ to represent the battery life of
the car
The ‘GasolineCar’ class will have a member variable ‘fuelTankCapacity’ to represent the fuel
tank capacity of the car
Your task is to implement the constructors and destructors for each class in such a way that the member
variables are initialized properly and all allocated resources are released when the objects are destroyed.
Specifically, your implementation should:
In-Lab Activities:
public, protected, and private Inheritance:
In C++ inheritance, we can derive a child class from the base class in different access modes. E.g.,
class Base {
.... ... ....
};
Notice the keyword public in the code ‘class Derived : public Base’
This means that we have created a derived class from the base class in public mode. Alternatively, we
can also derive classes in protected or private modes. These 3 keywords (public, protected, and
private) are known as access specifiers in inheritance.
Public Inheritance:
Public inheritance makes public members of the base class public in the derived class, and the
protected members of the base class remain protected in the derived class.
Example:
Output:
Since private and protected members are not accessible from main(), we need to create public
functions getPVT() and getProt() to access them.
Accessibility:
Protected Inheritance:
Protected inheritance makes the public and protected members of the base class protected in the derived
class.
Example:
Output:
Accessibility:
Private Inheritance:
Private inheritance makes the public and protected members of the base class private in the derived
class.
Example:
Output:
As we know, private members cannot be directly accessed from outside the class. As a result, we
cannot use getPVT() from PrivateDerived. That is why we need to create the getPub() function in
PrivateDerived in order to access the pub variable.
Accessibility:
Example:
Output:
Example:
Error:
Net pay = basic pay + human resource allowance + dearness allowance - profitability fund
This function, at the start, should invoke the function of employee class that takes input
from the user.
o Display() that first invokes display function of employee class and then display the data
members of Salary class (including net pay)
Design another class named ‘BankCredit’ that has two data members:
o bank name and
o account number.
It should have two member functions as:
o getBankDetails() that first invokes the getter method of Salary class and then takes input from
user for bank name and account number
o Print() that first invokes display method of Salary class and then display the data members of
BankCredit class
In main(), declare array of objects of BankCredit class with size 5. It should then take input from
the user about the number of employees. It should then invoke getBankDetails() and print()
function for each employee
Post-Lab Activities:
Task 01: Restaurant Ordering System [Estimated time 60 minutes / 40 marks]
Design a class hierarchy for a restaurant ordering system. The system should have the following
requirements:
The restaurant offers different types of food items, such as appetizers, entrees, and desserts.
Each food item should have a name, description, and price.
The restaurant also offers different types of beverages, such as soft drinks, juices, and alcoholic
drinks.
Each beverage should have a name, description, and price.
The restaurant offers different types of discounts, such as percentage-based discounts and dollar-
based discounts.
The restaurant offers different types of payment methods, such as credit cards, debit cards, and
cash.
The ordering system should be able to calculate the total cost of an order, including any discounts
and taxes.
Your task is to design a class hierarchy that models these requirements, using appropriate inheritance.
You should also write code to demonstrate how the classes work together in the context of the ordering
system.
Submissions:
For In-Lab Activity:
Save the files on your PC.
TA’s will evaluate the tasks offline.
For Pre-Lab & Post-Lab Activity:
Submit the .cpp file on Google Classroom and name it to your roll no.
Evaluations Metric:
All the lab tasks will be evaluated offline by TA’s
Division of Pre -Lab marks: [20 marks]
Task 01: Car Manufacturing [20 marks]
Division of In-Lab marks: [60 marks]
Task 01: Animals [20 marks]
Task 02: Student Data [20 marks]
Task 03: Employee [20 marks]
Division of Post-Lab marks: [40 marks]
Task 01: Restaurant Ordering System [40 marks]