Facebook
From asd, 11 Months ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 209
  1. #include <iostream>
  2.  
  3. using namespace std;
  4. class Complex {
  5.     private: int real,imag;
  6.     public: Complex(int r=0, int i=0) {
  7.         real = r;
  8.         imag = i;
  9.     }
  10.     Complex operator + (Complex obj) {
  11.         Complex res;
  12.         res.real = real + obj.real;
  13.         res.imag = imag + obj.imag;
  14.         return res;
  15.     }
  16.     Complex operator - (Complex obj) {
  17.         Complex res;
  18.         res.real = real - obj.real;
  19.         res.imag = imag - obj.imag;
  20.         return res;
  21.     }
  22.     void printAdd() {
  23.         cout << real << " + i" << imag << endl;
  24.     }
  25.     void printSub() {
  26.         cout << real << " - i" << imag << endl;
  27.     }
  28. };
  29.  
  30. int main() {
  31.     Complex c1 = Complex(10, 5);
  32.     Complex c2 = Complex(2, 3);
  33.     Complex add = c1 + c2;
  34.     Complex sub = c1 - c2;
  35.     c1.printAdd();
  36.     c2.printSub();
  37.     return 0;
  38. }
  39.