Table of Contents#
- Types of Access Modifiers
- Public
- Private
- Protected
- Public Access Modifier
- Common Practices
- Example Usage
- Private Access Modifier
- Common Practices
- Example Usage
- Protected Access Modifier
- Common Practices
- Example Usage
- Best Practices for Using Access Modifiers
- Conclusion
- References
Types of Access Modifiers#
C++ has three main access modifiers:
Public#
Members declared as public can be accessed from anywhere in the program, including outside the class. They are part of the class's interface and are used to expose the functionality of the class to other parts of the program.
Private#
Members declared as private can only be accessed within the class in which they are declared. They are used to hide the internal implementation details of the class from the outside world.
Protected#
Members declared as protected are similar to private members, but they can also be accessed by derived classes. This allows derived classes to access the protected members of the base class.
Public Access Modifier#
Common Practices#
- Use public access for methods that need to be called from other parts of the program. For example, getters and setters are often declared as public so that other classes can access and modify the private data members in a controlled way.
- Public attributes can be used when they represent a fundamental property of the class that needs to be directly accessible, but this should be used sparingly as it can violate the principle of encapsulation.
Example Usage#
#include <iostream>
class Rectangle {
public:
int length;
int width;
int area() {
return length * width;
}
};
int main() {
Rectangle rect;
rect.length = 5;
rect.width = 3;
std::cout << "Area of the rectangle: " << rect.area() << std::endl;
return 0;
}In this example, the length and width attributes and the area method are declared as public. So, we can directly access and modify the attributes and call the method from the main function.
Private Access Modifier#
Common Practices#
- Use private access for data members that should not be directly accessed or modified from outside the class. This helps in maintaining the integrity of the data.
- Private methods are used for internal implementation details that are not relevant to the outside world. For example, helper methods that perform some calculations used by other public methods.
Example Usage#
#include <iostream>
class Circle {
private:
double radius;
double calculateArea() {
return 3.14 * radius * radius;
}
public:
void setRadius(double r) {
if (r > 0) {
radius = r;
}
}
double getArea() {
return calculateArea();
}
};
int main() {
Circle c;
c.setRadius(5.0);
std::cout << "Area of the circle: " << c.getArea() << std::endl;
return 0;
}In this example, the radius attribute and the calculateArea method are private. We cannot access them directly from the main function. Instead, we use the public setRadius method to set the radius and the getArea method to get the area of the circle.
Protected Access Modifier#
Common Practices#
- Use protected access when you want to allow derived classes to access certain members of the base class, but still keep them hidden from the outside world.
- It is useful in inheritance scenarios where the derived class needs to access some internal state or behavior of the base class.
Example Usage#
#include <iostream>
class Shape {
protected:
int sides;
public:
Shape(int s) : sides(s) {}
};
class Triangle : public Shape {
public:
Triangle() : Shape(3) {}
void printSides() {
std::cout << "The triangle has " << sides << " sides." << std::endl;
}
};
int main() {
Triangle t;
t.printSides();
return 0;
}In this example, the sides attribute in the Shape class is declared as protected. The Triangle class, which is derived from the Shape class, can access the sides attribute. However, the sides attribute is still hidden from the outside world.
Best Practices for Using Access Modifiers#
- Start with Private: Always start by making your class members private. Only make them public or protected if there is a valid reason to do so.
- Use Getters and Setters: For private data members, use public getters and setters to control access and modification. This helps in maintaining the integrity of the data.
- Limit Public Interface: Keep the public interface of your class as small as possible. Only expose the necessary functionality to the outside world.
- Understand Inheritance: When using inheritance, carefully consider which members should be protected to allow derived classes to access them without exposing them to the outside world.
Conclusion#
Access modifiers in C++ are essential for implementing encapsulation and ensuring the security and maintainability of your code. By using public, private, and protected access modifiers appropriately, you can control the accessibility of class members and create well - structured and robust classes.
References#
- "C++ Primer" by Stanley Lippman, Josée Lajoie, and Barbara Moo.
- The official C++ documentation on cppreference.com.