0% found this document useful (0 votes)
38 views

Lab 05

This document provides instructions for a lab on object-oriented programming concepts including friend functions, friend classes, the 'this' pointer, and static members. The lab contains pre-lab, in-lab, and post-lab activities to help students practice and demonstrate their understanding of these concepts. The pre-lab activities involve writing programs to add complex numbers and check if a number can be expressed as the sum of two prime numbers using friend functions. The in-lab activities explore friend classes, the 'this' pointer, static data members, static member functions, and include example code and tasks. The post-lab activity involves one final task to solidify learning.

Uploaded by

aimenzahid600
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views

Lab 05

This document provides instructions for a lab on object-oriented programming concepts including friend functions, friend classes, the 'this' pointer, and static members. The lab contains pre-lab, in-lab, and post-lab activities to help students practice and demonstrate their understanding of these concepts. The pre-lab activities involve writing programs to add complex numbers and check if a number can be expressed as the sum of two prime numbers using friend functions. The in-lab activities explore friend classes, the 'this' pointer, static data members, static member functions, and include example code and tasks. The post-lab activity involves one final task to solidify learning.

Uploaded by

aimenzahid600
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

CC-211L

Object Oriented Programming

Laboratory 05

Friend Classes Functions and static Members

Version: 1.0.0

Release Date: 03-01-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 Friend Function
o Friend Class
o “this” Pointer
o static Keyword
• Activities
o Pre-Lab Activity
▪ Friend Functions
▪ Task 01
▪ Task 02
o In-Lab Activity
▪ Friend Class
▪ ‘this’ Pointer
▪ static Data member
▪ static Member Function
▪ 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 05 – Friend Classes Functions and static Members Page 2 of 16


CC-211L Object Oriented Programming FALL 2023

Learning Objectives:
• Friend functions
• Friend Classes
• Use of “this” pointer
• static Data Members
• static Member Function

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 [email protected]
Lab Instructor Azka Saddiqa [email protected]

Saad Rahman [email protected]


Teacher Assistants
Zain Ali Shan [email protected]

Laboratory 05 – Friend Classes Functions and static Members Page 3 of 16


CC-211L Object Oriented Programming FALL 2023

Background and Overview:


Friend Function:
A friend function can be granted special access to private and protected members of a class in C++.
They are the non-member functions that can access and manipulate the private and protected members
of the class for they are declared as friends.
Friend Class:
A friend class can access private and protected members of other classes in which it is declared as a
friend. It is sometimes useful to allow a particular class to access private and protected members of
other classes. For example, a LinkedList class may be allowed to access private members of Node.
‘this’ Pointer:
The “this” pointer is a pointer accessible only within the non-static member functions of a class. It
points to the object for which the member function is called. Static member functions don't have a “this”
pointer.
static Keyword:
When static keyword is used, variable or data members or functions cannot be modified again. It is
allocated for the lifetime of program. Static functions can be called directly by using class name.
Static variables are initialized only once. Compilers persist the variable till the end of the program.
Static variable can be defined inside or outside the function. They are local to the block. The default
value of static variable is zero. The static variables are alive till the execution of the program.

Laboratory 05 – Friend Classes Functions and static Members Page 4 of 16


CC-211L Object Oriented Programming FALL 2023

Activities:
Pre-Lab Activities:
Friend Functions:
A friend function in C++ is a function that is declared outside a class but is capable of accessing the
private and protected members of the class. There could be situations in programming where we want
two classes to share their members. These members may be data members, class functions or function
templates. In such cases, we make the desired function, a friend to both these classes which will allow
accessing private and protected data of members of the class.
Generally, non-member functions cannot access the private members of a particular class. Once
declared as a friend function, the function is able to access the private and the protected members of
these classes.
Example:
The picture code below shows the declaration, definition and working of friend function

Fig. 01 (Working of a Friend Function)


Output:

Fig. 02 (Working of a Friend Function)

Laboratory 05 – Friend Classes Functions and static Members Page 5 of 16


CC-211L Object Oriented Programming FALL 2023

Example (Add Members of Two Different Classes using Friend Function):

Fig. 03 (Working of a Friend Function)


Output:

Fig. 04 (Working of a Friend Function)


Explanation:
In this program, Class A and Class B have declared add( ) as a friend function. Thus, this function
can access private data of both classes. One thing to notice here is the friend function inside Class
A is using the Class B. However, we haven 't defined Class B at this point. For this to work, we need
a forward declaration of Class B in our program (This point is also discussed in comments).

Laboratory 05 – Friend Classes Functions and static Members Page 6 of 16


CC-211L Object Oriented Programming FALL 2023

Task 01: Complex Numbers [Estimated time 20 minutes / 10 marks]


Write a C++ to add two complex numbers using friend function. Your program should have a class
named as Complex, your class should also have a function to print the result.
Sample Output:

Fig. 05 (Pre-Lab Task)


6 + 12i is the result of addition of (1+4i and 5+8i)

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


Write a program in C++ to Check Whether a Number can be Express as Sum of Two Prime Numbers
using the friend function.
Pictorial Representation:
Sample Output:

Fig. 06 (Pre-Lab Task)


Sample Output:

Fig. 07 (Pre-Lab Task)


Use your knowledge to understand where to use friend function in this program.

Laboratory 05 – Friend Classes Functions and static Members Page 7 of 16


CC-211L Object Oriented Programming FALL 2023

