Facebook
From asdasd, 11 Months ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 213
  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.     Complex operator * (Complex obj) {
  31.         Complex res;
  32.         res.real = real * obj.real;
  33.         res.three = real * obj.imag;
  34.         res.four = imag * obj.real;
  35.         res.imag = imag * obj.imag;
  36.         return res;
  37.     }
  38.     void printAdd() {
  39.         cout << real << " + i" << imag << endl;
  40.     }
  41.     void printSub() {
  42.         cout << real << " - i" << imag << endl;
  43.     }
  44.     void printMul() {
  45.         cout << real << " + i" << three << " + i" << four << " - " << imag << endl;
  46.         cout << real - imag << " + i" << three + four << endl;
  47.     }
  48. };
  49.  
  50. int main() {
  51.     Complex c1 = Complex(10, 5);
  52.     Complex c2 = Complex(2, 4);
  53.     Complex add = c1 + c2;
  54.     Complex sub = c1 - c2;
  55.     Complex mul = c1 * c2;
  56.     add.printAdd();
  57.     sub.printSub();
  58.     mul.printMul();
  59.     return 0;
  60. }
  61.