I am trying to add two complex numbers using class but keep getting an error. I am also having trouble with the way functions compare and add are declared does the & means the functions are called by reference and is there any special way to define these functions.
#include <iostream>
#include <set>
#include <list>
#include <cmath>
using namespace std;
class Complex {
int real, img;
public:
void getdata() {
cin >> real >> img;
}
void display() {
cout << real << "+" << img << "i" << endl;
}
Complex & compare(Complex & );
Complex & add(Complex & );
};
Complex & compare(Complex & c2) {
Complex t;
if (real > c2.real) {
t.real = real; //getting is private within this context error for real here
} else if (c2.real > real) {
t.real = c2.real;
} else if (img > c2.img) {
t.img = img;
} else if (c2.img > img) {
t.img = c2.img;
}
return t;
}
Complex & add(Complex & a) {
Complex temp;
temp.real = real + a.real;
temp.img = img + a.img;
return temp;
}
int main() {
Complex c1, c2, c3;
c1.getdata();
c2.getdata();
c3 = c1.compare(c2);
c3.display();
c3 = c1.add(c2);
c3.display();
return 0;
}