2017-11-11 3 views
1

아래 주어진 프로그램에서 코드 블록을 이해할 수 없습니다. 특히 변수 인 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; 


} 
+0

C5=C1.operator+(C2) 

동등한 의미하지만, [일반적으로 나쁜'사용하여 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). –

+0

당신의 문제에 관해서, 우리가'+'연산자의 사용법을 보았을 때, 우리가 (더 단순한)'C3 = C1 + C2'를 가지고 있다고 가정하면, 그것은 C3 = C1.operator + (C2) '. 그게 도움이 되니? –

답변

0
complex C5; 
C5=C1+C2; 

이 문제에 대한

관련없는
complex temp; 
temp.x = x + C2.x; /* x=C1.x*/ 
temp.y = y + C2.y; /* y=C1.y*/ 
C5=temp; 
+0

당신은 거기에서 아주 중요한 부분을 놓치고 있습니다 ... 'C1'은 어디 있습니까? –

+0

@Someprogrammerdude, 이것이 매우 중요한 포인트입니다. – Arash

+0

C1? dint get ... –

관련 문제