-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproblem_006.cpp
More file actions
44 lines (43 loc) · 983 Bytes
/
problem_006.cpp
File metadata and controls
44 lines (43 loc) · 983 Bytes
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
//Write a c++ program to implement the concept of binary operator overloading
#include<iostream>
using namespace std;
class Complex{
private:
double real;
double imag;
public:
Complex(double r,double i):real(r),imag(i){}
Complex operator+(Complex& c3)
{
Complex c(0,0);
c.real=this->real+c3.real;
c.imag=this->imag+c3.imag;
return c;
}
Complex operator-(Complex& c3)
{
Complex c(0,0);
c.real=this->real-c3.real;
c.imag=this->imag-c3.imag;
return c;
}
void display()
{
if(imag>=0)
{
cout<<real<<"+"<<imag<<"i"<<endl;
}else{
cout<<real<<imag<<"i"<<endl;
}
}
};
int main()
{
Complex c1(10,3);
Complex c2(20,5);
Complex c4(0,0);
c4=c1+c2;
c4.display();
c4=c1-c2;
c4.display();
}