Предмет: Информатика, автор: Kukuwka0Ha0DepeBe

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

- Модель

- Скорость

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


}

Ответы

Автор ответа: nitroamogus227
0

Ответ:

#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;

}

Автор ответа: daniilgermany
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;

}

Похожие вопросы
Предмет: Английский язык, автор: solnced228
Find eight mistakes with past forms and correct them. Tick (V) the correct sentences.
1 Lady Jane Grey used to be Queen of England for only nine days in 1553.
2 What were the children doing while their mother was visiting her sister in the hospital?
3 The huge liners doing the journey from Europe to the United States would be extremely comfortable for the first class passengers but those travelling in steerage suffered appalling conditions.
4 The victim was talking on her mobile phone while her attacker struck from behind.
5 Mary didn't join us yesterday evening because she expected a phone call from her son in Australia.
6 Before printing and literacy became widespread, people would get news from the town crier, who would shout out anything newsworthy for the whole town to hear.
7 I'm sure that children didn't use to being so undisciplined when we were at school!
8 The fox cubs were coming into our garden several times during the spring to try to get our chickens.
9 Gerald wasn't used to having such dark hair - do you think he's coloured it?
10 Sorry I'm late. I was expecting to catch the early train but I got held up in traffic on the way to the station.
11 Would your mother work when you were a child or did she stay at home?
12 I found it really difficult when I got the job on the early news programme as I really wasn't used to getting up at five o'clock in the morning! Find eight mistakes with past forms and correct them. Tick (V) the correct sentences.
1 Lady Jane Grey used to be Queen of England for only nine days in 1553.
2 What were the children doing while their mother was visiting her sister in the hospital?
3 The huge liners doing the journey from Europe to the United States would be extremely comfortable for the first class passengers but those travelling in steerage suffered appalling conditions.
4 The victim was talking on her mobile phone while her attacker struck from behind.
5 Mary didn't join us yesterday evening because she expected a phone call from her son in Australia.
6 Before printing and literacy became widespread, people would get news from the town crier, who would shout out anything newsworthy for the whole town to hear.
7 I'm sure that children didn't use to being so undisciplined when we were at school!
8 The fox cubs were coming into our garden several times during the spring to try to get our chickens.
9 Gerald wasn't used to having such dark hair - do you think he's coloured it?
10 Sorry I'm late. I was expecting to catch the early train but I got held up in traffic on the way to the station.
11 Would your mother work when you were a child or did she stay at home?
12 I found it really difficult when I got the job on the early news programme as I really wasn't used to getting up at five o'clock in the morning!
Предмет: Математика, автор: katerinailina199983