Lab 03
Lab 03
Laboratory 03
Version: 1.0.0
Contents:
• Learning Objectives
• Required Resources
• General Instructions
• Background and Overview
o Reference to an Object
o Copy Constructors
o Const Keyword
o Composition
o Member Initializer List
• Activities
o Pre-Lab Activity
▪ Default memberwise assignment
▪ Copy Constructor
▪ Example Code 1
▪ When do we need to define our own copy constructor?
▪ Example Code 2
▪ Example Code 3
▪ Exercise 1
▪ Task 01: Copy Constructor text
▪ Task 02: Shallow Copy
▪ Task 03: Deep Copy
o In-Lab Activity
▪ Creating Reference to an Object
▪ Example Code 4
▪ Using Reference to an Object
▪ Returning Reference to a private data member
▪ Importance of const keyword in returning reference to a private data member
▪ Example Code 5
▪ Example Code 5 after using const keyword
▪ Task 01: Access using Reference as return type
▪ Task 02: Update an array using reference as return type
▪ Task 03: Forbid Change
o Post-Lab Activity
▪ Task 01: Composition with two classes
▪ Task 02: Composition with three classes
• 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
• Class Separation using header
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]
Activities:
Pre-Lab Activities:
Default memberwise assignment (Default copy constructor):
Default memberwise assignment is a feature of the C++ programming language that enables implicit
copies of data members when an object is assigned to another. In other words, when an object is
assigned to another, the data members of the first object are copied to the second object. This is done
automatically by the compiler unless the user specifies that the assignment operator should be
overridden. This is a shallow copy, meaning that any pointers or references are simply copied, not
their values.
Copy Constructor:
A copy constructor is a special constructor in C++ that creates an object by initializing it with an
existing object of the same type. The copy constructor will make a deep copy, meaning that any
pointers or references will be copied with their values. The copy constructor is used to create a new
object with updated values from an existing object.
Example Code 1:
Default member-wise assignment and copy constructors are generally similar in their usage when
used in a static environment like shown in the code below. Their syntax is also generally the same,
with the copy constructor sometimes being initialized as object1(object2) instead of object1=object2.
which does a member-wise copy between objects. The compiler created copy constructor works fine in
general. We need to define our own copy constructor only if an object has pointers or any runtime
allocation of the resource like file handle, a network connection, etc.
Example Code 2:
Following example shows shallow copy i.e., default assignment operator usage:
Output:
Fig. 06 (Exercise 1)
Write expected output of above code:
Output >
Output >
Expected Output:
In-Lab Activities:
Creating Reference to an Object:
1. Instantiate an object: To create a reference to an object in C++, first, you must instantiate an object.
This can be done with a simple declaration, like "MyObject myObject;"
2. Create a reference variable: To create a reference variable, use the syntax "Type &name = object;"
For example, "MyObject &myObjectRef = myObject;"
3. Use the reference variable: Now you can use the reference variable to access the properties and
functions of the object it is referencing. For example, "myObjectRef.doSomething();"
Example Code 4:
Output:
Task 01: Access using Reference as return type [40 minutes / 20 marks]
Write a C++ program to create a class Set5 with an array of 5 integers and the following functions:
setElements(), copy constructor, and an overloaded operator ++. Allow the user to enter values of a
Set5 object and create a copy with each element incremented by one. Display the values and addresses
of the elements in main() using a display function with reference as return type.
Sample Output:
Post-Lab Activities:
Task 01: Composition with two classes [Estimated 20 minutes / 10 marks]
Design a Line class to be implemented using composition with the Point class. The program should
allow users to create Lines using two Points, and output the coordinates of each Point. Create
constructors and getter/setter methods for each class, and write the logic in main () to generate the
Lines and display the coordinates of the two Points.
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 .c 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: [50 marks]
▪ Task 01: Copy Constructor [05 marks]
▪ Task 02: Shallow Copy [20 marks]
▪ Task 03: Deep Copy [25 marks]
• Division of In-Lab marks: [60 marks]
▪ Task 01: Access using Reference as return type [20 marks]
▪ Task 02: Update an array using reference as return type [30 marks]
▪ Task 03: Forbid Change [10 marks]
• Division of Post-Lab marks: [30 marks]
▪ Task 01: Composition with two classes [10 marks]
▪ Task 02: Composition with three classes [20 marks]
• Return By Reference
https://www.scaler.com/topics/cpp-return-reference/
• Composition
https://www.geeksforgeeks.org/object-composition-delegation-in-c-with-examples/