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++
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | #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