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++

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

#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

 

Prof.Fazal Rehman Shamil (Available for Professional Discussions)
1. Message on Facebook page for discussions,
2. Video lectures on Youtube
3. Email is only for Advertisement/business enquiries.