Advanced programing assignment
OPERATOR OVERLOADING
- Shashank Reddy (CSE22102)
1)
#include <iostream>
using namespace std;
class Complex
private:
double real;
double imaginary;
public:
Complex(double r = 0, double i = 0) : real(r), imaginary(i) {}
Complex operator+(const Complex &other)
return Complex(real + [Link], imaginary + [Link]);
Complex operator-(const Complex &other)
return Complex(real - [Link], imaginary - [Link]);
Complex operator*(const Complex &other)
{
return Complex(real * [Link] - imaginary * [Link],
real * [Link] + imaginary * [Link]);
Complex operator+(const int &integer)
return Complex(real + integer, imaginary);
Complex operator-(const int &integer)
return Complex(real - integer, imaginary);
Complex operator*(const int &integer)
return Complex(real * integer, imaginary * integer);
void display()
cout << real << " + " << imaginary << "i" << endl;
};
int main()
Complex obj1(2.0, 3.0);
Complex obj2(1.0, 2.0);
Complex result1 = obj1 + obj2;
Complex result2 = obj1 - 5;
Complex result3 = obj2 + 4;
cout << "Result1 (obj1 + obj2): ";
[Link]();
cout << "Result2 (7 - obj1): ";
[Link]();
cout << "Result3 (4 * obj2): ";
[Link]();
return 0;
OUTPUT
2) #include <iostream>
#include <string>
using namespace std;
class MyString {
private:
string str;
public:
MyString(const string &s) : str(s) {}
MyString operator+(const MyString &other) {
return MyString(str + [Link]);
bool operator>(const MyString &other) {
return str > [Link];
bool operator==(const MyString &other) {
return str == [Link];
void display() {
cout << str << Endl;
};
int main() {
MyString str1("Hello, ");
MyString str2("World!");
MyString str3("Hello, ");
MyString str4("C++");
MyString concatenated = str1 + str2;
cout << "Concatenated string: ";
[Link]();
if (str1 > str2) {
cout << "str1 is greater than str2" << endl;
} else {
cout << "str1 is not greater than str2" << endl;
if (str1 == str3) {
cout << "str1 is equal to str3" << endl;
} else {
cout << "str1 is not equal to str3" << endl;
if (str1 == str4) {
cout << "str1 is equal to str4" << endl;
} else {
cout << "str1 is not equal to str4" << endl;
return 0;
}
3) #include <iostream>
using namespace std;
class Time {
private:
int hours;
int minutes;
public:
Time(int h = 0, int m = 0) : hours(h), minutes(m) {}
friend istream& operator>>(istream &in, Time &t);
friend ostream& operator<<(ostream &out, const Time &t);
Time operator++(int);
Time& operator++();
Time operator--(int);
Time& operator--();
};
istream& operator>>(istream &in, Time &t) {
cout << "Enter hours: ";
in >> [Link];
cout << "Enter minutes: ";
in >> [Link];
return in;
ostream& operator<<(ostream &out, const Time &t) {
out << "Time: " << [Link] << " hours " << [Link] << " minutes";
return out;
Time Time::operator++(int) {
Time temp(hours, minutes);
minutes++;
if (minutes == 60) {
hours++;
minutes = 0;
return temp;
Time& Time::operator++() {
minutes++;
if (minutes == 60) {
hours++;
minutes = 0;
return *this;
}
Time Time::operator--(int) {
Time temp(hours, minutes);
minutes--;
if (minutes < 0) {
hours--;
minutes = 59;
if (hours < 0) {
hours = 0;
minutes = 0;
return temp;
Time& Time::operator--() {
minutes--;
if (minutes < 0) {
hours--;
minutes = 59;
if (hours < 0) {
hours = 0;
minutes = 0;
return *this;
int main() {
Time t1, t2;
cout << "Enter time t1: " << endl;
cin >> t1;
cout << "Enter time t2: " << endl;
cin >> t2;
Time t3 = t1++;
Time t4 = --t2;
cout << "t1 after post-increment: " << t1 << endl;
cout << "t2 after pre-decrement: " << t2 << endl;
cout << "t3 (post-incremented t1): " << t3 << endl;
cout << "t4 (pre-decremented t2): " << t4 << endl;
return 0;
}
4)
#include <iostream>
#include <vector>
using namespace std;
class Matrix {
private:
vector<vector<int>> data;
int rows;
int cols;
public:
Matrix(int r, int c) : rows(r), cols(c) {
[Link](r, vector<int>(c));
Matrix operator+(const Matrix &other) {
if (rows != [Link] || cols != [Link]) {
cerr << "Matrix dimensions are not compatible for addition."
<< endl;
return Matrix(0, 0);
Matrix result(rows, cols);
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
[Link][i][j] = data[i][j] + [Link][i][j];
return result;
Matrix operator-(const Matrix &other) {
if (rows != [Link] || cols != [Link]) {
cerr << "Matrix dimensions are not compatible for
subtraction." << endl;
return Matrix(0, 0);
Matrix result(rows, cols);
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
[Link][i][j] = data[i][j] - [Link][i][j];
return result;
bool operator==(const Matrix &other) {
return (rows == [Link] && cols == [Link]);
friend istream& operator>>(istream &in, Matrix &matrix);
friend ostream& operator<<(ostream &out, const Matrix &matrix);
};
istream& operator>>(istream &in, Matrix &matrix) {
for (int i = 0; i < [Link]; i++) {
for (int j = 0; j < [Link]; j++) {
in >> [Link][i][j];
return in;
ostream& operator<<(ostream &out, const Matrix &matrix) {
for (int i = 0; i < [Link]; i++) {
for (int j = 0; j < [Link]; j++) {
out << [Link][i][j] << ' ';
out << '\n';
return out;
int main() {
int rows, cols;
cout << "Enter the number of rows and columns for the first matrix:
";
cin >> rows >> cols;
Matrix matrix1(rows, cols);
cout << "Enter the elements for the first matrix:\n";
cin >> matrix1;
cout << "Enter the number of rows and columns for the second matrix:
";
cin >> rows >> cols;
Matrix matrix2(rows, cols);
cout << "Enter the elements for the second matrix:\n";
cin >> matrix2;
if (matrix1 == matrix2) {
Matrix sum = matrix1 + matrix2;
Matrix diff = matrix1 - matrix2;
cout << "Matrix 1 + Matrix 2:\n" << sum;
cout << "Matrix 1 - Matrix 2:\n" << diff;
return 0;