C++ | Function passing and returning object

Introduction

This is the tutorial of c++ for function passing and returning object. The program is given below that adds two complex numbers using the above concept. The program is extendable. Go enjoy the program. Lets begin………

Program to add two complex numbers.


#include<iostream.h>
#include<conio.h>
//declare class
class cno
{
int a,b;
public:
void getno(void)
{
cout<<"Enter a and b of a+ib"<<endl;
cin>>a>>b;
}
cno sum(cno c)
{
cno tmp;
tmp.a=a+c.a;
tmp.b=b+c.b;
return (tmp);
}
void print(void)
{
cout<<a<<"+i"<<b;
}
};
void main()
{
//clear the screen.
clrscr();
//declare x,y,z as class cno.
cno x,y,z;
//Call functions.
x.getno();
y.getno();
z=x.sum(y);
//show the output c
cout<<"Sum = ";
z.print();
//get character
getch();
}

Output

Enter the a and b of a+ib
1
2
Enter the a and b of a+ib
2
3
Sum = 3+i5

Extending it

The program can be extended by using the concept in any program.

  • Remember this is a basic part of c++ which can be used in any program….

Explanation.

  1. Include ‘iostream.h’ and ‘conio.h’ files.
  2. Declare class.
  3. Add void main.
  4. Start program by first clearing the screen.
  5. Declare the x,y,z as your class name.
  6. Call the functions.

At the end

You learnt creating the C++ program for Adding complex numbers using function that passes object and returns object in c++. So now enjoy the program.

Please comment on the post and share it.
And like it if you liked.

Join India's Largest Code Learning Community on Uphub.io Get it on Google Play
Sharing options: