fork download
  1. #include<iostream>
  2. using namespace std;
  3.  
  4. class complex1
  5. {
  6. float real,img;
  7. public:
  8. complex1() : real(0.0), img(0.0){}
  9. complex1(float a,float b)
  10. {
  11. real=a;
  12. img=b;
  13. }
  14. friend complex1 sum(complex1,complex1);
  15. friend void display(complex1);
  16. };
  17.  
  18. complex1 sum(complex1 c1,complex1 c2)
  19. {
  20. complex1 c3;
  21. c3.real=c1.real+c2.real;
  22. c3.img=c1.img+c2.img;
  23. return c3;
  24. }
  25.  
  26. void display(complex1 c)
  27. {
  28. cout<<c.real<<"+j"<<c.img;
  29. }
  30.  
  31. int main()
  32. {
  33. complex1 c1(100.9,200.9);
  34. complex1 c2(50.9,50.9);
  35. complex1 c=sum(c1,c2);
  36. display(c); //display and sum is given directly because it is friend
  37. return 0;
  38. }
Success #stdin #stdout 0s 16064KB
stdin
Standard input is empty
stdout
151.8+j251.8