아래 주어진 프로그램에서 코드 블록을 이해할 수 없습니다. 특히 변수 인 temp는 반환 유형이 복합체 (클래스 이름)로되어 있으며 변수를 반환 할 때 temp는 어디로 반환됩니까? 이것은 프로그램에서 return(temp);
입니다.C에서 연산자 오버로딩을 사용하는 복소수 추가
프로그램
#include <iostream>
using namespace std;
class complex
{
public:
complex();//default constructors
complex(float real, float imag)//constructor for setting values
{
x = real;
y = imag;
}
complex operator +(complex);
void display(void);
~complex();
private:
float x;
float y;
};
complex::complex()
{
}
////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////
complex complex::operator+(complex c)
{
complex temp;
temp.x = x + c.x;
temp.y = y + c.y;
return(temp);
}
/////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////
void complex::display(void) {
cout << x << "+j" << y << "/n";
}
complex::~complex()
{
}
int main()
{
complex C1, C2, C3,C4;
C1 = complex(1, 3.5);//setting of first number
C2 = complex(2,2.7);//setting of second number
C4 = complex(2, 5);
C3 = C1 + C2+C4;//operator overloading
cout << "C1 = ";
C1.display();
cout << "\n C2 = ";
C2.display();
cout << "\n C4 = ";
C4.display();
cout << "\n C3 = ";
C3.display();
system("pause");
return 0;
}
에
동등한 의미하지만, [일반적으로 나쁜'사용하여 std 네임 스페이스와; '] (htt ps : //stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) 그런 다음 클래스 이름을 [표준'std :: complex '클래스 템플릿] (http://en.cppreference.com/w/cpp/numeric/complex). –
당신의 문제에 관해서, 우리가'+'연산자의 사용법을 보았을 때, 우리가 (더 단순한)'C3 = C1 + C2'를 가지고 있다고 가정하면, 그것은 C3 = C1.operator + (C2) '. 그게 도움이 되니? –