0% found this document useful (0 votes)
20 views7 pages

OOP Assignment 6

This document discusses operator overloading in C++. It provides examples of overloading operators like +, -, *, ++ and == for user-defined classes like Time and Complex. The exercises involve overloading these operators for classes to perform arithmetic and comparisons on objects of these classes.

Uploaded by

saadsohail1918
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views7 pages

OOP Assignment 6

This document discusses operator overloading in C++. It provides examples of overloading operators like +, -, *, ++ and == for user-defined classes like Time and Complex. The exercises involve overloading these operators for classes to perform arithmetic and comparisons on objects of these classes.

Uploaded by

saadsohail1918
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Lab Journal – Lab 6

Object Oriented Programming

Lab Journal – Lab 6


Name: Muhammad Saad Sohail

Enrollment #: 01-134232-203

Class: BS-CS-2(C)

Objective

This lab session is dedicated to operator overloading in C++. You will learn the syntax of
overloading arithmetic and relational operators and implement them.

Go through Lab 6 of the manual, attempt the given programs and verify their outputs. Once you
are done, perform the following tasks.

Tasks :

Answer the following/Write C++ code where required.

1. What is operator overloading?


ANS:
Operator overloading is a programming concept that enables operators to work
with user-defined types as well as their default behavior with standard types.

2. Is it possible to overload the ‘+’ operator for data type int?


ANS: NO

3. How do we differentiate between prefix and postfix increment operator while


overloading them?
ANS: For prefix increment, you overload the operator as a member function with no parameters
but with the prefix version of the operator (++var). For postfix increment, you overload it with a
dummy integer parameter inside the parentheses.
Object Oriented Programming Page 1
Lab Journal – Lab 6

4. What is the syntax of overloading ‘*’ operator for a class Matrix. (You do not need to
write the body of the function)

class Matrix {
private:
// Define your matrix data members here

public:
// Constructor(s), destructor, and other member functions

// Overloading the '*' operator for Matrix multiplication


Matrix operator*(const Matrix& other) const;
};

5. Go through example 6.2 of the manual and write the output of the given code segment.

counter c1(5), c2(10), c3;


c3=c1++;
c2=--c3;;
cout<<”\n”<<c1.get_count();
cout<<”\n”<<c2.get_count();
cout<<”\n”<<c3.get_count();

OUTPUT:

6
9
5

Exercise 1

Write a complete C++ program with the following features.


a. Declare a class Time with two fields, hour and minute.
b. Provide appropriate constructors to initialize the data members.
c. Overload the pre and postfix increment operators (using member
functions) to increment the minutes by one. Also make sure if minutes
are 59 and you increment the Time it should update hours and minutes
accordingly.
d. Provide a function display() to display the hours and minutes.

Object Oriented Programming Page 2


Lab Journal – Lab 6

e. In main(), create objects of Time, initialize them using constructors and


call the display functions.
f. Test the following in your main:
a. T3= ++T1;
b. T4=T2++;

#include <iostream>

class Time {
private:
int hour;
int minute;

public:
Time(int h = 0, int m = 0) : hour(h), minute(m) {}

Time& operator++() {
++minute;
if (minute == 60) {
++hour;
minute = 0;
}
if (hour == 24) {
hour = 0;
}
return *this;
}

Time operator++(int) {
Time temp = *this;
++(*this);
return temp;
}

void display() const {


std::cout << "Time: " << hour << ":" << minute << std::endl;
}
};

int main() {
Time T1(12, 30);
Time T2(23, 59);

Time T3, T4;

T3 = ++T1;
std::cout << "After prefix increment (T3 = ++T1): ";
T3.display();

T4 = T2++;
std::cout << "After postfix increment (T4 = T2++): ";
T4.display();

return 0;
}

Object Oriented Programming Page 3


Lab Journal – Lab 6

Exercise 2

Write a class Complex to model complex numbers and overload the following
operators (using member functions).
a. ‘+’ and ‘–‘ operators for addition and subtraction respectively.
b. ‘~’ operator to find the conjugate of a complex number.
c. ‘*’ operator to multiply two complex numbers.
d. ‘!’ operator to find the magnitude (absolute value) of a complex number.

CODE :

#include <iostream>
#include <cmath>

using namespace std;

class Complex {
private:
double real;
double imag;

public:
Complex(double r = 0, double i = 0) : real(r), imag(i) {}

Complex operator+(const Complex& other) const {


return Complex(real + other.real, imag + other.imag);
}

Complex operator-(const Complex& other) const {


return Complex(real - other.real, imag - other.imag);
}

Complex operator~() const {


return Complex(real, -imag);
}

Complex operator*(const Complex& other) const {


double real_part = (real * other.real) - (imag * other.imag);
double imag_part = (real * other.imag) + (imag * other.real);
return Complex(real_part, imag_part);
}

double operator!() const {


return sqrt((real * real) + (imag * imag));
}

Object Oriented Programming Page 4


Lab Journal – Lab 6

void display() const {


cout << real << (imag >= 0 ? " + " : " - ") << fabs(imag) << "i" << endl;
}
};

int main() {
Complex a(3, 4);
Complex b(1, -2);

Complex sum = a + b;
cout << "Sum: ";
sum.display();

Complex diff = a - b;
cout << "Difference: ";
diff.display();

Complex conjugate = ~a;


cout << "Conjugate of a: ";
conjugate.display();

Complex product = a * b;
cout << "Product: ";
product.display();

double magnitude_a = !a;


double magnitude_b = !b;
cout << "Magnitude of a: " << magnitude_a << endl;
cout << "Magnitude of b: " << magnitude_b << endl;

return 0;
}
OUTPUT :

Exercise 3

Using non-member functions, overload the following operators for the class
Complex.
a. The ‘>’ operator such that the statement c1>c2 should return true if the
real part of c1 is greater than that of c2.
b. The’==’ operator which returns true if the two complex numbers are equal.

Object Oriented Programming Page 5


Lab Journal – Lab 6

CODE :

#include <iostream>
#include <cmath>

using namespace std;

class Complex {
private:
double real;
double imag;

public:
Complex(double r = 0, double i = 0) : real(r), imag(i) {}

double getReal() const {


return real;
}

double getImag() const {


return imag;
}

void display() const {


cout << real << (imag >= 0 ? " + " : " - ") << fabs(imag) << "i";
}
};

bool operator>(const Complex& c1, const Complex& c2) {


return c1.getReal() > c2.getReal();
}

bool operator==(const Complex& c1, const Complex& c2) {


return (c1.getReal() == c2.getReal()) && (c1.getImag() == c2.getImag());
}

int main() {
Complex c1(3, 4);
Complex c2(5, 2);
Complex c3(3, 4);

cout << "Is c1 > c2? " << (c1 > c2 ? "Yes" : "No") << endl;
cout << "Are c1 and c3 equal? " << (c1 == c3 ? "Yes" : "No") << endl;

return 0;
}

OUTPUT :

Object Oriented Programming Page 6


Lab Journal – Lab 6

Implement the given exercises and get them evaluated by your instructor.

S No. Exercise Checked By:


1. Exercise 1

2. Exercise 2

3. Exercuse 3

+++++++++++++++++++++++++

Object Oriented Programming Page 7

You might also like