C++ Access Specifiers and Inheritance

Access specifiers in C++ are used to control the accessibility of class members. There are three types of access specifiers in C++: public, protected, and private. The access level of a class member determines which parts of the program can access the member. In this article, we will discuss how access specifiers are used in inheritance and the effect they have on derived classes.

Public Access Specifier

When a class member is declared as public, it can be accessed from anywhere in the program. This means that any function or object, inside or outside of the class, can access the member. In the case of inheritance, if a base class member is declared as public, the derived class can also access the member.

class Shape {
   public:
      int width;
      int height;
};

class Rectangle: public Shape {
   public:
      int getArea() { 
         return (width * height); 
      }
};

int main() {
   Rectangle rect;
   rect.width = 5;
   rect.height = 7;

   cout << "Area of Rectangle: " << rect.getArea() << endl;

   return 0;
}

In this example, the class Shape has two public members: width and height. The class Rectangle inherits these members from the Shape class and can access them directly. The output of the program will be:

Area of Rectangle: 35

Protected Access Specifier

When a class member is declared as protected, it can be accessed from within the class and its derived classes. However, the member cannot be accessed from outside of the class and its derived classes. In the case of inheritance, if a base class member is declared as protected, the derived class can access the member, but other parts of the program cannot.

class Shape {
   protected:
      int width;
      int height;
};

class Rectangle: public Shape {
   public:
      int getArea() { 
         return (width * height); 
      }
};

int main() {
   Rectangle rect;
   rect.width = 5; // Compile-time error
   rect.height = 7; // Compile-time error

   return 0;
}

In this example, the class Shape has two protected members: width and height. The class Rectangle inherits these members from the Shape class, but it cannot access them directly from outside the class. If you try to access the width and height members in the main function, it will give a compile-time error. However, the Rectangle class can still use the members in its member functions, like getArea().

Private Access Specifier

When a class member is declared as private, it can only be accessed from within the class. It cannot be accessed from outside the class or its derived classes. In the case of inheritance, if a base class member is declared as private, the derived class cannot access the member directly. However, the derived class can still access the member through a public or protected member function of the base class.

class Shape {
   private:
      int width;
      int height;
   public:
      void setDimensions(int w, int h) {
         width = w;
         height = h;
      }
};

class Rectangle: public Shape {
   public:
      int getArea() { 
         return (width * height); 
      }
};

int main() {
   Rectangle rect;
   rect.setDimensions(5, 7);
   cout << "Area of Rectangle: " << rect.getArea() << endl;

   return 0;
}

In this example, the class Shape has two private members: width and height. The class Rectangle inherits these members from the Shape class, but it cannot access them directly. However, the Rectangle class can still use the members in its member functions, like getArea(), by calling the public member function setDimensions() of the base class. The output of the program will be:

Area of Rectangle: 35

Conclusion

Access specifiers in C++ are used to control the accessibility of class members. Public members can be accessed from anywhere, protected members can be accessed from within the class and its derived classes, and private members can only be accessed from within the class. In the case of inheritance, the access level of a base class member determines the accessibility of the member in the derived class. Understanding the use of access specifiers in inheritance is crucial for creating robust and maintainable code in C++.

Leave a Reply

Your email address will not be published. Required fields are marked *