0% found this document useful (0 votes)
4 views

Function Overloading

The document contains multiple C++ programming examples demonstrating operator overloading for various classes including Complex, MyString, Time, and Matrix. Each class implements specific operators such as addition, subtraction, and comparison, along with methods for input and output. The main functions illustrate the usage of these overloaded operators in practical scenarios.

Uploaded by

shashank
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Function Overloading

The document contains multiple C++ programming examples demonstrating operator overloading for various classes including Complex, MyString, Time, and Matrix. Each class implements specific operators such as addition, subtraction, and comparison, along with methods for input and output. The main functions illustrate the usage of these overloaded operators in practical scenarios.

Uploaded by

shashank
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 14

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 + other.real, imaginary + other.imaginary);

Complex operator-(const Complex &other)

return Complex(real - other.real, imaginary - other.imaginary);

Complex operator*(const Complex &other)

{
return Complex(real * other.real - imaginary * other.imaginary,
real * other.imaginary + imaginary * other.real);

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): ";

result1.display();

cout << "Result2 (7 - obj1): ";

result2.display();

cout << "Result3 (4 * obj2): ";

result3.display();

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 + other.str);

bool operator>(const MyString &other) {

return str > other.str;

bool operator==(const MyString &other) {

return str == other.str;

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: ";

concatenated.display();

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 >> t.hours;
cout << "Enter minutes: ";

in >> t.minutes;

return in;

ostream& operator<<(ostream &out, const Time &t) {

out << "Time: " << t.hours << " hours " << t.minutes << " 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) {

data.resize(r, vector<int>(c));

Matrix operator+(const Matrix &other) {

if (rows != other.rows || cols != other.cols) {

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++) {

result.data[i][j] = data[i][j] + other.data[i][j];

return result;

Matrix operator-(const Matrix &other) {

if (rows != other.rows || cols != other.cols) {

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++) {

result.data[i][j] = data[i][j] - other.data[i][j];

return result;

bool operator==(const Matrix &other) {


return (rows == other.rows && cols == other.cols);

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 < matrix.rows; i++) {

for (int j = 0; j < matrix.cols; j++) {

in >> matrix.data[i][j];

return in;

ostream& operator<<(ostream &out, const Matrix &matrix) {

for (int i = 0; i < matrix.rows; i++) {

for (int j = 0; j < matrix.cols; j++) {

out << matrix.data[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;

You might also like