CodeClerks

Returning Objects in C++



Write the reason you're deleting this FAQ

Returning Objects in C++

A function can not only receive objects as arguments but also can return them. The example in program…



#include <iostream>

using namespace std;

class complex    // x + iy form
{
   float x;   // real part
   float y;   // imaginary part
   public:
   void input (float real), (float imag)
   { x = real; y = imag;}

   friend complex sum(complex, complex);

void show (comples);
};

complex sum (complex c1, complex  c2)
{
   complex c3;      // objects c3 is created
   c3.x = c1.x + c2.x;
   c3.y = c1.y + c2.y;
   return (c3);      // returns object c3
}

void complex :: show (complex c)
{
   Cout << c.x <<+j” << c.y << “n”;
}

int main()
{
   Complex A, B, C;

   A.input (3.1, 5.65);
   B.input (2.75, 1.2);

   C = sum (A, B);    // C = A+B

   cout << “A=; A.show(A);
   cout << “B=; B.show(B);
cout << “C=; C.show(C);

return 0;
}
 

 

Comments

Please login or sign up to leave a comment

Join