Advertisement
Guest User

Untitled

a guest
Jan 21st, 2020
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.97 KB | None | 0 0
  1. // JIIP KOLOSY.cpp : Ten plik zawiera funkcję „main”. W nim rozpoczyna się i kończy wykonywanie programu.
  2. //
  3.  
  4. #include <iostream>
  5. #include<map>
  6. #include<string>
  7. #include<cmath>
  8. using namespace std;
  9.  
  10. class M {
  11. protected:
  12.     int rows, cols;
  13.     double** a;
  14. public:
  15.     M(int x, int y)
  16.     {
  17.         cols = x;
  18.         rows = y;
  19.         a = new double* [y];
  20.         for (int i = 0; i < y; i++)
  21.         {
  22.             a[i] = new double[x];
  23.         }
  24.     }
  25.     void Show() {
  26.         for (int i = 0; i < cols; i++)
  27.         {
  28.             for (int j = 0; j < rows; j++)
  29.             {
  30.                 cout << a[i][j] << " ";
  31.             }
  32.             cout << endl;
  33.         }
  34.        
  35.     }
  36.    
  37.     double* operator() (const M&,int x,int y) const
  38.     {
  39.         if (x > cols || y > rows) throw std::out_of_range(" X lub Y poza zakresem");
  40.         else
  41.         {
  42.             return &a[x][y];
  43.         }
  44.     }
  45.     ~M() {
  46.         cout << "Destroyed like yo mama" << endl;
  47.     };
  48. };
  49. class NamedM : public M
  50. {
  51. //protected:
  52.    
  53. public:
  54.     string name;
  55.     NamedM(int x, int y, string xname) :name(xname), M(x, y) {};
  56.     void namedShow()
  57.     {
  58.         cout << name << endl;
  59.         Show();
  60.     }
  61. };
  62.  
  63.  
  64. int main()
  65. {
  66.     M janusz(4, 4);
  67.     janusz.Show();
  68.     try {
  69.         cout << janusz(janusz, 3, 3) << endl;
  70.     }
  71.     catch (exception & e)
  72.     {
  73.         cout << "Wyjebalo bleda" << e.what() << endl;
  74.     }
  75.     NamedM janina(3, 3, "SZLAUF");
  76.     janina.namedShow();
  77.     //TO NIE DZIALA PRZE PANA
  78.     /*
  79.     M* z(4,3);
  80.     if (NamedM* y = dynamic_cast<NamedM*>(z))
  81.     {
  82.          z->Show();
  83.     }
  84.     else {
  85.         z->namedShow();
  86.     }*/
  87. }
  88.  
  89. /* MAPY
  90. map<int, string> name_map;
  91. name_map[1] = "Plusz";
  92. name_map[2] = "Lubi";
  93. name_map[3] = "Susz";
  94. name_map[4] = "kappa"
  95. name_map[4] = "Penis ?";
  96. cout << name_map[4] << endl;
  97. for (map<int, string>::iterator it = name_map.begin(); it != name_map.end(); it++)
  98. {
  99.     cout << it->first << " => " << it->second << endl;
  100. }
  101. name_map.clear();
  102. name_map.insert(pair<int, string>(1, "KUPA"));
  103. cout << name_map[1] << endl;
  104. cout << name_map.size() << endl;
  105. name_map.insert(pair<int, string>(2, "KAL"));
  106. cout << name_map[2] << endl;
  107. cout << name_map.empty() << endl;
  108. name_map.clear();
  109. cout << name_map.empty() << endl;
  110.  
  111. map<float, string> mapka;
  112. mapka[1.3]=("shiettt");
  113. cout << mapka[1.3] << endl;
  114. */
  115. /* TEMPLATES
  116. template<typename T>
  117. void Print(T value)
  118. {
  119.     cout << value << endl;
  120. }
  121.  
  122.     Print(4);
  123.     Print("HELLO");
  124.     Print("kappa");
  125.     Print<string>("KAPUCZINO");
  126.  
  127.     template<typename T, int N>
  128. class Array
  129. {
  130. private:
  131.     T m_Array[N];
  132. public:
  133.     int GetSize() const { return N; }
  134. };
  135.     Array<int,5> array;
  136.     cout << array.GetSize() << endl;
  137. */
  138. /* OPERATORS
  139.  
  140. struct Vector2
  141. {
  142.     float x, y;
  143.     Vector2(float x, float y)
  144.         : x(x), y(y) {}
  145.     Vector2 Add(const Vector2& other) const
  146.     {
  147.         return Vector2(x + other.x, y + other.y);
  148.     }
  149.     Vector2 Multiply(const Vector2& other) const
  150.     {
  151.         return Vector2(x * other.x, y * other.y);
  152.     }
  153.  
  154.     Vector2 operator+(const Vector2& other) const
  155.     {
  156.         return (Add(other));
  157.     }
  158.     Vector2 operator*(const Vector2& other) const
  159.     {
  160.         return (Multiply(other));
  161.     }
  162. };
  163. ostream& operator <<(ostream& stream, const Vector2& other)
  164. {
  165.     stream << other.x << ", " << other.y;
  166.     return stream;
  167. }
  168.     Vector2 position(3.0f, 2.0f);
  169.     Vector2 speed(0.5f, 0.5f);
  170.     Vector2 powerup(1.1f, 1.1f);
  171.     Vector2 result1 = position.Add(speed.Multiply(powerup));
  172.     Vector2 result = position + speed * powerup;
  173.     cout << result.x << " " << result.y << endl;
  174.     cout << result1.x << " " << result1.y << endl;
  175.     cout << result << endl;
  176.     cout << result1 << endl;
  177. */
  178. /* LAMBDA
  179.     double x = 3;
  180.   double y=  [] (double x) {
  181.         if (sin(x) > cos(x)) return sin(x);
  182.         else return cos(x);
  183.             }(x);
  184.             cout << y << endl;
  185.             */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement