Inheritance Exercises

  < Previous  Next >
  1. What is an appropriate inheritance hierarchy between the following objects (who inherits from who?):
    • Employee
    • Cashier
    • Manager
  2. Brainstorm other examples of multiple objects that would fit into an inheritance hierarchy. Share them with your neighbor(s).
  3. Given the code below, add a Circle class that inherits from Shape and has a single private data member double m_Radius and a method void setRadius(double r) (which sets m_Area). Are there any changes that should be made to the Shape class?
    #include <iostream>
    using std::cout;
    
    class Shape {
    private:
        double m_Area;
    public:
        Shape(): m_Area(0.0) { }
        Shape( double area) : m_Area( area) { }
        void setArea( double a){ m_Area = a; }
        double getArea(){ return m_Area; }
    };
    
    int main(){
        Circle cir;
        cir.setRadius(5.0);
        cir.print();
        return 0;
    }
    
  4. Share with a neighbor 3 examples for each of the following relationships:
    • Public inheritance ("is a")
    • Private inheritance ("as a")

Last Modified: