6 CC-214 Object Oriented Programming Lab
Spring 2022 Issue Date: March 25, 2024
LAB-06 ,, 2024
The objective of this lab is to:
1. Practice Friend Functions, Friend Classes.
Instructions!
1. Please follow the dress code before coming to the lab. Keep your student identity cards with you.
2. This is an individual lab, you are strictly NOT allowed to discuss your solutions with
your fellow colleagues, even not allowed to ask how he/she is doing, it may result in
negative marking. You can ONLY discuss with your TAs or with me.
3. Strictly follow good coding conventions (commenting, meaningful variable and
functions names, properly indented and modular code.
4. Save your work frequently. Make a habit of pressing CTRL+S after every line of code
you write.
5. Beware of memory leaks and dangling pointers.
Note: You are required to create a multi-file project for each task.
Task 01: [30 Marks]
Task: Implementing Complex Number Operations Using a Friend Class
You are required to implement a C++ program that performs basic arithmetic operations on complex
numbers using a friend class.
Problem Statement:
1. Define a class named Complex to represent complex numbers. The class should have private data
members to store the real and imaginary parts of the complex number.
2. Implement a parameterized constructor to initialize the complex number with given real and
imaginary parts.
3. Provide getter and setter functions.
4. Declare a friend class named ComplexCalculator that will perform arithmetic operations on
complex numbers.
5. Inside the ComplexCalculator class, define member functions to perform the following
operations:
Addition of two complex numbers.
Complex add(const Complex& c1, const Complex& c2);
Subtraction of two complex numbers.
Complex subtract(const Complex& c1, const Complex& c2);
Multiplication of two complex numbers.
Complex multiply(const Complex& c1, const Complex& c2);
Division of two complex numbers. Ensure to handle division by zero gracefully
Complex divide(const Complex& c1, const Complex& c2);.
Square of a complex number.
Complex Square(const Complex& c1) For this purpose you can use the multiply
function. (You better know how to)
Magnitude of a complex number.
float Magnitude(const Complex& c1)
The magnitude of a complex number a+bi is given by sqrt(a2+b2).
Madiha Khalid Page 1 of 2
Department of Software Engineering, University of the
Punjab.
6 CC-214 Object Oriented Programming Lab
Spring 2022 Issue Date: March 25, 2024
LAB-06 ,, 2024
For sqrt you can use cmath library but for square you can’t use any library
6. The ComplexCalculator class should have access to the private members of the Complex class
(Use friend class Concept here).
7. Implement a display function within the Complex class to print the complex number in a
readable format.
8. In main, Display the results of each operation.
Madiha Khalid Page 2 of 2
Department of Software Engineering, University of the
Punjab.