Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Lab 10

Download as pdf or txt
Download as pdf or txt
You are on page 1of 19

CC-211L

Object Oriented Programming

Laboratory 10

Inheritance - II

Version: 1.0.0

Release Date: 23-03-2023

Department of Information Technology


University of the Punjab
Lahore, Pakistan
CC-211L Object Oriented Programming FALL 2023

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

Laboratory 10 – Inheritance - II Page 2 of 19


CC-211L Object Oriented Programming FALL 2023

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

Saad Rahman bsef19m021@pucit.edu.pk


Teacher Assistants
Zain Ali Shan bcsf19a022@pucit.edu.pk

Laboratory 10 – Inheritance - II Page 3 of 19


CC-211L Object Oriented Programming FALL 2023

Background and Overview:


Inheritance :
Inheritance is one of the core concepts of object-oriented programming (OOP) languages. It is a
mechanism where you can derive a class from another class for a hierarchy of classes that share a set of
attributes and methods.

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.

Laboratory 10 – Inheritance - II Page 4 of 19


CC-211L Object Oriented Programming FALL 2023

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:

Fig. 01 (Constructor and Destructor in Single Inheritance)

Output:

Fig. 02 (Constructor and Destructor in Single Inheritance)

Laboratory 10 – Inheritance - II Page 5 of 19


CC-211L Object Oriented Programming FALL 2023

Single Inheritance with arguments:


The derived class is responsible for calling the constructor of the base class. in case of constructor of
the base class with arguments, the syntax to define constructor of the derived class is different. The
base class constructor is connected with the derived class constructor by using the colon (:) in the
header of the derived class constructor. The parameters of the base class constructor are also given as
parameter list in the derived class constructor. In derived class constructor, the parameters of the base
class come first than the derived class constructor.

Example:

Fig. 03 (Constructor and Destructor in Single Inheritance with arguments)


Output:

Fig. 04 (Constructor and Destructor in Single Inheritance with arguments)

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

Laboratory 10 – Inheritance - II Page 6 of 19


CC-211L Object Oriented Programming FALL 2023

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:

Fig. 05 (Constructor and Destructor in Multiple Inheritance)

Output:

Fig. 06 (Constructor and Destructor in Multiple Inheritance)

Multiple Inheritance with arguments:


The constructor of the derived class calls the constructors of the base classes in the same order in
which they are specified in the header of the derived class. The syntax define constructors in multiple
inheritance with arguments is also like the definition of single inheritance with arguments. The
constructors of the base classes relate to the constructor of the derived class by using colon (:) and
separated by commas.

Laboratory 10 – Inheritance - II Page 7 of 19


CC-211L Object Oriented Programming FALL 2023

Example:

Fig. 07 (Constructor and Destructor in Multiple Inheritance with arguments)


Output:

Fig. 08 (Constructor and Destructor in Multiple Inheritance with arguments)

Laboratory 10 – Inheritance - II Page 8 of 19


CC-211L Object Oriented Programming FALL 2023

Task 01: Car Manufacturing [Estimated time 25 minutes / 20 marks]


You are working on a software project for a car manufacturing company. The project involves designing
a class hierarchy to represent different types of cars. You are required to implement:

 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:

 Initialize the member variables of each class appropriately in the constructors


 Ensure that any resources allocated in the constructors are properly released in the destructors
 Demonstrate how the constructors and destructors work in the context of inheritance, for example,
by creating objects of the derived classes and calling their constructors and destructors and showing
how the base class constructor and destructor are also called

Laboratory 10 – Inheritance - II Page 9 of 19


CC-211L Object Oriented Programming FALL 2023

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 {
.... ... ....
};

class Derived : public 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:

Fig. 09 (public Inheritance)

Laboratory 10 – Inheritance - II Page 10 of 19


CC-211L Object Oriented Programming FALL 2023

Output:

Fig. 10 (public Inheritance)

Since private and protected members are not accessible from main(), we need to create public
functions getPVT() and getProt() to access them.

Accessibility:

Accessibility Private Members Protected Members Public Members


Base Class Yes Yes Yes
Derived Class No Yes Yes

Protected Inheritance:
Protected inheritance makes the public and protected members of the base class protected in the derived
class.
Example:

Fig. 11 (protected Inheritance)

Laboratory 10 – Inheritance - II Page 11 of 19


CC-211L Object Oriented Programming FALL 2023

Output:

Fig. 12 (protected Inheritance)


As we know, protected members cannot be directly accessed from outside the class. As a result, we
cannot use getPVT() from ProtectedDerived. That is why we need to create the getPub() function in
ProtectedDerived in order to access the pub variable.

Accessibility:

Accessibility Private Members Protected Members Public Members


Base Class Yes Yes Yes
Yes (Inherited as
Derived Class No Yes
protected variable)

Private Inheritance:
Private inheritance makes the public and protected members of the base class private in the derived
class.

Example:

