/*
A key feature of C++ classes is inheritance. Inheritance allows to create classes which are derived from other
classes, so that they automatically include some of its "parent's" members, plus its own.
*/

#include <iostream>
using namespace std;

class CPolygon {
      protected:
             int width, height;
      public:
             void set_values (int a, int b)
             { 
                  width=a; 
                  height=b;
                  }
};

class CRectangle: public CPolygon {
      public:
             int area ()
             { 
                 return (width * height); 
             }
};

class CTriangle: public CPolygon {
      public:
             int area ()
             { 
                 return (width * height / 2); 
             }
};

int main () {
    CRectangle rect;
    CTriangle trgl;

    rect.set_values (4,5);
    trgl.set_values (4,5);
    
    cout << rect.area() << endl;
    cout << trgl.area() << endl;
        
    std::cin.get();
    return (0);       
}

/*
The objects of the classes CRectangle and CTriangle each contain members inherited from CPolygon. These are:
width, height and set_values().

The protected access specifier is similar to private. Its only difference occurs in fact with inheritance. When a
class inherits from another one, the members of the derived class can access the protected members inherited
from the base class, but not its private members.

Since we wanted width and height to be accessible from members of the derived classes CRectangle and
CTriangle and not only by members of CPolygon, we have used protected access instead of private.
We can summarize the different access types according to who can access them in the following way:
       
Access                       public protected private
members of the same class     yes    yes        yes
members of derived classes    yes    yes         no
not members                   yes     no         no
*/
