Advertisement
Guest User

Exercise 2 / Lab 3

a guest
Oct 19th, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.19 KB | None | 0 0
  1. #include<iostream>
  2. using namespace std;
  3.  
  4. class Complex {
  5. private:
  6. double real, imag;
  7. public:
  8. Complex()
  9. {
  10. real = 0; imag = 0;
  11. }
  12. Complex(double a, double b)
  13. {
  14. real = a;
  15. imag = b;
  16. }
  17. void setReal(double a)
  18. {
  19. real = a;
  20. }
  21. void setImag(double a)
  22. {
  23. imag = a;
  24. }
  25. void setValue(double a, double b)
  26. {
  27. real = a;
  28. imag = b;
  29. }
  30. double getReal()
  31. {
  32. return real;
  33. }
  34. double getImag()
  35. {
  36. return imag;
  37. }
  38.  
  39. void print()
  40. {
  41. cout << "(" << real << "," << imag << ")" << endl;
  42. }
  43. bool isReal()
  44. {
  45. if (imag == 0)
  46. return true;
  47. else
  48. return false;
  49. }
  50. bool isImag()
  51. {
  52. if (real == 0)
  53. return true;
  54. else
  55. return false;
  56. }
  57.  
  58. void Add(Complex c)
  59. {
  60. real = real + c.real;
  61. imag = imag + c.imag;
  62. }
  63.  
  64.  
  65.  
  66. };
  67.  
  68. Complex Add(Complex C1, Complex C2)
  69. {
  70. Complex C3;
  71. C3.setReal( C1.getReal() + C2.getReal());
  72. C3.setImag(C1.getImag() + C2.getImag());
  73. return C3;
  74. }
  75.  
  76. int main()
  77. {
  78. Complex C1(3, 5);
  79. Complex C2;
  80. C2.setValue(5, -6);
  81. cout << "C1="; C1.print(); cout << "C2="; C2.print();
  82. C1.Add(C2);
  83. cout << "C1 after adding = "; C1.print();
  84. Complex C3 = Add(C1, C2);
  85. cout << "C3 = ";
  86. C3.print();
  87.  
  88.  
  89. return 0;
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement