-
Notifications
You must be signed in to change notification settings - Fork 30
/
operator-overloading_complex.cpp
67 lines (65 loc) · 1.52 KB
/
operator-overloading_complex.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include<iostream>
using namespace std;
class Complex
{
private:
int real,imaginary;
public:
Complex(int a=0, int b=0)
{
real=a;
imaginary=b;
}
Complex operator + (Complex const &n)
{
Complex add;
add.real=real+n.real;
add.imaginary=imaginary+n.imaginary;
return add;
}
Complex operator-(Complex const &n)
{
Complex sub;
sub.real=real-n.real;
sub.imaginary=imaginary-n.imaginary;
return sub;
}
void display1()
{
if(imaginary<0)
cout<<"complex number: "<<real<<" "<<imaginary<<"i\n";
else
cout<<"complex number: "<<real<<" + "<<imaginary<<"i\n";
}
void display2()
{
if(imaginary<0)
cout<<real<<" "<<imaginary<<"i\n";
else
cout<<real<<" + "<<imaginary<<"i\n";
}
};
int main ()
{
int a, b, c, d;
cout << "Enter real part of 1st complex number ";
cin >>a;
cout << "Enter imaginary part of 1st complex number ";
cin >>b;
cout << "\nEnter real part of 2nd complex number ";
cin >>c;
cout << "Enter imaginary part of 2nd complex number ";
cin >>d;
Complex t(a,b),t1(c,d),t3,t4;
cout<<"\nFirst ";
t.display1();
cout<<"Second ";
t1.display1();
cout<<"\nAddition: ";
t3=t+t1;
t3.display2();
t4=t-t1;
cout<<"Substraction: ";
t4.display2();
return 0;
}