Function To Add Complex number using operator overloading
Program:
// Online C++ compiler to run C++ program online
#include <bits/stdc++.h>
using namespace std;
class Complex {
private:
int a, b;
public:
void setcomplex(int x, int y)
{
a = x;
b = y;
}
Complex operator + (Complex p1)
{
Complex tmp;
tmp.a = p1.a + a;
tmp.b = p1.b + b;
return tmp;
}
void showValue() {
cout << a << " " << b;
}
};
int main() {
Complex el;
el.setcomplex(2, 4);
Complex pq;
pq.setcomplex(2, 5);
Complex ans = el + pq;
ans.showValue();
return 0;
}
Comments
Post a Comment