fork download
  1. #include <iostream>
  2. #include <string>
  3.  
  4. class Fruit
  5. {
  6. private:
  7. std::string name; //название
  8. std::string color; //цвет
  9. double weight; //вес
  10. static int totalFruits; //количество
  11.  
  12. public:
  13. //конструктор
  14. Fruit() : name(""), color(""), weight(0)
  15. {
  16. totalFruits++;
  17. std::cout << "конструктор. всего: " << totalFruits << std::endl;
  18. }
  19.  
  20. //конструктор
  21. Fruit(std::string n, std::string c) : name(n), color(c), weight(0)
  22. {
  23. totalFruits++;
  24. std::cout << "конструктор. всего: " << totalFruits << std::endl;
  25. }
  26.  
  27. //конструктор
  28. Fruit(std::string n, std::string c, double w) : Fruit(n, c)
  29. {
  30. weight = w;
  31. std::cout << "конструктор. всего: " << totalFruits << std::endl;
  32. }
  33.  
  34. //геттер
  35. std::string getName() const
  36. {
  37. return name;
  38. }
  39.  
  40. //геттер
  41. std::string getColor() const
  42. {
  43. return color;
  44. }
  45.  
  46. //геттер
  47. double getWeight() const
  48. {
  49. return weight;
  50. }
  51.  
  52. //геттер
  53. static int getTotalFruits()
  54. {
  55. return totalFruits;
  56. }
  57.  
  58. //сеттер
  59. void setName(std::string n)
  60. {
  61. name = n;
  62. }
  63.  
  64. //сеттер
  65. void setColor(std::string c)
  66. {
  67. color = c;
  68. }
  69.  
  70. //сеттер
  71. void setWeight(double w)
  72. {
  73. weight = w;
  74. }
  75.  
  76. //вывод
  77. void printInfo() const
  78. {
  79. std::cout << "фрукт: " << name << std::endl;
  80. std::cout << " цвет: " << color << std::endl;
  81. std::cout << " вес: " << weight << " кг" << std::endl;
  82. std::cout << " всего: " << totalFruits << std::endl;
  83. }
  84.  
  85. //деструктор
  86. ~Fruit()
  87. {
  88. totalFruits--;
  89. std::cout << "деструктор. всего: " << totalFruits << std::endl;
  90. }
  91. };
  92.  
  93. //инициализация
  94. int Fruit::totalFruits = 0;
  95.  
  96. int main()
  97. {
  98. //создание объектов с разными конструкторами
  99. Fruit pineapple("Ананас", "красный", 0.5); // 3 параметра
  100. Fruit pear("Груша", "желтый"); // 2 параметра
  101. Fruit apple; // по умолчанию
  102. apple.setName("Яблоко"); // устанавливаем имя через сеттер
  103.  
  104. //вывод информации
  105. std::cout << "\nинформация:\n";
  106. pineapple.printInfo();
  107. pear.printInfo();
  108. apple.printInfo();
  109.  
  110. //изменение статической переменной
  111. std::cout << "\nизменение...\n";
  112. Fruit::getTotalFruits();
  113.  
  114. //вывод обновленной информации
  115. std::cout << "\nинформация:\n";
  116. pineapple.printInfo();
  117. pear.printInfo();
  118. apple.printInfo();
  119.  
  120. return 0;
  121. }
Success #stdin #stdout 0s 5288KB
stdin
45
stdout
конструктор. всего: 1
конструктор. всего: 1
конструктор. всего: 2
конструктор. всего: 3

информация:
фрукт: Ананас
  цвет: красный
  вес: 0.5 кг
  всего: 3
фрукт: Груша
  цвет: желтый
  вес: 0 кг
  всего: 3
фрукт: Яблоко
  цвет: 
  вес: 0 кг
  всего: 3

изменение...

информация:
фрукт: Ананас
  цвет: красный
  вес: 0.5 кг
  всего: 3
фрукт: Груша
  цвет: желтый
  вес: 0 кг
  всего: 3
фрукт: Яблоко
  цвет: 
  вес: 0 кг
  всего: 3
деструктор. всего: 2
деструктор. всего: 1
деструктор. всего: 0