Josif_tepe

Untitled

Sep 12th, 2026
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.13 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4.  
  5. class Vehicle {
  6. protected:
  7.     string name;
  8.     int year;
  9.    
  10. public:
  11.     Vehicle() {}
  12.     Vehicle(string name, int year) {
  13.         this->name = name;
  14.         this->year = year;
  15.     }
  16.     Vehicle(const Vehicle & tmp) {
  17.         this->name = tmp.name;
  18.         this->year = tmp.year;
  19.     }
  20.    
  21.     ~Vehicle() {}
  22.    
  23.     virtual void print() {
  24.         cout << name << " " << year << endl;
  25.     }
  26.    
  27. };
  28.  
  29. class MotorVehicle : public Vehicle {
  30. protected:
  31.     double consume;
  32.     int tyres;
  33.    
  34. public:
  35.     MotorVehicle() : Vehicle() {}
  36.     MotorVehicle(string name, int year, double consume, int tyres) : Vehicle(name, year) {
  37.         this->consume = consume;
  38.         this->tyres = tyres;
  39.     }
  40.    
  41.     MotorVehicle(const MotorVehicle &tmp) : Vehicle(tmp) {
  42.         this->consume = tmp.consume;
  43.         this->tyres= tmp.tyres;
  44.     }
  45.    
  46.     virtual void print() override {
  47.         cout << consume << " " << tyres << endl;
  48.     }
  49.    
  50. };
  51.  
  52. int main() {
  53.    
  54.    
  55.     Vehicle * v = new MotorVehicle("car", 2025, 20, 4);
  56.    
  57.     v->print();
  58.  
  59.     return 0;
  60. }
  61.  
Advertisement
Add Comment
Please, Sign In to add comment