Advertisement
zielo

Untitled

Apr 10th, 2018
399
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.52 KB | None | 0 0
  1. /*
  2.  * Andrzej Zieliński
  3.  * ne ind. 116366
  4.  * klasa liczby zespolone
  5.  */
  6.  
  7. #include <iostream>
  8. #include <memory>
  9. #include <cmath>
  10. using namespace std;
  11.  
  12. template<class type>
  13. class complex_base
  14. {
  15. protected:
  16.     type a, b;
  17. public:
  18.     complex_base(type a1 = 0, type b1 = 0) :
  19.             a(a1), b(b1)
  20.     {
  21.     }
  22.     type real()
  23.     {
  24.         return a;
  25.     }
  26.     void real(type a)
  27.     {
  28.         this->a = a;
  29.     }
  30.     type imag()
  31.     {
  32.         return b;
  33.     }
  34.     void imag(type b)
  35.     {
  36.         this->b = b;
  37.     }
  38.     void print() const
  39.     {
  40.         cout << a << ", j" << b;
  41.     }
  42.     friend ostream& operator<<(ostream& ostre, const complex_base<type>& z)
  43.     {
  44.         ostre << z.a << ", j" << z.b;
  45.         return ostre;
  46.     }
  47. };
  48.  
  49. template<class type>
  50. class extended_complex: public complex_base<type>
  51. {
  52. public:
  53.     extended_complex(type a1 = 0, type b1 = 0) :
  54.             complex_base<type>(a1, b1)
  55.     {
  56.     }
  57.     double mod()
  58.     {
  59.         type a2 = complex_base<type>::a * complex_base<type>::a;
  60.         type b2 = complex_base<type>::b * complex_base<type>::b;
  61.         return sqrt(a2 + b2);
  62.     }
  63.     double arg()
  64.     {
  65.         return atan2(complex_base<type>::b, complex_base<type>::a);
  66.     }
  67.     void print()
  68.     {
  69.         cout << mod() << ", " << arg();
  70.     }
  71. };
  72.  
  73. int main()
  74. {
  75.     complex_base<int> X(7, -10);
  76.     const complex_base<int>& Y = X;
  77.     cout << "Zawartosc Y: " << Y << endl;
  78.     Y.print();
  79.     shared_ptr<complex_base<int>> X1(new complex_base<int>(143, -24));
  80.     shared_ptr<complex_base<int>> Y1;
  81.     Y1 = X1;
  82.     Y1->print();
  83.     cout << "\nZawartosc Y1: " << *Y1 << endl;
  84.     extended_complex<int> Z(5, 3);
  85.     Z.print();
  86.     cout << "\nMod: " << Z.mod();
  87.     return 0;
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement