#include <iostream>
#include <string>
class Fruit
{
private:
std::string name; //название
std::string color; //цвет
double weight; //вес
static int totalFruits; //количество
public:
//конструктор
Fruit() : name(""), color(""), weight(0)
{
totalFruits++;
std::cout << "конструктор. всего: " << totalFruits << std::endl;
}
//конструктор
Fruit(std::string n, std::string c) : name(n), color(c), weight(0)
{
totalFruits++;
std::cout << "конструктор. всего: " << totalFruits << std::endl;
}
//конструктор
Fruit(std::string n, std::string c, double w) : Fruit(n, c)
{
weight = w;
std::cout << "конструктор. всего: " << totalFruits << std::endl;
}
//геттер
std::string getName() const
{
return name;
}
//геттер
std::string getColor() const
{
return color;
}
//геттер
double getWeight() const
{
return weight;
}
//геттер
static int getTotalFruits()
{
return totalFruits;
}
//сеттер
void setName(std::string n)
{
name = n;
}
//сеттер
void setColor(std::string c)
{
color = c;
}
//сеттер
void setWeight(double w)
{
weight = w;
}
//вывод
void printInfo() const
{
std::cout << "фрукт: " << name << std::endl;
std::cout << " цвет: " << color << std::endl;
std::cout << " вес: " << weight << " кг" << std::endl;
std::cout << " всего: " << totalFruits << std::endl;
}
//деструктор
~Fruit()
{
totalFruits--;
std::cout << "деструктор. всего: " << totalFruits << std::endl;
}
};
//инициализация
int Fruit::totalFruits = 0;
int main()
{
//создание объектов с разными конструкторами
Fruit pineapple("Ананас", "красный", 0.5); // 3 параметра
Fruit pear("Груша", "желтый"); // 2 параметра
Fruit apple; // по умолчанию
apple.setName("Яблоко"); // устанавливаем имя через сеттер
//вывод информации
std::cout << "\nинформация:\n";
pineapple.printInfo();
pear.printInfo();
apple.printInfo();
//изменение статической переменной
std::cout << "\nизменение...\n";
Fruit::getTotalFruits();
//вывод обновленной информации
std::cout << "\nинформация:\n";
pineapple.printInfo();
pear.printInfo();
apple.printInfo();
return 0;
}