Showing posts with label pass an object. Show all posts
Showing posts with label pass an object. Show all posts

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