Advertisement
Guest User

Lab examples

a guest
Jan 24th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.74 KB | None | 0 0
  1. #include <string>
  2. #include <vector>
  3. #include <iostream>
  4.  
  5. using namespace std;
  6.  
  7. class Mammal {
  8. private:
  9.     string name;
  10.  
  11. public:
  12.     virtual string voice() const {
  13.         return "Not implemented yet\n";
  14.     }
  15. };
  16.  
  17. class Cat : public Mammal {
  18. public:
  19.     string murmur() const {
  20.         return "murmur\n";
  21.     }
  22.  
  23.     string voice() const override {
  24.         return "meow\n";
  25.     }
  26. };
  27.  
  28. class Dog : public Mammal {
  29. public:
  30.     string voice() const override {
  31.         return "Waff!\n";
  32.     }
  33. };
  34.  
  35. int main() {
  36.     Cat c;
  37.     Dog d;
  38.     vector<Mammal> zoo;
  39.     zoo.push_back(c);
  40.     zoo.push_back(d);
  41.     cout << zoo[0].voice() << endl;
  42.  
  43.     vector<Mammal*> zoo2;
  44.     zoo2.push_back(&c);
  45.     zoo2.push_back(&d);
  46.     cout << zoo2[0]->voice() << endl;
  47. }
  48. ======================================================
  49. #include <iostream>
  50. #include <string>
  51.  
  52. using namespace std;
  53.  
  54. class Old {
  55. protected:
  56.     int x = 3;
  57. };
  58.  
  59. class New: public Old {
  60. protected:
  61.     int x = 4;
  62. public:
  63.     New() {
  64.         cout << x << " " << Old::x << endl;
  65.     }
  66. };
  67.  
  68. class Newest: New {
  69. protected:
  70.     string x = "5";
  71. public:
  72.     Newest() {
  73.         cout << this->x << " " << New::x << " " << Old::x << endl;
  74.     }
  75. };
  76.  
  77. int main() {
  78.     Old();
  79.     New();
  80.     Newest();
  81. }
  82. =============================================
  83. #include <iostream>
  84. #include <string>
  85. using namespace std;
  86.  
  87. enum {
  88.     FOO = 1, BAR = 2, BUZZ = 4
  89. };
  90.  
  91. enum Colors {
  92.     RED, BLUE
  93. };
  94.  
  95. enum Numbers {
  96.     // можно переопределять начал значения
  97.     ONE = 1, TWO, SEVEN = 7, EIGHT
  98. };
  99.  
  100. enum class TheColors {
  101.     THE_RED, THE_BLUE, THE_VIOLET
  102. };
  103.  
  104. enum class TheColors2 : int {
  105.     THE_RED = 1, THE_BLUE = 4, THE_VIOLET = 5
  106. };
  107.  
  108. int main() {
  109.     cout << sizeof(Colors) << " " << sizeof(RED) << " " << Colors::BLUE << endl;
  110.     // это константы, которые экспортируют свои имена в пространство имён
  111.     int x = 2 + RED;
  112.     Colors y = RED; // y++; хотя они и защищены явно от арифметики
  113.     y = static_cast<Colors>(
  114.             static_cast<int>(y) + 1
  115.         );
  116.     cout << (y == BLUE ? "DA": "NET") << endl;
  117.    
  118.     cout << ONE << TWO << SEVEN << EIGHT << endl;
  119.     // часто использовались в C как множества (настроек, ключей, ...)
  120.     auto some = FOO | BUZZ;
  121.     if ((some & BUZZ) == BUZZ) {
  122.         cout << "We have BUZZ" << endl;
  123.     }
  124.    
  125.     //===========================================
  126.    
  127.     TheColors color = TheColors::THE_RED; // просто THE_RED не сработает
  128.    
  129.     TheColors2 color2 = static_cast<TheColors2>(
  130.                                 static_cast<int>(TheColors2::THE_RED) |
  131.                                 static_cast<int>(TheColors2::THE_BLUE)
  132.                         );
  133.     if (color2 == TheColors2::THE_VIOLET) {
  134.         cout << "WOW" << endl;
  135.     }
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement