how to return obj in cpp

How To Return Object 

PROGRAM:

  1. #include<iostream>
  2.  
  3. using namespace std;
  4.  
  5. class Complex{
  6. int x,y;
  7.  
  8. public:
  9. void setdata(int a,int b){
  10. x=a; y=b;
  11. }
  12. void showdata(){
  13. cout<<"\nx = "<<x<<" y = "<<y;
  14. }
  15. friend Complex addition(Complex,Complex);
  16. };
  17.  
  18. Complex addition(Complex a,Complex b){
  19. Complex temp;
  20. temp.x=a.x+b.x;
  21. temp.y=a.y+b.y;
  22. return temp;
  23. }
  24.  
  25. int main(){
  26. Complex c1,c2,c3;
  27. c1.setdata(3,2);
  28. c2.setdata(2,3);
  29. cout<<"\n c1:";
  30. c1.showdata();
  31. cout<<"\n c2:";
  32. c2.showdata();
  33. c3=addition(c1,c2);
  34. cout<<"\n c3:";
  35. c3.showdata();
  36. return 0;
  37. }
  38.  
  39.  

Comments

Popular posts from this blog

Introduction to ChatGPT: Explain what ChatGPT is, how it works, and what it is used for.

Multi Level Inheritance In cpp

Hybrid Inheritance In cpp