Dưới đây là một chương trình C++ đơn giản sử dụng lớp và kế thừa để xây dựng một hệ thống quản lý xe hơi, bao gồm các loại xe khác nhau như xe hơi và xe tải.
### Chương trình C++
```cpp
#include
#include
using namespace std;
// Lớp cơ sở Vehicle
class Vehicle {
protected:
string brand;
string model;
int year;
public:
Vehicle(string b, string m, int y) : brand(b), model(m), year(y) {}
virtual void displayInfo() {
cout << "Brand: " << brand << ", Model: " << model << ", Year: " << year << endl;</p>
}
};
// Lớp dẫn xuất Car
class Car : public Vehicle {
private:
int numberOfDoors;
public:
Car(string b, string m, int y, int doors) : Vehicle(b, m, y), numberOfDoors(doors) {}
void displayInfo() override {
Vehicle::displayInfo();
cout << "Number of doors: " << numberOfDoors << endl;</p>
}
};
// Lớp dẫn xuất Truck
class Truck : public Vehicle {
private:
double loadCapacity;
public:
Truck(string b, string m, int y, double capacity) : Vehicle(b, m, y), loadCapacity(capacity) {}
void displayInfo() override {
Vehicle::displayInfo();
cout << "Load capacity: " << loadCapacity << " tons" << endl;</p>
}
};
int main() {
// Tạo đối tượng Car
Car myCar("Toyota", "Camry", 2020, 4);
// Tạo đối tượng Truck
Truck myTruck("Ford", "F-150", 2019, 1.5);
// Hiển thị thông tin xe
cout << "Car Information:" << endl;</p>
myCar.displayInfo();
cout << "\nTruck Information:" << endl;</p>
myTruck.displayInfo();
return 0;
}
```