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

Popular posts from this blog

153: Write a program to read an amount (integer value) and break the amount into smallest possible number of bank notes. Note: The possible banknotes are 500, 100, 50, 20, 10, 5, 2, and 1

206: Write a program to create a class named "Circle" which has the property "radius". Define functions to calculate the area and circumference of the circle.

221: // In Task 2, we discussed multilevel inheritance with parameterized constructors for Student, UndergraduateStudent, and GraduateStudent classes in a university management system. Can you explain the advantages of using multilevel inheritance with specific details about the functions and data members in these classes? How were the parameterized constructors (e.g., setting student name, age, and ID) used to ensure that each class in the hierarchy correctly initializes its properties, such as creating an UndergraduateStudent named "John," aged 20, with a student ID of 12345