fork download
  1. #include<iostream>
  2. using namespace std;
  3.  
  4. class Num
  5. {
  6. int real,imag;
  7. public:
  8. Num()
  9. {
  10. real=0;
  11. imag=0;
  12. }
  13. Num(int i)
  14. {
  15. real=i;
  16. imag=0;
  17. }
  18. Num(int a,int b)
  19. {
  20. real=a;
  21. imag=b;
  22. }
  23. void add(Num c1, Num c2)
  24. {
  25. real = c1.real+c2.real;
  26. imag = c1.imag+c2.imag;
  27. }
  28. void display()
  29. {
  30. cout<<real<<"+"<<imag<<"i";
  31. }
  32. };
  33.  
  34. int main()
  35. {
  36. int real,imag;
  37. cout<<"\nEnter the 1st real and imaginery number : ";
  38. cin>>real>>imag;
  39. Num n1(real,imag);
  40.  
  41. cout<<"\nEnter the 2nd real and imaginery number : ";
  42. cin>>real>>imag;
  43. Num n2(real,imag);
  44.  
  45. Num n3;
  46. n3.add(n1,n2);
  47. cout<<"\nThe sum is : ";
  48. n3.display();
  49.  
  50. return 0;
  51. }
  52.  
Success #stdin #stdout 0.01s 5284KB
stdin
Standard input is empty
stdout
Enter the 1st real and imaginery number : 
Enter the 2nd real and imaginery number : 
The sum is : -489589184+10442i