Информатика, вопрос задал Kukuwka0Ha0DepeBe , 1 год назад

С++
Реализовать класс Машина.

- Модель

- Скорость

- Цвет (лучше не строкой, а 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);


}

Ответы на вопрос

Ответил professional65
0

#include <iostream>

using namespace std;

enum class Color { RED, BLUE, GREEN, BLACK, WHITE };

class Car {

protected:

string model;

int speed;

Color color;

public:

Car() {

cout << "Car default " << this << endl;

model = "Unknown";

speed = 0;

color = Color::BLACK;

}

Car(string model, int speed, Color color) {

cout << "Car full " << this << endl;

this->model = model;

this->speed = speed;

this->color = color;

}

};

class Truck : public Car {

float cargoVolume;

public:

Truck() : Car() {

cout << "Truck default " << this << endl;

cargoVolume = 0;

}

Truck(string model, int speed, Color color, float cargoVolume)

: Car(model, speed, color) {

cout << "Truck full " << this << endl;

this->cargoVolume = cargoVolume;

}

};

int main() {

Car c1("Sedan", 120, Color::BLUE);

Truck t1("BigTruck", 80, Color::RED, 1500.0);

}

Пример выше показывает класс Car с полями модели, скорости и цвета, а также класс Truck, который наследует класс Car и добавляет поле объема грузового отсека. В main вы создаете объекты обоих классов с использованием различных конструкторов.

Новые вопросы