In-Lab Activities:
Understanding the Friend Classes:
A friend class can access private and protected members of other classes in which it is declared as a
friend. It is sometimes useful to allow a particular class to access private and protected members of
other classes.
Example Code:
Earlier we discussed how a friend function of a class can access its members. If we make the whole
class (ClassB) a friend of other class (ClassA). Now any function of ClassB will have access to all the
members of ClassA. Refer to the picture code below for better understanding:

Fig. 08 (Friend Class)


Output:

Fig. 09 (Friend Class)


Since ClassB is a friend class, we can create objects of ClassA inside of ClassB.

Laboratory 05 – Friend Classes Functions and static Members Page 8 of 16


CC-211L Object Oriented Programming FALL 2023

‘this’ pointer:
Every object in C++ has access to its own address through an important pointer called ‘this’ pointer.
The ‘this’ pointer is an implicit parameter to all member functions. Therefore, inside a member function,
this may be used to refer to the invoking object.
Friend functions do not have a ‘this’ pointer, because friends are not members of a class. Only member
functions have a ‘this’ pointer.
Example:

Fig. 10 (‘this Pointer’)


Output:
Output will not be 20, rather program will display some garbage value. Our program failed to invoke
the member.

Fig. 11 (‘this’ Pointer)


Solution:
See line# 9 change it to this->x=x;.

Laboratory 05 – Friend Classes Functions and static Members Page 9 of 16


CC-211L Object Oriented Programming FALL 2023

Fig. 12 (‘this Pointer’)


‘this’ pointer can also be used to return reference to the calling object:
Example Code:
Kindly observe the code and comments carefully for the better understanding:

Fig. 13 (‘this Pointer’)


Output:

Fig. 14 (‘this’ Pointer)


static data members:

• Only one copy of that member is created for the entire class and is shared by all the objects of
that class, no matter how many objects are created.
• It is initialized before any object of this class is being created, even before main starts.
• It is visible only within the class, but its lifetime is the entire program

Laboratory 05 – Friend Classes Functions and static Members Page 10 of 16


CC-211L Object Oriented Programming FALL 2023

Example:

Fig. 15 (static Data member)


Output:

Fig. 16 (static Data member)

Laboratory 05 – Friend Classes Functions and static Members Page 11 of 16


CC-211L Object Oriented Programming FALL 2023

static Member function:


Static member functions in C++ are the functions that can access only the static data members. These
static data members share a single copy of themselves with the different objects of the same class. A
function can be made static by using the keyword static before the function name while defining a class.
We will make some changes in the above code and make ourselves a static member function getCount
that will display the objectCount (a static data member)
Example:

Fig. 17 (static Member function)

Laboratory 05 – Friend Classes Functions and static Members Page 12 of 16


CC-211L Object Oriented Programming FALL 2023

Task 01: Integer Array [Estimated time 40 minutes / 30 marks]


Write a program with a class integer that contains an array of integers. Initialize the integer array in
the constructor of the class. Then create friend functions to the class:

• Find the largest integer in the array.


• Find the smallest integer in the array.
• Find the repeated elements in array.
• Sort the elements of array in ascending order.
• Create a destructor that sets all of the elements in the array to 0

Task 02: Object Invocation [Estimated time 30 minutes / 20 marks]


Write a C++ program to create three objects for a class named “pntr_obj” with data members

• roll_no
• name
Create member functions:

• set_data() for setting the data values


• print() member function to print which object has invoked it using this pointer

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


Develop a C++ program to find the area of a rectangle by converting the member of a class square (data
member: side) which is a friend class of rectangle (data member: height, width). Declare Rectangle as
a friend of Square so that Rectangle member functions could have access to the private member of
square.

Laboratory 05 – Friend Classes Functions and static Members Page 13 of 16


CC-211L Object Oriented Programming FALL 2023

Post-Lab Activities:
Task 01: VicobaAccount [Estimated time 40 minutes / 30 marks]
Village Community Banks (VICOBA) is a microfinance scheme to improve the economic status of
people living in rural areas in Tanzania. In this scheme, members take out a loan and are required to
pay back the money after a fixed number of years, and for each year, a fixed interest rate must be paid.
Create a VicobaAccount class that will help VICOBA track their customers’ loans. Provide data
members to represent the

• amount borrowed
• the annual interest rate
• the loan duration in years
The annual interest rate should be declared static.
Also provide a static member function that will be used to alter the annual interest rate, another function
that will report the amount of money a customer has to repay based on the annual interest rate and the
loan duration (assume simple interest is used) and a constructor that accepts the loan amount and
duration. Finally, add get and set functions for all the data members.

Laboratory 05 – Friend Classes Functions and static Members Page 14 of 16


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: [30 marks]
▪ Task 01: Complex Numbers [10 marks]
▪ Task 02: Prime Sum [20 marks]
• Division of In-Lab marks: [70 marks]
▪ Task 01: Integer Array [30 marks]
▪ Task 02: Object Invocation [20 marks]
▪ Task 03: Friend Class [20 marks]
• Division of Post-Lab marks: [30 marks]
▪ Task 01: VicobaAccount [30 marks]

References and Additional Material:


• Friend Function and Friend Classes
https://www.programiz.com/cpp-programming/friend-function-class
• ‘this’ Pointer
https://www.geeksforgeeks.org/this-pointer-in-c/
• static Member function
https://www.geeksforgeeks.org/static-member-function-in-cpp/
• static Data member
https://www.geeksforgeeks.org/static-data-members-c/

Laboratory 05 – Friend Classes Functions and static Members Page 15 of 16


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 05 – Friend Classes Functions and static Members Page 16 of 16

You might also like