Advertisement
silentkiler029

Classes and Objects for Maimona Farjana

Aug 12th, 2020
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.97 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. class Complex
  5. {
  6.     private:
  7.        int real;
  8.        int imag;
  9.     public:
  10.        Complex(): real(0), imag(0) { }
  11.        void readData()
  12.         {
  13.            cout << "Enter real and imaginary number respectively:"<<endl;
  14.            cin >> real >> imag;
  15.         }
  16.         void addComplexNumbers(Complex comp1, Complex comp2)
  17.         {
  18.            // real represents the real data of object c3 because this function is called using code c3.add(c1,c2);
  19.             real=comp1.real+comp2.real;
  20.  
  21.              // imag represents the imag data of object c3 because this function is called using code c3.add(c1,c2);
  22.             imag=comp1.imag+comp2.imag;
  23.         }
  24.  
  25.         void displaySum()
  26.         {
  27.             cout << "Sum = " << real<< "+" << imag << "i";
  28.         }
  29. };
  30. int main()
  31. {
  32.     Complex c1,c2,c3;
  33.  
  34.     c1.readData();
  35.     c2.readData();
  36.  
  37.     c3.addComplexNumbers(c1, c2);
  38.     c3.displaySum();
  39.  
  40.     return 0;
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement