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.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s