fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. class Engine{
  5. private:
  6. int power;
  7. public:
  8. Engine (int p=0){
  9. power = p;
  10. }
  11. //встановленння значення
  12. void setPower(int p){
  13. power = p;
  14. }
  15. //отримання значення
  16. int getPower(){
  17. return power;
  18. }
  19.  
  20. };
  21. class Vehicle{
  22. private:
  23. Engine engine;
  24. int maxSpeed;
  25. public:
  26. Vehicle (int power, int speed) : engine(power), maxSpeed(speed) {}
  27. Vehicle (): engine (0), maxSpeed (0) {};
  28. void setEnginePower(int p){
  29. engine.setPower(p);
  30. }
  31. int getEnginePower(){
  32. return engine.getPower();
  33. }
  34. void setMaxSpeed(int speed){
  35. maxSpeed = speed;
  36. }
  37. int getMaxSpeed(){
  38. return maxSpeed;
  39. }
  40. virtual string getType() { // визначення віртуальної функції
  41. return "Unknown vehicle";
  42. }
  43. };
  44. class Car: public Vehicle{
  45. private:
  46. int numberOfSeats;
  47. public:
  48. Car(int power, int speed, int seats): Vehicle(power,speed), numberOfSeats(seats) {}
  49. void setNumberOfSeats(int seats){
  50. numberOfSeats = seats;
  51. }
  52. int getNumberOfSeats(){
  53. return numberOfSeats;
  54. }
  55. string getType() override{
  56. return "Car";
  57. }
  58. void printInfo(){
  59. cout << "Type:" << getType()<<endl;
  60. cout << "EnginePower:" << getEnginePower()<<endl;
  61. cout << "Max speed:" << getMaxSpeed()<<endl;
  62.  
  63. }
  64. };
  65.  
  66. class Truck: public Vehicle{
  67. private:
  68. double cargoCapacity;
  69. public:
  70. Truck(int power, int speed, double cargoCapacity): Vehicle(power,speed), cargoCapacity(cargoCapacity) {}
  71. void setCargoCapacity (double capacity){
  72. cargoCapacity = capacity;
  73. }
  74. double getCargoCapacity(){
  75. return cargoCapacity;
  76. }
  77. string getType() override{
  78. return "Truck";
  79. }
  80. void printInfo(){
  81. cout << "Type:" << getType()<<endl;
  82. cout << "EnginePower:" << getEnginePower()<<endl;
  83. cout << "Max speed:" << getMaxSpeed()<<endl;
  84. cout << "Cargo capacity:" << getCargoCapacity()<<endl;
  85. }
  86. };
  87.  
  88.  
  89. int main() {
  90. Car mycar(300,200,2);
  91. Truck mytruck(400,180, 12);
  92. cout<<"Car Info" << endl;
  93. mycar.printInfo();
  94. cout<<"Trusk Info" << endl;
  95. mytruck.printInfo();
  96. return 0;
  97. }
Success #stdin #stdout 0s 5320KB
stdin
Standard input is empty
stdout
Car Info
Type:Car
EnginePower:300
Max speed:200
Trusk Info
Type:Truck
EnginePower:400
Max speed:180
Cargo capacity:12