Fig. 13 (private Inheritance)

Laboratory 10 – Inheritance - II Page 12 of 19


CC-211L Object Oriented Programming FALL 2023

Output:

Fig. 14 (private Inheritance)

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:

Accessibility Private Members Protected Members Public Members


Base Class Yes Yes Yes
Yes (Inherited as Yes (Inherited as
Derived Class No
private variable) private variable)

Relationship among objects in an Inheritance Hierarchy:


Invoking Base-Class Functions from Derived-Class Objects:
A derived class can invoke a function of its base class by using the scope resolution operator (::) and
the name of the function. This is useful when the derived class wants to call a function that is
implemented in the base class, or to access a member variable that is defined in the base class.

Example:

Fig. 15 (Relationship among objects)

Laboratory 10 – Inheritance - II Page 13 of 19


CC-211L Object Oriented Programming FALL 2023

Output:

Fig. 16 (Relationship among objects)

Derived-Class Member-Function Calls via Base-Class Pointers:


Off a base-class pointer, the compiler allows us to invoke only base-class member functions. Thus, if
a base-class pointer is aimed at a derived-class object, and an attempt is made to access a derived-
class-only member function, a compilation error will occur.

Example:

Fig. 17 (Relationship among objects)

Error:

Fig. 18 (Relationship among objects)

Laboratory 10 – Inheritance - II Page 14 of 19


CC-211L Object Oriented Programming FALL 2023

Task 01: Animals [Estimated time 30 minutes / 20 marks]

 Create a base class called ‘Animal’ with a private data member


o name
 Add a constructor to the Animal class that initializes the name data member
 Create two derived classes from the Animal class called ‘Mammal’ and ‘Bird’
 The Mammal class should have a private data member
o numLegs
o a constructor that initializes the name and numLegs data members
 The Bird class should have a private data member
o wingSpan
o a constructor that initializes the name and wingSpan data members
 Create two additional derived classes from the Mammal class called ‘Dog’ and ‘Cat’
 The Dog class should have a private data member
o breed
o a constructor that initializes the name, numLegs, and breed data members
 The Cat class should have a private data member
o color
o a constructor that initializes the name, numLegs, and color data members
 Finally, create a new main() function that creates objects of all four classes (Mammal, Bird, Dog,
and Cat) and test the constructors and destructors
 Make sure to test how the constructors and destructors of each class are called when creating and
deleting objects

Task 02: Student Data [Estimated time 30 minutes / 20 marks]

 Define a class ‘Student’ as a base class with data members


o admissionNo,
o Name
o age and
o address as protected type and
o getStudent() function for setting values for data members
 These members and functions are common to both derived classes
 Make two derived classes of Student class as ‘Undergraduate’ and ‘Graduate’ each containing
data member
o degree_program in which student is enrolled and
o member functions getdegree() to set degree_program
o how() to display whole information
 Output should be as follows:

Laboratory 10 – Inheritance - II Page 15 of 19


CC-211L Object Oriented Programming FALL 2023

Fig. 19 (In-Lab Task)

Task 03: Employee [Estimated time 30 minutes / 20 marks]


 Create a class of ‘Employee’ having data member
o employee ID
o name, and
o designation
 This class should have at least two member functions. One function takes input from users and
update the data members. The second function should display the value of data members
 Design another class ‘Salary’ that has five data members
o basic pay
o human resource allowance
o dearness allowance
o profitability fund, and
o net pay.
 This class should have following functions:
o getEmployeeDetails() that take input from users in all the data members and calculate net pay
using formula:

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

Laboratory 10 – Inheritance - II Page 16 of 19


CC-211L Object Oriented Programming FALL 2023

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.

Laboratory 10 – Inheritance - II Page 17 of 19


CC-211L Object Oriented Programming FALL 2023

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]

References and Additional Material:


 Inheritance
https://www.programiz.com/cpp-programming/inheritance
 Base and Derived Classes
https://learncplusplus.org/learn-c-inheritance-base-classes-and-derived-classes/
 Constructor and Destructor in Derived Classes
https://www.geeksforgeeks.org/order-constructor-destructor-call-c/
 public, protected, and private Inheritance
https://www.programiz.com/cpp-programming/public-protected-private-inheritance

Laboratory 10 – Inheritance - II Page 18 of 19


CC-211L Object Oriented Programming FALL 2023

Lab Time Activity Simulation Log:


 Slot – 01 – 00:00 – 00:15: Class Settlement
 Slot – 02 – 00:15 – 00:40: In-Lab Task
 Slot – 03 – 00:40 – 01:20: In-Lab Task
 Slot – 04 – 01:20 – 02:20: In-Lab Task
 Slot – 05 – 02:20 – 02:45: Evaluation of Lab Tasks
 Slot – 06 – 02:45 – 03:00: Discussion on Post-Lab Task

Laboratory 10 – Inheritance - II Page 19 of 19

You might also like