//illustrates inheritance in a more complex example //automobile is my base class #ifndef AUTOMOBILE_H #define AUTOMOBILE_H #include //output direct from the class is not best practice but OK for the purpose of illustration #include using namespace std; // The Automobile base class holds general data for all vehicles // in inventory. Its functions are simple and are coded inline class Automobile { protected: string make; // The auto's make int model; // The auto's year model int mileage; // The auto's mileage double price; // The auto's price public: // Default constructor Automobile() { make = ""; model = 0; mileage = 0; price = 0.0; } // Constructor with an initializer list // At present no attempt has been made to validate input parameters Automobile(string autoMake, int autoModel, int autoMileage, double autoPrice):make(autoMake),model(autoModel), mileage(autoMileage), price(autoPrice) { // empty code body for now You could check the values and set defaults // you might add code to force data to make sense such as 0 < = mileage <= 600000 You } // Accessors string getMake() const { return make; } int getModel() const { return model; } int getMileage() const { return mileage; } double getPrice() const { return price; } //A virtual base class requires that there are implemented derived classes //It also means that you cannot instantiate an object of the base class //But its member fcns do execute. virtual void displayVehicleInfo() const { cout << fixed << showpoint << setprecision(2); cout << "Model " << model << endl; cout << "make " << make << endl; cout << "mileage " << mileage << " miles " << endl; cout << "Price: $" << price << endl; } //pure virtual function //virtual void displayVehicleInfo() const = 0; //It is important that you provide a virtual base class destructor anytime that a base class //contains a virtual function. This ensure that all objects of derived classes //are destroyed properly, i.e., both base class and derived class destructors are called. //Do this even if there are no explicit derived class destructors supplied. //Do this even when the destructor does nothing as in this example! virtual ~Automobile() {} }; #endif // AUTOMOBILE_H