CC 211L - OOP - Lab 06 1
CC 211L - OOP - Lab 06 1
Laboratory 06
Version: 1.0.0
Contents:
● Learning Objectives
● Required Resources
● General Instructions
● Background and Overview
o Copy Constructor
o Operator Overloading
o Binary Operators
● Activities
o Pre-Lab Activity
▪ Copy Constructor
– Default Copy Constructor
– User Defined Copy Constructor
▪ Constructor Copies
– Shallow Copy
– Deep Copy
▪ Task 01 Part(a)
▪ Task 01 Part(b)
o In-Lab Activity
▪ Overloaded Operators of Standard Library Class string
▪ Operators that cannot be Overloaded
▪ Rules and Restrictions on Overloaded Operator
▪ Overloading Binary Operators
▪ 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:
● Copy Constructor
● Using Overloaded Operators of Standard Library
● Fundamentals of Operator Overloading
● Overloading Binary Operators
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]
A copy constructor is a type of constructor that creates a copy of another object. If we want one object
to resemble another object, we can use a copy constructor. If no copy constructor is written in the
program compiler will supply its own copy constructor.
Operator Overloading:
In C++, we can make operators work for user-defined classes. This means C++ has the ability to provide
the operators with a special meaning for a data type, this ability is known as operator overloading. For
example, we can overload an operator ‘+’ in a class like String so that we can concatenate two strings
by just using +.
© https://www.geeksforgeeks.org/operator-overloading-c/
Binary Operators:
A binary operator is an operator that operates on two operands and manipulates them to return a
result. Operators are represented by special characters or by keywords and provide an easy way to
compare numerical values or character strings.
Activities:
Pre-Lab Activities:
Copy Constructor:
Copy constructors are the member functions of a class that initialize the data members of the class
using another object of the same class. It copies the values of the data variables of one object of a
class to the data members of another object of the same class. A copy constructor can be defined as
follows:
variable_name = old_object.variable_name; …. …. } };
When a copy constructor is not defined, the C++ compiler automatically supplies with its self-
generated constructor that copies the values of the object to the new object.
Example:
Output:
In user-defined copy constructor, the values of the parameterized object of a class are copied to the
member variables of the newly created class object. The initialization or copying of the values to the
member variables is done as per the definition of the copy constructor.
Example:
Output:
Constructor Copies:
There are two ways in which copy constructor copies, and they are:
● Shallow copy
● Deep copy
Shallow Copy:
It is the process of creating a copy of an object by copying data of all the member variables as it
is. Only a default Constructor produces a shallow copy (Constructor which has no arguments).
Example:
Output:
Explanation:
In the above example, both ‘obj1’ and ‘obj2’ will be having the same input and both the object
variables will be pointing to the same memory locations. The changes made to one object
will affect another one. This can be solved by using deep copy.
Deep Copy:
It dynamically allocates memory for the copy first and then copies the actual value. In a deep copy,
both objects which have to copy and another which has to be copied will be having different
memory locations. So, the changes made to one will not affect another. This is used by a user-
defined copy constructor.
Example:
Output:
Explanation:
N1 and N2 are the two objects. ‘N2’ is the object which stores the value of objec’N1’. ‘N1’ takes 100
as input and will initialize to ‘N2’. Both N1 and N2 will have different locations. Changes made to one
will not affect the other.
In-Lab Activities:
Overloaded Operators of Standard Library Class string:
The C++ string class overloads these operators to work on string objects:
● String comparison (==, !=, >, <, >=, <=): For example, you can use str1 == str2 to compare the
contents of two string objects
● Stream insertion and extraction (<<, >>): For example, you can use cout << str1 and cin >> str2 to
output/input string objects
● Strings concatenation (+, +=): For example, str1 + str2 concatenates two string objects to produce
a new string object; str1 += str2 appends str2 into str1
● Character indexing or subscripting []: For example, you can use str[n] to get the char at index n;
or str[n] = c to modify the char at index n. Take note that [] operator does not perform index-
bound check, i.e., you have to ensure that the index is within the bounds. To perform index-bound
check, you can use string's at() member function
● Assignment (=): For example, str1 = str2 assigns str2 into str1
Example:
Output:
Operator Name
. Member selection
.* Member selection through pointer to function
:: Scope Resolution Operator
?: Ternary Operator
When we overload an operator which works on two operands, it is known as binary operator
overloading. It is used to manipulate the values of two objects of the same class. Following is the
syntax of overloading binary operators:
// Function Body
For defining our function inside the class the syntax will be:
// Function Body
It is similar to our function definition in C++. We start with a return data type of our function and
follow it with the name of our class. The catch comes after this, instead of writing the function
name, we initialize it with the operator we want to “overload”. One more thing to keep in mind is
that, unlike our default function, the binary operator overloaded function can only have one
argument.
Algorithm:
Example:
Output:
● Write a program to make a class of Complex numbers with data members and member
functions:
o real number
o imaginary number
o getreal() to get real numbers
o getimg() to get imaginary numbers
o show() to show data
o operator+() "subtract two objects"
o operator-() "add two objects"
o operator/()"multiply two objects"
o operator*() "divide first object by second object".
● Also write a driver code to test the above functionalities
Example Usage:
● Create a class Matrix to represent matrices. The class should have two data members:
o row to represent the number of rows
o col to represent the number of columns
Post-Lab Activities:
coef[n] = x^n .
For polynomial P = 3X^4 + 4X^2 + 16, if someone tries to access P[4] then it should get 3. Similarly if
someone tries to access P[3] it should get 0 as there is no term with degree 3.
Write a C++ program to create a class named “Polynomial” which must do the following tasks:
● Declare two polynomials and set their values ONLY by taking input from the user.
● Declare third polynomial and use copy constructor to set it to polynomial 1’s contents. Make
sure you perform deep copy otherwise ZERO will be granted for this part!
● Declare fourth polynomial, one by one apply the following functions, and display the resultant
fourth object side by side.
● Make sure to have a destructor that deallocates the allocated memory.
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: [20 marks]
▪ Task 01 Part(a): Object Copy [10 marks]
▪ Task 02 Part(b): Object Copy [10 marks]
● Division of In-Lab marks: [80 marks]
▪ Task 01: Copy Students [20 marks]
▪ Task 02: Complex Numbers [30 marks]
▪ Task 03: Matrix Operations [30 marks]
● Division of Post-Lab marks: [40 marks]
▪ Task 01: Polynomial [40 marks]