Site icon T4Tutorials.com

How to pass and return object from a function in C++ using classes

Let us see How to pass and return an object from a function in C++ using classes.

Program to pass and return an object – C++

#include <iostream> 
using namespace std; 
class t4tutorials
{
	private: 
	int t;
	public:
		void setdata()
		{cout<<"enter a value"<<endl;
		cin>>t;
		}
		void disp()
		{cout<<"the vaue of t="<<t;
		}
		t4tutorials sum(t4tutorials, t4tutorials);
	};
	t4tutorials t4tutorials::sum(t4tutorials x, t4tutorials y)
	{t4tutorials o;
	o.t=x.t+y.t;
	}
	int main()
	{
		t4tutorials objectect1, objectect2, objectect3, objectect4;
		objectect2.setdata();
		objectect3.setdata();
		objectect4=objectect1.sum(objectect2, objectect3);
		objectect4.disp();
		return 0;	
		}

Output

enter a value

3

enter a value

5

the vaue of t=8

 

Exit mobile version