19. Vehicle Inheritance Public

#include <iostream>
using namespace std;

class Vehicle {
public:
    void printCategory() {
        cout << "Generic Vehicle\n";
    }
};

class Car : public Vehicle {
public:
    void printCar() {
        cout << "Car Vehicle\n";
    }
};

int main() {
    Car c;
    c.printCategory();
    c.printCar();
    return 0;
}

 

Solution Explanation

  • Car inherits from Vehicle using public inheritance.
  • This makes printCategory() directly callable from a Car object.
  • The derived class adds an additional behavior with printCar().
     

Layman’s Terms

car is just a specialized vehicle. It inherits everything that makes it a vehicle and adds its own unique description.


Significance for Embedded Developers

This models situations like:

  • generic device driver (Vehicle) with common setup functions.
  • specific driver (Car) with extra configuration for that hardware.
     
Loading...

Input

Expected Output

Generic Vehicle Car Vehicle