Reputation: 11
Please help me in understanding the following 5 lines of code. What the difference between these two
comp3.AddBoth(comp1, comp2); //this 1 ) give 0 0 but why
comp3.Show();
and
comp3 = comp3.AddBoth(comp1,comp2); //this 2) give 6 + 12i which is the correct answer but why
comp3.Show();
Full code:
#include<iostream>
class ComplexNumber
{
private:
int m_r = 0;
float m_i = 0.0f;
public:
ComplexNumber()
{
}
ComplexNumber(int real, float imagi)
{
m_r = real;
m_i = imagi;
}
void Show()
{
std::cout << " Real and imaginary equation is " << m_r << " + " << m_i << "i " << std::endl << std::endl;
}
ComplexNumber AddBoth( ComplexNumber c1, ComplexNumber c2)
{
int re;
float img;
re = c1.m_r + c2.m_r;
img = c1.m_i + c2.m_i;
ComplexNumber temp(re,img);
return temp;
//std::cout << " Real and imaginary equation is " << re << " + " << img << "i " << std::endl << std::endl;
}
};
int main()
{
ComplexNumber comp1(2, 4), comp2(4, 8), comp3;
comp1.Show();
comp2.Show();
comp3.AddBoth(comp1, comp2); //this 1 ) give 0,0 but why
comp3.Show();
// what the difference between these two
comp3 = comp3.AddBoth(comp1,comp2); //this 2) give 6 + 12i which is the correct answer but why
comp3.Show();
std::cin.get();
//return 0;
}
//#include<iostream>
//
//class ComplexNumber
//{
//
//private:
// int r;
// float i;
//
//public:
// ComplexNumber()
// {
// }
// ComplexNumber(int a, float b)
// {
// r = a;
// i = b;
// }
//
// void Show()
// {
// std::cout << " Real and imaginary equation is " << r << " + " << i << "i " << std::endl << std::endl;
// }
// int getReal()
// {
// return r;
// }
// float getImagi()
// {
// return i;
// }
//
//};
//
//// ComplexNumber here is the return type like int getReal() and flot getImagi
//ComplexNumber AddBothObj(ComplexNumber obj1, ComplexNumber obj2)
//{
// int newR;
// float newI;
//
// newR = obj1.getReal() + obj2.getReal();
// newI = obj1.getImagi() + obj2.getImagi();
//
// ComplexNumber temp(newR, newI);
//
// return temp;
//}
//
//
//int main()
//{
// ComplexNumber comp1(3, 7), comp2(6, 14), comp3;
// comp1.Show();
// comp2.Show();
//
// comp3 = AddBothObj(comp1,comp2);
// comp3.Show();
//
// std::cin.get();
//}
Upvotes: 1
Views: 69
Reputation: 556
As pointed out in the comments, in your example, comp3.AddBoth
returns a temporary result and doesn't actually modify the current object itself. If you want that behavior you will have to do something like this:
void Add(ComplexNumber other) // you can also make other a const reference which would avoid unnecessary object copies (if the compiler doesn't optimize the copy away)
{
m_r = m_r + other.m_r; // or m_r += other.m_r
m_i = m_i + other.m_i; // or m_i += other.m_i
// if you want to you can also return a reference to the current object here if you
// wish to chain operations like comp3.Add(comp1).Add(comp2)
}
The usage would then be:
// assuming comp1, comp2 are instances of the ComplexNumber class
comp1.Add(comp2); // adds comp2 to comp1, result is "in" comp1
comp1.Show();
If you would rather want to create a new ComplexNumber
object from the addition of two other ComplexNumber
s you can do:
// notice the static keyword here meaning you don't call this function using an instance of ComplexNumber
static ComplexNumber AddBoth(ComplexNumber c1, ComplexNumber c2) // again you can make c1 and c2 const references
{
int re = c1.m_r + c2.m_r;
float img = c1.m_i + c2.m_i;
ComplexNumber temp(re, img);
return temp;
// or simply return ComplexNumber(c1.m_r + c2.m_r, c1.m_i + c2.m_i);
}
which you would then have to use like this:
// assuming comp1 and comp2 are instances of the ComplexNumber class
ComplexNumber comp3 = ComplexNumber::AddBoth(comp1, comp2); // add comp1 and comp2 and save the result in comp3
comp3.Show();
I think it is obvious that this is an exercise on classes and class objects in C++ but if you want to use complex numbers in real programs I would suggest you have a look at the std::complex data type provided by the C++ standard. I also don't see an obvious reason why the real and imaginary variables should be different data types (int and float).
Upvotes: 1
Reputation: 8074
AddBoth
is a method of the class ComplexNumber
that doesn't modify any instance member, neither m_r
nor m_i
. What it does is to add two complex numbers and return a new complex number. In the first block you call AddBoth
and that call doesn't modify comp3
, so you get a (0, 0) output because those happen to be the values to which the members of the instance are initialized to (see private section of the class). In the second block you assign the result of the sum to comp3
and then you print it. That's why you see the expected output.
Upvotes: 2