Facebook
From asd, 10 Months ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 269
  1. #include <iostream>
  2.  
  3. using namespace std;
  4. class Complex {
  5.     private: int real,imag,three,four;
  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.     Complex operator * (Complex obj) {
  23.         Complex res;
  24.         res.real = real * obj.real;
  25.         res.three = real * obj.imag;
  26.         res.four = imag * obj.real;
  27.         res.imag = imag * obj.imag;
  28.         return res;
  29.     }
  30.     void printAdd() {
  31.         cout << real << " + i" << imag << endl;
  32.     }
  33.     void printSub() {
  34.         cout << real << " - i" << imag << endl;
  35.     }
  36.     void printMul() {
  37.         cout << real << " + i" << three << " + " << four << " + i" << imag << endl;
  38.     }
  39. };
  40.  
  41. int main() {
  42.     Complex c1 = Complex(10, 5);
  43.     Complex c2 = Complex(2, 3);
  44.     Complex add = c1 + c2;
  45.     Complex sub = c1 - c2;
  46.     Complex mul = c1 * c2;
  47.     add.printAdd();
  48.     sub.printSub();
  49.     mul.printMul();
  50.     return 0;
  51. }
  52.