/    /  CPP – Returning Objects

Returning Objects

 

A function is able to not only receive objects as arguments but also return them. 

The example below shows how an object can be created and returned to another function.

class Complex

{
 private:
int real, imag;
public:
Complex(int r = 0, int i = 0)
{
real = r; imag = i
};
void setComplex(void)
{
cout << "Enter real and imaginary parts";
cin >> this->real; // cin>>real;
cin >> this->imag; // cin>>image; }
Complex add(const Complex & c)
{
 Complex comp;
comp.real = this->real + c.real;
comp.imag = this->imag + c.imag;
return comp;
}
void printComplex(void)
{
cout << "Real : " << this->real << endl;
cout<< "Imaginary" << this->imag << endl;}
};