Operator Overloading

 /* OOP Group A - 1

Q1. Implement A Class Complex Which Represents The Complex Number Data Type.
Implement The Following -
1. Constructer(Including Default Constructer Which Creates Complex Number 0 + 0i)
2. Overload Operator + To Add Two Complex Numbers
3. Overload Operator * To Multiply Two Complex Numbers
4. Overload Operators << And >> To Print And Read Complex Numbers
*/

#include <iostream>
using namespace std;

class Complex
{
float real;
float imaginary;

public:
// Constructer Declaration
Complex(void); // Default Constructer
~Complex(void) {} // Destructer
friend istream &operator>>(istream &, Complex &); // Stream Extraction Operator Overloading Declaration
friend ostream &operator<<(ostream &, Complex &); // Stream Insertion Operator Overloading Declaration

void printResult(Complex &obj1, Complex &obj2, const char *op, string str)
{
cout << endl << str << " : " << "(" << obj1.real << " " << "+" << " " << obj1.imaginary << "i" << ")" << " " << op << " " << "(" << obj2.real << " " << "+" << " " << obj2.imaginary << "i" << ")" << " = " << real << " + " << imaginary << "i" << endl;
}

Complex operator+(Complex &obj1)
{
Complex result;
result.real = real + obj1.real;
result.imaginary = imaginary + obj1.imaginary;
return result;
}

Complex operator-(Complex &obj1)
{
Complex result;
result.real = real - obj1.real;
result.imaginary = imaginary - obj1.imaginary;
return result;
}

Complex operator*(Complex &obj1)
{
Complex result;
result.real = (real * obj1.real) + (imaginary * obj1.imaginary) * (-1);
result.imaginary = (imaginary * obj1.real) + (real * obj1.imaginary);
return result;
}

Complex operator/(Complex &obj1)
{
Complex result;
result.real = (real * obj1.real) / ((obj1.real * obj1.real) + (obj1.imaginary * obj1.imaginary)) - ((imaginary * obj1.imaginary) * (-1)) / ((obj1.real * obj1.real) + (obj1.imaginary * obj1.imaginary));
result.imaginary = (imaginary * obj1.real) / ((obj1.real * obj1.real) + (obj1.imaginary * obj1.imaginary)) - (real * obj1.imaginary) / ((obj1.real * obj1.real) + (obj1.imaginary * obj1.imaginary));
return result;
}
};

// Default Constructer Definition
Complex ::Complex(void)
{
real = 0;
imaginary = 0;
}

// Stream Extraction Operator Overloading Definition
istream &operator>>(istream &in, Complex &obj1)
{
in >> obj1.real >> obj1.imaginary;
return in;
}

// Stream Insertion Operator Overloading Definition
ostream &operator<<(ostream &out, Complex &obj1)
{
out << obj1.real << " + " << obj1.imaginary << "i" << endl
<< endl;
return out;
}

int main()
{
int userInput1;
char userInput2;
Complex cx, cx1, cx2;

cout << "Enter The Real And Imaginary Part For First Object " << endl;
cin >> cx1; // Extraction Operator Overloaded For First Object

// Handling Wrong Input
while (!cin.good())
{
cout << "Wrong Input, Please Enter Floats Or Integers Only!!\n";
cin.clear();
cin.ignore(100, '\n');
cout << "\nEnter The Real And Imaginary Part For First Object " << endl;
cin >> cx1;
}

cout << "Your Complex Number For First Object Is = ";
cout << cx1; // Insertion Operator Overloaded For First Object

cout << "Enter The Real And Imaginary Part For Second Object " << endl;
cin >> cx2; // Extraction Operator Overloaded For Second Object

// Handling Wrong Input
while (!cin.good())
{
cout << "Wrong Input, Please Enter Floats Or Integers Only!!\n";
cin.clear();
cin.ignore(100, '\n');
cout << "\nEnter The Real And Imaginary Part For Second Object " << endl;
cin >> cx2;
}

cout << "Your Complex Number For Second Object Is = ";
cout << cx2; // Insertion Operator Overloaded For Second Object

Complex cx3 = cx1 + cx2;
Complex cx4 = cx1 - cx2;
Complex cx5 = cx1 * cx2;
Complex cx6 = cx1 / cx2;

while (true)
{
cout << "1. Press 1 For Addition\n2. Press 2 For Subtraction\n3. Press 3 For Multiplication\n4. Press 4 For Division\n";
cin >> userInput1;

// Handling Wrong Input
while (!cin.good() >> userInput1)
{
cout << "Wrong Input, Please Enter Integers Only!!\n";
cin.clear();
cin.ignore(100, '\n');
cout << "\n1. Press 1 For Addition\n2. Press 2 For Subtraction\n3. Press 3 For Multiplication\n4. Press 4 For Division\n";
cin >> userInput1;
}

switch (userInput1)
{
case 1:
cx3.printResult(cx1, cx2, "+", "Addition");
break;

case 2:
cx4.printResult(cx1, cx2, "-", "Substraction");
break;

case 3:
cx5.printResult(cx1, cx2, "*", "Multiplication");
break;

case 4:
cx6.printResult(cx1, cx2, "/", "Division");
break;

default:
cout << "Please Enter Correct Input\n\n";
continue;
}

cout << "\nPress 'c' To Continue And 'e' To Exit\n";
cin >> userInput2;
cout << endl;

if (userInput2 == 'c')
{
continue;
}

else if (userInput2 == 'e')
{
break;
}

else
{
cout << "Please Enter Correct Input\n\n";
cout << "Press 'c' To Continue And 'e' To Exit\n";
cin >> userInput2;
cout << endl;

if (userInput2 == 'c')
{
continue;
}

else if (userInput2 == 'e')
{
break;
}
}
}
}

Comments

Popular posts from this blog

Ticket Booking System

Student Database

Generalised Linked List