Not able to understand a block of code in below given program.
Especially the variable, temp which has return type as the complex(the class name) and when we return the variable, temp where it gets returned to?
That is return(temp);
in the program.
The Program
#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;
}