You are developing a software library in C++ that will be used by other developers, and you want to ensure that the internal variables of your classes are not accessible while the methods are. How might encapsulation be implemented to achieve this while providing a clear API? 

  • Make all variables public and methods private. 
  • Make all methods and variables private. 
  • Make variables private and methods public. 
  • Only use static variables and methods.
Encapsulation is achieved by bundling data (variables) and methods that operate on the data into a single unit (class) and restricting the direct access to some of the object's components. In C++, this is implemented by declaring variables as private and providing public methods (API) to access and manipulate them.
Add your answer
Loading...

Leave a comment

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