Private Versus Public

Other keywords are used in the declaration of a class. Two of the most important are public and private.

All member data and functions are private by default. Private members can be accessed only within methods of the class itself. Public members can be accessed through any object of the class. This distinction is important, but can be confusing. To make it a bit clearer, consider an example from earlier in this chapter:

class Cat
{
    unsigned int  itsAge;
    unsigned int  itsWeight;
    Meow();
};

In this declaration, itsAge, itsWeight, and Meow() are all private, because all members of a class are private by default. This means ...

Get Sams Teach Yourself C++ in 24 Hours, Third 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.