Добавил:
vvrstcnho
Рад, если кому-то помог
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз:
Предмет:
Файл:Лабораторные работы С++ (для ИВТ) / Готовые лабы С++ / Лаба2 / Laba 2 (21)
.cpp#include <iostream>
#include <string>
#include <windows.h>
using namespace std;
class tiger {
private:
string name;
string color;
int weight;
static int count;
public:
tiger() : name(""), color(""), weight(0) {
count++;
cout << "Создан тигр по умолчанию" << endl;
}
tiger(string n) : name(n), color(""), weight(0) {
count++;
cout << "Создан тигр с именем: " << n << endl;
}
tiger& setname(string n) {
name = n;
cout << "Установлено имя: " << n << endl;
return *this;
}
tiger& setcolor(string c) {
color = c;
cout << "Установлен цвет: " << c << endl;
return *this;
}
tiger& setweight(int w) {
weight = w;
cout << "Установлен вес: " << w << endl;
return *this;
}
void print() {
cout << "Тигр: " << name << ", цвет: " << color << ", вес: " << weight << " кг" << endl;
}
int getWeight() const { return weight; }
static int getCount() { return count; }
};
int tiger::count = 0;
// Функция для сравнения веса тигров (переименована чтобы избежать конфликта)
bool compareWeight(const tiger& t1, const tiger& t2) {
return t1.getWeight() < t2.getWeight();
}
int main() {
SetConsoleOutputCP(65001);
cout << "=== Задание 2.21 - Тигры ===" << endl;
tiger T1, T2("Kuzya");
cout << "\nУстановка параметров для T1:" << endl;
T1.setname("Murzik").setcolor("light").setweight(200);
cout << "\nИнформация о тиграх:" << endl;
cout << "T1: "; T1.print();
cout << "T2: "; T2.print();
cout << "\nИзменение имени T2:" << endl;
T2.setname("Tigr");
cout << "\nСравнение тигров по весу:" << endl;
if(compareWeight(T2, T1)) {
cout << "T2 меньше T1: "; T2.print();
} else {
cout << "T1 меньше T2: "; T1.print();
}
cout << "\nКоличество объектов-тигров: " << tiger::getCount() << endl;
return 0;
}
Соседние файлы в папке Лаба2
