Lab 10

You might also like

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

Object Oriented Programming Lab (CS1022L)

Lab Manual (Lab # 10)

Session: Spring ’24


Instructor: Jaweria Aslam

Department of Computer Science


School of Systems and Technology UMT Lahore Pakistan
Objective: (Mapping CLO 03+4 + CLO 01)
• Friend Functions- Friend classes
• C++ Polymorphism

Friend Functions- Friend classes:


Data hiding is a fundamental concept of object-oriented programming. It restricts
the access of private members from outside of the class.
Similarly, protected members can only be accessed by derived classes and are
inaccessible from outside. For example,

However, there is a feature in C++ called friend functions that break this rule and
allow us to access member functions from outside the class.
A friend function can access the private and protected data of a class. We declare a
friend function using the friend keyword inside the body of the class.
Example 1: Working of friend Function
C++ program to demonstrate the working of friend function.
Used the word const:

Friend classes:
We can also use a friend Class in C++ using the friend keyword. For example,
hen a class is declared a friend class, all the member functions of the friend class
become friend functions.
Since ClassB is a friend class, we can access all members of ClassA from inside
ClassB.
However, we cannot access members of ClassB from inside ClassA. It is because
friend relation in C++ is only granted, not taken.
C++ Polymorphism:
Polymorphism is an important concept of object-oriented programming. It simply
means more than one form. That is, the same entity (function or operator) behaves
differently in different scenarios. For example,
The + operator in C++ is used to perform two specific functions. When it is used
with numbers (integers and floating-point numbers), it performs addition.

We can implement polymorphism in C++ using the following ways:


• Function overloading
• Operator overloading
• Function overriding
• Virtual functions

Virtual functions:
Using virtual functions in the base class ensures that the function can be overridden
in these cases.

Thus, virtual functions actually fall under function overriding. For example,
C++ Function Overriding:
When we call the function using an object of the derived class, the function of the
derived class is executed instead of the one in the base class.
So, different functions are executed depending on the object calling the function.
This is known as function overriding in C++.

Here, we have used a print() function in the Base class and the same function in the
Derived class
When we call print() using the Derived object derived1, it overrides the print()
function of Base by executing the print() function of the Derived class.

It's a runtime polymorphism because the function call is not resolved by the
compiler, but it is resolved in the runtime instead.
LAB Task
Task#1: Create a class named Shape that includes
• Two data member named width and height
• A virtual function named compute_area() to calculate and display the area

Derive a class named Triangle inherited from Shape class that contains
• A parameterized constructor to initialize the inherited data members
• Overridden function compute_area() to compute and display the area of a triangle
(0.5*width*height)

Derive a class named Rectangle inherited from Shape class that contains
• A parameterized constructor to initialize the inherited data members
• Overridden function compute_area() to compute and display the area of a rectangle
(width*height)

Write a main() function that instantiates objects of derived classes to call respective
compute_area() function using dynamic or late binding.

LAB Task#2: Create a class named Person, which contains


• Two data fields i.e. personName and age
• A pure virtual function named print()

A class named Patient inherits Person class, which contains


• Two data fields i.e. diseaseType and recommendedMedicine
• A parameterized constructor to initialize its own data members as well as the inherited
data members
• Overridden function print() to display all details relevant to a patient

A class named MedicarePatient inherited from class Patient, which holds


• A data field representing the name of the hospital
• A data filed representing the name of the ward
• A data field representing room number
• A parameterized constructor to initialize its own data members as well as the inherited
data members
• Overridden function print() to display all details relevant to a patient

In the main() function, create instances of derived classes to call respective print() function using
dynamic or late binding.
Task#3: Consider an abstract class Computer having
• Two fields (i.e. companyName, price) and
• A single function named show()

A class named Desktop inherits Computer class and adds fields representing
• color, monitor size, and processor type and
• A parameterized constructor to initialize its own data members as well as the inherited
data members
• Override function named show() to display values of its all attributes

A class named Laptop inherits Computer class and adds fields representing
• color, size, weight, and processor type and
• A parameterized constructor to initialize its own data members as well as the inherited
data members
• Override function named show() to display values of its all attributes

Write a main() function that instantiates objects of derived classes to call respective show()
function using dynamic or late binding.

You might also like