С++
Реализовать класс Машина.
- Модель
- Скорость
- Цвет (лучше не строкой, а enum)
От класса машина унаследовать класс Грузовик.
Грузовику добавить поле объем грузового отсека.
Унаследоваться, правильно вызывать конструкторы, в мейне все потестить.
Код:#include
using namespace std;
class Human {
protected:
string name;
int age;
public:
Human() {
cout << "Human default " << this << endl;
age = 0;
}
Human(string name, int age) {
cout << "Human full " << this << endl;
this->name = name;
this->age = age;
}
string GetName() const {
return name;
}
int GetAge() const {
return age;
}
void SetName(const string name) {
this->name = name;
}
void SetAge(const int age) {
this->age = age;
}
};
class Student : public Human {
float averageGrade;
public:
Student() : Human() {
cout << "Student default " << this << endl;
averageGrade = 0;
}
Student(string name, int age, float averageGrade) : Human(name, age) {
cout << "Student full " << this << endl;
this->averageGrade = averageGrade;
}
float GetAverageGrade() const {
return averageGrade;
}
void SetAverageGrade(float averageGrade) {
this->averageGrade = averageGrade;
}
};
int main() {
Student st("Nikita", 21, 10.9);
}
Ответы
Ответ:
#include <iostream>
#include <string>
//set colors(enum)
enum class Color {
RED,
BLUE,
GREEN,
WHITE,
BLACK
};
// Create classes and constructors...
class Car {
protected:
std::string model;
double speed;
Color color;
public:
Car() {
std::cout << "Car default " << this << std::endl;
model = "";
speed = 0.0;
color = Color::WHITE;
}
Car(std::string model, double speed, Color color) {
std::cout << "Car full " << this << std::endl;
this->model = model;
this->speed = speed;
this->color = color;
}
std::string GetModel() const {
return model;
}
double GetSpeed() const {
return speed;
}
Color GetColor() const {
return color;
}
void SetModel(const std::string& model) {
this->model = model;
}
void SetSpeed(double speed) {
this->speed = speed;
}
void SetColor(Color color) {
this->color = color;
}
};
class Truck : public Car {
private:
double cargoVolume;
public:
Truck() : Car() {
std::cout << "Truck default " << this << std::endl;
cargoVolume = 0.0;
}
Truck(std::string model, double speed, Color color, double cargoVolume)
: Car(model, speed, color) {
std::cout << "Truck full " << this << std::endl;
this->cargoVolume = cargoVolume;
}
double GetCargoVolume() const {
return cargoVolume;
}
void SetCargoVolume(double volume) {
cargoVolume = volume;
}
};
//create main function
int main() {
Car car("Mercedes", 160.0, Color::GREEN); //input case
Truck truck("Ford", 80.0, Color::BLACK, 1000.0); //input case
std::cout << "Car model: " << car.GetModel() << ", Speed: " << car.GetSpeed()
<< ", Color: " << static_cast<int>(car.GetColor()) << std::endl;
std::cout << "Truck model: " << truck.GetModel() << ", Speed: " << truck.GetSpeed()
<< ", Color: " << static_cast<int>(truck.GetColor())
<< ", Cargo Volume: " << truck.GetCargoVolume() << std::endl;
return 0;
}
#include <iostream>
enum class Color {
RED,
BLUE,
GREEN,
YELLOW
};
class Car {
protected:
std::string model;
int speed;
Color color;
public:
Car(const std::string& model, int speed, Color color)
: model(model), speed(speed), color(color) {}
void display() {
std::cout << "Model: " << model << std::endl;
std::cout << "Speed: " << speed << " km/h" << std::endl;
std::cout << "Color: ";
switch (color) {
case Color::RED:
std::cout << "Red";
break;
case Color::BLUE:
std::cout << "Blue";
break;
case Color::GREEN:
std::cout << "Green";
break;
case Color::YELLOW:
std::cout << "Yellow";
break;
}
std::cout << std::endl;
}
};
class Truck : public Car {
private:
int cargoVolume;
public:
Truck(const std::string& model, int speed, Color color, int cargoVolume)
: Car(model, speed, color), cargoVolume(cargoVolume) {}
void display() {
Car::display();
std::cout << "Cargo Volume: " << cargoVolume << " cubic meters" << std::endl;
}
};
int main() {
Car car("Toyota Camry", 180, Color::BLUE);
car.display();
std::cout << "------------------------" << std::endl;
Truck truck("Volvo FH16", 120, Color::RED, 50);
truck.display();
return 0;
}