CC-211L_OOP_Lab-04
CC-211L_OOP_Lab-04
Laboratory 04
Version: 1.0.0
Contents:
Learning Objectives
Required Resources
General Instructions
Background and Overview
o Reference to an Object
o Const Keyword
o Composition
o Member Initializer List
Activities
o Pre-Lab Activity
Returning reference to private data members
Task 01
Task 02
o In-Lab Activity
Default Memberwise Assignment
const Objects and Member functions
Composition
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:
Returning Reference to private Data Members
Default Memberwise Assignment
const Objects
const Member Functions
Object Composition and Aggregation
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:
Returning Reference to a private data member:
Returning a reference to a private data member is a way for a class to allow other classes and functions
to access a private variable without making the variable public. This allows the private variable to
remain protected while still providing a way for it to be accessed.
The use of the const keyword is of utmost importance in this regard, as it prevents external code from
modifying private data members by not allowing the reference to be used to change their values. If the
reference return type is declared const, the reference is a nonmodifiable value, meaning it cannot be
used to modify the data. On the other hand, if the reference return type is not declared const, it can lead
to subtle errors.
Example:
If we use the ‘const’ keyword as a return type, the compiler will prevent any modification of private
data members of a class, thus preserving the concept of encapsulation while still allowing access to
those members. As seen in the code snippet below, the compiler will throw an error on any lines where
we attempt to change the values of private data members.
In-Lab Activities:
Default Memberwise Assignment:
The assignment operator (=) can be used to assign an object to another object of the same type. By
default, such assignment is performed by Memberwise assignment each data member of the object
on the right of the assignment operator is assigned individually to the same data member in the object
on the left of the assignment operator.
Example:
The statement above declares a const object noon of class Time and initializes it to 12 noon. It’s
possible to instantiate const and non-const objects of the same class.
Constant member functions are those functions that are denied permission to change the values of the
data members of their class. To make a member function constant, the keyword “const” is appended
to the function prototype and to the function definition header.
Example:
Fig. 11 (Composition)
Output:
Fig. 12 (Composition)
Explanation:
In this program, class X has one data member ‘num’ and two member functions ‘set_value()’ and
‘show_sum()’. The set_value() function is used to assign value to ‘num’.
The show_sum() function uses an integer type parameter. It adds the value of parameter with the
value of ‘num’ and displays the result on the Console.
Another class Y is defined after the class X. the class Y has an object of class X that is the C++
Composition relationship between classes X and Y. This class has its own member function
print_result().
In the main() function, an object ‘b’ of class Y is created. The member function set_value() of object
‘a’ that is the sub-object of object ‘b’ is called by using two dot operators. One dot operator is used to
access the member of the object ‘b’ that is object ‘a’, and second is used to access the member
function set_value() of sub-object ‘a’ and ‘num’ is assigned a value 20.
In the same way, the show_sum() member function is called by using two dot operators. The value 50
is also passed as a parameter. The member function print_result of object ‘b’ of class Y is also called
for execution. In the body of this function, the show_sum() function of object ‘a’ of class X is called
for execution by passing value 10.
Post-Lab Activities:
Task 01: Composition with three classes [Estimated 40 minutes / 20 marks]
Write a program that uses composition to define classes, Time, Date and Event, which are used to store
and print the details related to an event. The program should allow the user to enter the details of the
event, such as the hour, day, month, year and name of the event. There should be a function named
printEventData() in the event class. In main(), when this function is called with an argument like the
name of the event or date of the event, all the details entered by the user for that event should be printed
on screen. For example, if the user enters the date 25/12/2020, and there are two objects with this date,
the output should be:
Christmas occurs on 25/12/2020 at 06:00
Quaid’s Birthday occurs on 25/12/2020 at 01:15
And make sure to display when constructors and destructors are called for each class.
Note: Objects are constructed from the inside out (contained classes will be constructed first) and
destructed in the reverse order, from the outside in (container class will be destructed first).
Submit “.cpp” file named your “RollNo” on Google Classroom.
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: Array Updating [20 marks]
Task 02: Forbid Change [10 marks]
Division of In-Lab marks: [60 marks]
Task 01: Composition with two classes [20 marks]
Task 02: Rational Numbers [30 marks]
Task 03: Course Composition [10 marks]
Division of Post-Lab marks: [20 marks]
Task 01: Composition with three classes [20 marks]
Composition
https://www.geeksforgeeks.org/object-composition-delegation-in-c-with-examples/