217: Write a C++ program to model a vehicle hierarchy. Create a base class "Vehicle" with attributes like make and model. Derive classes "Car" and "Motorcycle" from "Vehicle" and add specific attributes like the number of tyres and engine type.
// Write a C++ program to model a vehicle hierarchy. Create a base class "Vehicle"
// with attributes like make and model. Derive classes "Car" and "Motorcycle" from
// "Vehicle" and add specific attributes like the number of tyres and engine type.
#include <iostream>
using namespace std;
class Vehicle
{
private:
string make, model;
public:
Vehicle(string ma = "", string mo = "") { make = ma, model = mo; }
virtual void display_vehicle();
};
void Vehicle::display_vehicle() { cout << "Make: " << make << "\nModel: " << model << endl; }
class Car : protected Vehicle
{
private:
int tyres;
string engine;
public:
Car(string, string, int, string);
void display_vehicle() override;
};
Car::Car(string ma = "", string mo = "", int t = 0, string e = "") : Vehicle(ma, mo)
{
tyres = t;
engine = e;
}
void Car::display_vehicle()
{
Vehicle::display_vehicle();
cout << "No. of tyres: " << tyres << "\nEngine: " << engine << endl;
}
class Motorcycle : protected Vehicle
{
private:
int tyres;
string engine;
public:
Motorcycle(string, string, int, string);
void display_vehicle() override;
};
Motorcycle::Motorcycle(string ma = "", string mo = "", int t = 0, string e = "") : Vehicle(ma, mo)
{
tyres = t;
engine = e;
}
void Motorcycle::display_vehicle()
{
Vehicle::display_vehicle();
cout << "No. of tyres: " << tyres << "\nEngine: " << engine << endl;
}
int main()
{
Car c1("Toyota", "Supra", 4, "255hp");
Motorcycle m1("Yamaha", "YBR", 2, "125CC");
c1.display_vehicle();
cout << endl;
m1.display_vehicle();
return 0;
}
Comments
Post a Comment