/
lyapdy
/
skillbox_controllers
Обзор
Документация
Войти
/
lyapdy
/
skillbox_controllers
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
car/main.cpp
323 строки
9 KB
lyapdy
fifth task
27 ноя 2025, 07:35
27 ноя 2025, 07:35
63ec551
Код
Авторство
О чём код?
#include <iostream> #include <string> /** * @brief ������� ����� Car, �������������� ���������� */ class Car { protected: std::string brand; // �������� ����� int cylinders; // ����� ��������� int power; // �������� (� �.�.) static int objectCount; // ������� ��������� �������� public: // ������������ Car(); // ����������� ��� ���������� Car(const std::string& b, int c, int p); // ����������� � ����������� Car(const Car& other); // ����������� ����������� // ���������� virtual ~Car(); // ������� std::string getBrand() const { return brand; } int getCylinders() const { return cylinders; } int getPower() const { return power; } // ������� void setBrand(const std::string& b) { brand = b; } void setCylinders(int c) { cylinders = c; } void setPower(int p) { power = p; } // ���������� ��������� ������������ Car& operator=(const Car& other); // ���������� ���������� �����/������ friend std::ostream& operator<<(std::ostream& os, const Car& car); friend std::istream& operator>>(std::istream& is, Car& car); // ������������� ������� ��� �������� �������� friend int count(); }; // ������������� ����������� ���������� int Car::objectCount = 0; /** * @brief ����������� ��� ���������� */ Car::Car() : brand("Unknown"), cylinders(0), power(0) { objectCount++; std::cout << "Car default constructor called. Total objects: " << objectCount << std::endl; } /** * @brief ����������� � ����������� * @param b �������� ����� * @param c ����� ��������� * @param p �������� */ Car::Car(const std::string& b, int c, int p) : brand(b), cylinders(c), power(p) { objectCount++; std::cout << "Car parameterized constructor called. Total objects: " << objectCount << std::endl; } /** * @brief ����������� ����������� * @param other ������ ��� ����������� */ Car::Car(const Car& other) : brand(other.brand), cylinders(other.cylinders), power(other.power) { objectCount++; std::cout << "Car copy constructor called. Total objects: " << objectCount << std::endl; } /** * @brief ���������� */ Car::~Car() { objectCount--; std::cout << "Car destructor called. Total objects: " << objectCount << std::endl; } /** * @brief ���������� ��������� ������������ * @param other ������ ��� ������������ * @return ������ �� ������� ������ */ Car& Car::operator=(const Car& other) { if (this != &other) { brand = other.brand; cylinders = other.cylinders; power = other.power; } return *this; } /** * @brief ���������� ��������� ������ * @param os ����� ������ * @param car ������ Car ��� ������ * @return ����� ������ */ std::ostream& operator<<(std::ostream& os, const Car& car) { os << "Brand: " << car.brand << ", Cylinders: " << car.cylinders << ", Power: " << car.power << " hp"; return os; } /** * @brief ���������� ��������� ����� * @param is ����� ����� * @param car ������ Car ��� ����� * @return ����� ����� */ std::istream& operator>>(std::istream& is, Car& car) { std::cout << "Enter brand: "; is >> car.brand; std::cout << "Enter number of cylinders: "; is >> car.cylinders; std::cout << "Enter power (hp): "; is >> car.power; return is; } /** * @brief ������������� ������� ��� �������� �������� * @return ���������� ��������� �������� */ int count() { return Car::objectCount; } /** * @brief ����������� ����� Lorry (��������) */ class Lorry : public Car { private: int loadCapacity; // ���������������� (� ��) public: // ������������ Lorry(); // ����������� ��� ���������� Lorry(const std::string& b, int c, int p, int l); // ����������� � ����������� Lorry(const Lorry& other); // ����������� ����������� // ���������� ~Lorry() override; // ������� � ������� int getLoadCapacity() const { return loadCapacity; } void setLoadCapacity(int l) { loadCapacity = l; } // ���������� ��������� ������������ Lorry& operator=(const Lorry& other); // ���������� ���������� �����/������ friend std::ostream& operator<<(std::ostream& os, const Lorry& lorry); friend std::istream& operator>>(std::istream& is, Lorry& lorry); }; /** * @brief ����������� ��� ���������� ��� Lorry */ Lorry::Lorry() : Car(), loadCapacity(0) { std::cout << "Lorry default constructor called." << std::endl; } /** * @brief ����������� � ����������� ��� Lorry * @param b �������� ����� * @param c ����� ��������� * @param p �������� * @param l ���������������� */ Lorry::Lorry(const std::string& b, int c, int p, int l) : Car(b, c, p), loadCapacity(l) { std::cout << "Lorry parameterized constructor called." << std::endl; } /** * @brief ����������� ����������� ��� Lorry * @param other ������ ��� ����������� */ Lorry::Lorry(const Lorry& other) : Car(other), loadCapacity(other.loadCapacity) { std::cout << "Lorry copy constructor called." << std::endl; } /** * @brief ���������� ��� Lorry */ Lorry::~Lorry() { std::cout << "Lorry destructor called." << std::endl; } /** * @brief ���������� ��������� ������������ ��� Lorry * @param other ������ ��� ������������ * @return ������ �� ������� ������ */ Lorry& Lorry::operator=(const Lorry& other) { if (this != &other) { Car::operator=(other); loadCapacity = other.loadCapacity; } return *this; } /** * @brief ���������� ��������� ������ ��� Lorry * @param os ����� ������ * @param lorry ������ Lorry ��� ������ * @return ����� ������ */ std::ostream& operator<<(std::ostream& os, const Lorry& lorry) { os << static_cast<const Car&>(lorry) << ", Load Capacity: " << lorry.loadCapacity << " kg"; return os; } /** * @brief ���������� ��������� ����� ��� Lorry * @param is ����� ����� * @param lorry ������ Lorry ��� ����� * @return ����� ����� */ std::istream& operator>>(std::istream& is, Lorry& lorry) { is >> static_cast<Car&>(lorry); std::cout << "Enter load capacity (kg): "; is >> lorry.loadCapacity; return is; } /** * @brief ������� ������� ��������� * ������������� ������ ���� ������� � ������������� �������� */ int main() { std::cout << "=== DEMONSTRATION OF CAR AND LORRY CLASSES ===" << std::endl; // ������������ ������ � ������� Car std::cout << "\n--- CAR CLASS DEMONSTRATION ---" << std::endl; // �������� �������� Car ������� ��������� Car car1; // ����������� ��� ���������� Car car2("Toyota", 4, 150); // ����������� � ����������� Car car3 = car2; // ����������� ����������� std::cout << "\nInitial cars:" << std::endl; std::cout << "car1: " << car1 << std::endl; std::cout << "car2: " << car2 << std::endl; std::cout << "car3: " << car3 << std::endl; // ������������ ��������� ������������ car1 = car2; std::cout << "\nAfter assignment car1 = car2:" << std::endl; std::cout << "car1: " << car1 << std::endl; // ������������ ������ � ��������� � ��������� car1.setBrand("Honda"); car1.setCylinders(6); car1.setPower(200); std::cout << "\nAfter using setters:" << std::endl; std::cout << "car1: " << car1 << std::endl; std::cout << "Brand: " << car1.getBrand() << std::endl; std::cout << "Cylinders: " << car1.getCylinders() << std::endl; std::cout << "Power: " << car1.getPower() << " hp" << std::endl; // ������������ ������ � ������� Lorry std::cout << "\n--- LORRY CLASS DEMONSTRATION ---" << std::endl; // �������� �������� Lorry ������� ��������� Lorry lorry1; // ����������� ��� ���������� Lorry lorry2("Volvo", 6, 300, 5000); // ����������� � ����������� Lorry lorry3 = lorry2; // ����������� ����������� std::cout << "\nInitial lorries:" << std::endl; std::cout << "lorry1: " << lorry1 << std::endl; std::cout << "lorry2: " << lorry2 << std::endl; std::cout << "lorry3: " << lorry3 << std::endl; // ������������ ��������� ������������ ��� Lorry lorry1 = lorry2; std::cout << "\nAfter assignment lorry1 = lorry2:" << std::endl; std::cout << "lorry1: " << lorry1 << std::endl; // ������������ ������ � ��������� � ��������� Lorry lorry1.setBrand("MAN"); lorry1.setLoadCapacity(8000); std::cout << "\nAfter using setters:" << std::endl; std::cout << "lorry1: " << lorry1 << std::endl; std::cout << "Load Capacity: " << lorry1.getLoadCapacity() << " kg" << std::endl; // ������������ ���������� �����/������ std::cout << "\n--- INPUT/OUTPUT DEMONSTRATION ---" << std::endl; Car inputCar; std::cout << "\nEnter data for a car:" << std::endl; std::cin >> inputCar; std::cout << "You entered: " << inputCar << std::endl; Lorry inputLorry; std::cout << "\nEnter data for a lorry:" << std::endl; std::cin >> inputLorry; std::cout << "You entered: " << inputLorry << std::endl; // ������������ ������� count std::cout << "\n--- OBJECT COUNT DEMONSTRATION ---" << std::endl; std::cout << "Total objects created: " << count() << std::endl; // �������� ���������� ������� ��� ������������ �������� { Car tempCar("BMW", 8, 400); std::cout << "After creating temp car: " << count() << " objects" << std::endl; } // tempCar ������������ ����� std::cout << "After destroying temp car: " << count() << " objects" << std::endl; std::cout << "\n=== PROGRAM END ===" << std::endl; return 0; }