CPP Access Specifiers

What is Inheritance in C++?

  • When something is said to be a kind of something else, it is implied that it is a specialization of that thing.
  • This kind of relation is normally named as a "is-a" relationships.
    A class model:
  • A rectangle, Square and Circle is a more specialized kind of a Shape.
  • A Cuboid is build upon a Rectangle but something more than a Rectangle.
  • A Cube is build upon a Square but something more than a Square.
  • A Cylinder is build upon a Circle but something more than a Circle.
    Example on how to specify an Inheritance in C++:
    class Person {
     ...
    };
    
    class Employee : public Person {
     ...
    };
  • A class that is more specialized of a class is called a derived class or subclass, and the class that the subclass is driven from is called the base class.
  • A base class can have many subclasses.
  • To specify an Inheritance you use a colon and Inheritance access specifier (we normally use public inheritance) after the subclass name and then the name of the base class.

Public, protected and private Inheritance?

  • Members of a class can have one of 3 access specifiers:
    Member access Description
    public All public members are fully visible to all other classes and objects.
    protected All protected members are fully visible to subclasses, but are otherwise private.
    private All private members are not available from outside of the class or objects of the class. Will prevent access from subclasses as well.
  • Any class can also be specified with one of 3 types of Inheritance access specifiers:
    Inheritance access specifier Description
    public When deriving a class from a public base class, public members of the base class become public members of the derived class and protected members of the base class become protected members of the derived class. A base class's private members are never accessible directly from a derived class.
    protected When deriving from a protected base class, public and protected members of the base class become protected members of the derived class.
    private When deriving from a private base class, public and protected members of the base class become private members (e.g., the functions become utility functions) of the derived class.

  • Private and protected inheritance are NOT is-a relationships.
    Class Inheritance example:
    #include <iostream>
    using namespace std;
    
    class Shape {
    private:
      int color;
    protected:
      // available to be change in drived classes
      int area;
    public:
      void setColor(int Col) {color=Col;}
      int getColor() {return color; }
      int getArea() {return area;}
      Shape() {color= 0xff00ff; area=0; }
    };
    class Rectangle : public Shape {
    private:
      int height;
      int width;
      void calcArea() {
        // as area is protected in Shape
        area=height*width;
      }
    public: 
      Rectangle(int height,int width) {
        this->height=height;
        this->width=width;
        calcArea();
      }
      void setWidth(int width) { 
        this->width=width;
        calcArea();
      }
    };
    
    int main(int argc, char **argv) {
      Shape shape;
      cout << "Shape default color is: " << hex << shape.getColor() << "\n";
      Rectangle rectangle(3,4);
      cout << "Rectangle area is: " << dec << rectangle.getArea()<< "\n";
      rectangle.setWidth(24);
      cout << "Rectangle area is now : " << dec << rectangle.getArea()<< "\n";
      return 0;
    }
    When we run this application, the result will be:
    Shape default color is: ff00ff
    Rectangle area is: 12
    Rectangle area is now : 72

    You can download this example here (needed tools can be found in the right menu on this page).

© 2010 by Finnesand Data. All rights reserved.
This site aims to provide FREE programming training and technics.
Finnesand Data as site owner gives no warranty for the correctness in the pages or source codes.
The risk of using this web-site pages or any program codes from this website is entirely at the individual user.