5.4 ACCESSING CLASS MEMBERS

One of the salient features of OOP is to qualify data members of the class as private and public (Protected will be discussed later). Private members can be referenced only within the class and not outside i.e. in the function main or by methods of other class or independently written functions. Public data members can be referenced anywhere (by dot operator mechanism). Similarly, a public method can also be called anywhere (object_name. method_name (parameters)).

 

images

 

Figure 5.2 Public and private access

 

A private data member of a class can be referenced by the private as well as public method of that class. When ...

Get Object Oriented Programming with C++, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.