CPP Programming
About Lesson

A function can also return object like a regular variable of basic data type.

//Example c++ program to demonstrate how an object can be returned from function
#include<iostream>
using namespace std;

class complex
{
float x;
float y;
public:
void input()
{
cout<<“Enter Real part: “;
cin>>x;
cout<<“Enter Imaginary part: “;
cin>>y;
}
friend complex sum(complex,complex);
void show(complex);
};

complex sum(complex c1,complex c2)
{
complex c3;
c3.x=c1.x+c2.x;
c3.y=c1.y+c2.y;
return c3;

}

void complex::show(complex c)
{
cout<<“sum of given two complex numbers is: “<<c.x<<“+i”<<c.y<<endl;
}

int main()
{
complex a,b,c;
cout<<“Enter your First Complex Number:”<<endl;
a.input();
cout<<“Enter your Second Complex Number:”<<endl;
b.input();
c=sum(a,b);
c.show(c);
}

You cannot copy content of this page