20120124

How to pass a object and return a object in C++/Cpp

//How to pass a object and return a object in C++/Cpp
//Program to show how to pass a object and return a object
// How to use friend function in C++/ Cpp
#include<iostream>
using namespace std;
class complex
{
private:
        float x;
        float y;
public:
        void getdata(float real, float imag)
        {
        x=real;
        y=imag;
        }
        friend complex sum(complex, complex);
        void show(complex);
};
//defining friend function outside the class
 //does not require scop resolution operator
complex sum(complex C1, complex C2)// friend function defined outside the class
//passing objects as function arguments
{
complex C3;
C3.x= C1.x + C2.x;
C3.y= C1.y + C2.y;
return(C); //returning object C

}
//we need :: operator to define simple mf outside the class
void complex :: show(complex c)// passing object of class complex
{
cout<<c.x<<"+j"<<c.y;
}
int main()
{
complex A, B, C;
A.getdata(1.2, 3.7);
B.getdata(1.9,5.4);
C=_sum(A,B);//C3=C1+C2
cout<<"A="<<A.showdata(A);
cout<<"B="<<B.showdata(B);
cout<<"C="<<C.showdata(C);
return 0;
}
Previous                             Home                               Next

3 comments:

  1. How to pass a object and return a object in C++/Cpp
    How to pass a object and return a object in C++/Cpp
    How to pass a object and return a object in C++/Cpp How to pass a object and return a object in C++/Cpp
    How to pass a object and return a object in C++/Cpp How to pass a object and return a object in C++/Cpp

    ReplyDelete
  2. Pls read as How to pass an Object
    Pls read as How to return an Object

    ReplyDelete
  3. How to pass a object and return a object in C++/Cpp
    How to pass a object and return a object in C++/Cpp
    How to pass an object and return a object in C++/Cpp
    How to pass an object and return an object in C++/Cpp
    How to pass an object and return a object in C++/Cpp How to pass an object and return an object in C++/Cpp
    How to pass a object and return a object in C++/Cpp
    How to pass a object and return a object in C++/Cpp
    How to pass an object and return a object in C++/Cpp
    How to pass an object and return an object in C++/Cpp
    How to pass an object and return a object in C++/Cpp How to pass an object and return an object in C++/Cpp

    ReplyDelete