试图理解类对象的行为

Trying to Understand the behavior of my class objects

本文关键字:对象      更新时间:2023-10-16

请帮助我理解以下5行代码。这两种有什么区别

comp3.AddBoth(comp1, comp2); //this 1 )  give 0 0 but why
comp3.Show();

comp3 = comp3.AddBoth(comp1,comp2); //this 2) give 6 + 12i which is the correct answer but why
comp3.Show();

完整代码:

#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();
//}

AddBoth是类ComplexNumber的一个方法,它不修改任何实例成员,既不修改m_r也不修改m_i。它的作用是将两个复数相加,然后返回一个新的复数。在第一个块中,您调用AddBoth,而该调用不会修改comp3,因此您会得到一个(0,0(输出,因为这些值恰好是实例成员初始化为的值(请参阅类的私有部分(。在第二个块中,您将和的结果分配给comp3,然后打印它。这就是为什么您会看到预期的输出。

正如注释中所指出的,在您的示例中,comp3.AddBoth返回一个临时结果,实际上并没有修改当前对象本身。如果你想要这种行为,你必须这样做:

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)
}

然后的用法是:

// assuming comp1, comp2 are instances of the ComplexNumber class
comp1.Add(comp2); // adds comp2 to comp1, result is "in" comp1
comp1.Show();

如果您希望通过添加另外两个ComplexNumber来创建一个新的ComplexNumber对象,您可以执行以下操作:

// 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);
}

然后你必须像这样使用:

// 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();

我认为这是一个关于C++中类和类对象的练习,但如果你想在实际程序中使用复数,我建议你看看C++标准提供的std::complex数据类型。我也看不出为什么实数和虚数变量应该是不同的数据类型(int和float(。