C++

PRIVATE VERSUS PUBLIC

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

All members of a class—data and methods—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 both important and confusing. To make it a bit clearer, consider an example from earlier in this unit:

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 that unless you specify otherwise, they are private.

However, if you write

Cat Boots;

Boots.itsAge=5; // error! can’t access private data!

the compiler flags this as an error. In effect, you’ve said to the compiler, "I’ll access itsAge, itsWeight, and Meow() only from within member functions of the Cat class." Yet here you’ve accessed the itsAge member variable of the Boots object from outside a Cat method. Just because Boots is an object of class Cat, that doesn’t mean that you can access the parts of Boots that are private.

This is a source of endless confusion to new C++ programmers. I can almost hear you yelling, "Hey! I just said Boots is a cat. Why can’t Boots access his own age?" The answer is that Boots can, but you can’t. Boots, in his own methods, can access all his parts—public and private. Even though you’ve created a Cat, that doesn’t mean that you can see or change the parts of it that are private.

The way to use Cat so that you can access the data members is

class Cat

{

public:

unsigned int itsAge;

unsigned int itsWeight;

Meow();

};

Now itsAge, itsWeight, and Meow() are all public. Boots.itsAge=5 compiles without problems.

Listing 6.1 shows the declaration of a Cat class with public member variables.

Listing 6.1. Accessing the public members of a simple class.

1: // Demonstrates declaration of a class and

2: // definition of an object of the class,

3:

4: #include <iostream.h> // for cout

5:

6: class Cat // declare the class object

7: {

8: public: // members which follow are public

9: int itsAge;

10: int itsWeight;

11: };

12:

13:

14: void main()

15: {

16: Cat Frisky;

17: Frisky.itsAge = 5; // assign to the member variable

18: cout << "Frisky is a cat who is " ;

19: cout << Frisky.itsAge << " years old.\n";

20:

Output: 

Frisky is a cat who is 5 years old.

Analysis: Line 6 contains the keyword class. This tells the compiler that what follows is a declaration. The name of the new class comes after the keyword class. In this case, it is Cat.
The body of the declaration begins with the opening brace in line 7 and ends with a closing brace and a semicolon in line 11. Line 8 contains the keyword public, which indicates that everything that follows is public until the keyword private or the end of the class declaration.

Lines 9 and 10 contain the declarations of the class members itsAge and itsWeight.

Line 14 begins the main function of the program. Frisky is defined in line 16 as an instance of a Cat—that is, as a Cat object. Frisky’s age is set in line 17 to 5. In lines 18 and 19, the itsAge member variable is used to print out a message about Frisky.

NOTE: Try commenting out line 8 and try to recompile. You will receive an error on line 17 because itsAge will no longer have public access. The default for classes is private access.

Back to Index