Pract 1
Pract 1
1
Input: -
# include<iostream> {
using namespace std; Complex tmp;
class Complex //decaring Class tmp.real = real * c2.real - img * c2.img;
Complex tmp.img = real * c2.img + img * c2.real;
{ return tmp;
double real; }
double img; int main()
public: {
Complex(); // Default Constructor Complex C1, C2, C3, C4;
friend istream & operator >> (istream &, int flag = 1;
Complex &); // Input char b;
friend ostream & operator << (ostream &, const while (flag == 1)
Complex &); // Output {
Complex operator + (Complex); // Addition cout << "Enter Real and Imaginary part of the
Complex operator * (Complex); // Complex Number 1 : \n";
Multiplication cin >> C1;
}; cout << "Enter Real and Imaginary part of the
Complex::Complex() // Default Complex Number 2 : \n";
Constructor cin >> C2;
{ int f = 1;
real = 0; while (f == 1)
img = 0; {
} cout << "Complex Number 1 : " << C1 << endl;
istream & operator >> (istream &, Complex & cout << "Complex Number 2 : " << C2 << endl;
i) cout << "**********MENU**********" <<
{ endl;
cin >> i.real >> i.img; cout << "1. Addition of Complex Numbers" <<
return cin; endl;
} cout << "2. Multiplication of Complex
ostream & operator << (ostream &, const Numbers" << endl;
Complex & d) cout << "3. Exit\n";
{ int a;
cout << d.real << " + " << d.img << "i" << cout << "Enter your choice from above MENU
endl; (1 to 3) : ";
return cout; cin >> a;
} if (a == 1)
Complex Complex::operator + (Complex c1) // {
Overloading + operator C3 = C1+C2;
{ cout << "Addition : " << C3 << endl;
Complex temp; cout << "Do you wan to perform another
temp.real = real + c1.real; operation (y/n) : \n";
temp.img = img + c1.img; cin >> b;
return temp; if (b == 'y' || b == 'Y')
}
Complex Complex::operator * (Complex c2) // {
Overloading * Operator f=1;
} }
else else
{ {
cout << "Thanks for using this program!!\n"; cout << "Thanks for using this program!!\n";
flag=0; flag=0;
f=0; f=0;
} }
} }
else if (a == 2) else
{ {
C4 = C1 * C2; cout << "Thanks for using this program!!\n";
cout << "Multiplication : " << C4 << endl; flag=0;
cout << "Do you wan to perform another f=0;
operation (y/n) : \n"; }
cin >> b; }
if (b == 'y' || b == 'Y') }
return 0;
{ }
f=1;
Output : -
Multiplication: 9 + 40i