Advertisement
Proff_Ust

Лаба для Елизаветы(первая)

Dec 14th, 2015
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.02 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. class Pair
  6. {
  7.     public:
  8.         Pair(int a, int b)
  9.         {
  10.             _a = a;
  11.             _b = b;
  12.         }
  13.         Pair Sum(Pair &p)
  14.         {
  15.             Pair *p2 = new Pair(p._a + _a, p._b + _b);
  16.             return *p2;
  17.         }
  18.         int MultAB()
  19.         {
  20.             return _a * _b;
  21.         }
  22.         void out()
  23.         {
  24.             cout << "(" << _a << "," << _b << ")" << endl;
  25.         }
  26.         protected:
  27.             int _a,_b;
  28.         private:
  29.     };
  30. class Complex : public Pair
  31. {
  32.     public:
  33.         Complex(int a, int b) : Pair(a,b) {};
  34.         Complex Mult(Complex &c)
  35.         {
  36.             Complex *c2 = new Complex(c._a * _a - c._b * _b, c._a * _b + c._b * _a);
  37.             return *c2;
  38.         }
  39.         Complex Diff(Complex &c)
  40.         {
  41.             Complex *c2 = new Complex(_a - c._a, _b - c._b);
  42.             return *c2;
  43.         }
  44.     protected:
  45.     private:
  46. };
  47.  
  48. int main()
  49. {
  50.     cout << "Введите первое число" << endl;
  51.     int a,b;
  52.     cout << "Введите действительную часть: ";
  53.     cin >> a;
  54.     cout << "Введите мнимую часть: ";
  55.     cin >> b;
  56.     Complex c1 = *(new Complex(a,b));
  57.     cout << "Введите второе число" << endl;
  58.     cout << "Введите действительную часть: ";
  59.     cin >> a;
  60.     cout << "Введите мнимую часть: ";
  61.     cin >> b;
  62.     Complex c2 = *(new Complex(a,b));
  63.  
  64.     Pair res = c1.Sum(c2);
  65.     cout << "Сумма: ";
  66.     res.out();
  67.     cout << endl;
  68.     res = c1.Diff(c2);
  69.     cout << "Разность: ";
  70.     res.out();
  71.     cout << endl;
  72.     res = c1.Mult(c2);
  73.     cout << "Произведение: ";
  74.     res.out();
  75.     cout << endl;
  76.     cout << "Произведение полей первого числа: ";
  77.     cout << c1.MultAB() << endl;
  78.     cout << "Произведение полей второго числа: ";
  79.     cout << c2.MultAB() << endl;
  80.  
  81.     return 0;
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement