The problem is you are modifying a ComplexNum object passed
by value (that is the calling code won't see the changes). You should pass it by reference, instead.
Change
Quote:
void getSum(ComplexNum n1, ComplexNum n2, ComplexNum n3){
To
void getSum(const ComplexNum & n1, const ComplexNum & n2, ComplexNum & n3){
in order to fix your code.
Anyway you may also consider overloading the
+
operator:
#include<iostream>
using namespace std;
class ComplexNum
{
int a,b;
public:
void getData()
{
cin >> a >> b;
}
friend ComplexNum operator + (const ComplexNum &, const ComplexNum &);
void display()
{
cout << "a = " <<a <<" " << "b = " <<b;
}
};
ComplexNum operator + (const ComplexNum & c1, const ComplexNum & c2)
{
ComplexNum r;
r.a = c1.a + c2.a;
r.b = c1.b + c2.b;
return r;
}
int main()
{
ComplexNum n1, n2, n3;
n1.getData();
n2.getData();
n3 = n1 + n2;
n3.display();
return 0;
}