Lab 05
Lab 05
Laboratory 05
Version: 1.0.0
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
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]
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
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:
‘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:
• 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
Example:
• roll_no
• name
Create member functions:
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.
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]