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

88: Using switch statement Write a C program to input marks of five subjects Physics, Chemistry, Biology, Mathematics and Computer. Calculate percentage and grade according to following: // Percentage >= 90% : Grade A Percentage >= 80% : Grade B Percentage >= 70% : Grade C Percentage >= 60% : Grade D Percentage >= 40% : Grade E Percentage < 40% : Grade F

205: Book Catalog: Define a struct to represent a book with attributes like title, author, and publication year. Write a program to create a catalog of books by taking user input and display books published after a certain year.

15: Take input of age and name of 3 people by user and determine oldest and youngest among them with his age. -_-_-_-_-_-_-_-_-(line with spaces input concept)-_-_-_-_-_-_-_-_