C++

MAKE MEMBER DATA PRIVATE

As a general rule of design, you should keep the member data of a class private. Therefore, you must create public functions known as accessor methods to set and get the private member variables. These accessor methods are the member functions that other parts of your program call to get and set your private member variables.

New Term: A public accessor method is a class member function used either to read the value of a private class member variable or to set its value.

Why bother with this extra level of indirect access? After all, it is simpler and easier to use the data, instead of working through accessor functions.

Accessor functions enable you to separate the details of how the data is stored from how it is used. This enables you to change how the data is stored without having to rewrite functions that use the data.

If a function that needs to know a Cat’s age accesses itsAge directly, that function would need to be rewritten if you, as the author of the Cat class, decided to change how that data is stored. By having the function call GetAge(), your Cat class can easily return the right value no matter how you arrive at the age. The calling function doesn’t need to know whether you are storing it as an unsigned integer or a long, or whether you are computing it as needed.

This technique makes your program easier to maintain. It gives your code a longer life because design changes don’t make your program obsolete.

Listing 6.2 shows the Cat class modified to include private member data and public accessor methods. Note that this is not an executable listing.

Listing 6.2. A class with accessor methods.

1: // Cat class declaration

2: // Data members are private, public accessor methods

3: // mediate setting and getting the values of the private data

4:

5: class Cat

6: {

7: public:

8: // public accessors

9: unsigned int GetAge();

10: void SetAge(unsigned int Age);

11:

12: unsigned int GetWeight();

13: void SetWeight(unsigned int Weight);

14:

15: // public member functions

16: Meow();

17:

18: // private member data

19: private:

20: unsigned int itsAge;

21: unsigned int itsWeight;

22:

23: };

Analysis: This class has five public methods. Lines 9 and 10 contain the accessor methods for itsAge. Lines 12 and 13 contain the accessor methods for itsWeight. These accessor functions set the member variables and return their values. The public member function Meow() is declared in line 16. Meow() is not an accessor function. It doesn’t get or set a member variable; it performs another service for the class, printing the word Meow.

The member variables themselves are declared in lines 20 and 21.

To set Frisky’s age, you would pass the value to the SetAge() method, as in

Cat Frisky;

Frisky.SetAge(5); // set Frisky’s age using the public accessor

Privacy Versus Security

Declaring methods or data private enables the compiler to find programming mistakes before they become bugs. Any programmer worth his consulting fees can find a way around privacy if he wants to. Stroustrup, the inventor of C++, said, "The C++ access control mechanisms provide protection against accident—not against fraud." (ARM, 1990.)

The class keyword

Syntax for the class keyword is as follows.

class class_name

{

// access control keywords here

// class variables and methods declared here

};

You use the class keyword to declare new types. A class is a collection of class member data, which are variables of various types, including other classes. The class also contains class functions—or methods—which are functions used to manipulate the data in the class and to perform other services for the class. You define objects of the new type in much the same way in which you define any variable. State the type (class) and then the variable name (the object). You access the class members and functions by using the dot (.) operator. You use access control keywords to declare sections of the class as public or private. The default for access control is private. Each keyword changes the access control from that point on to the end of the class or until the next access control keyword. Class declarations end with a closing brace and a semicolon.

Example 1

class Cat

{

public:

unsigned int Age;

unsigned int Weight;

void Meow();

};

Cat Frisky;

Frisky.Age = 8;

Frisky.Weight = 18;

Frisky.Meow();

Example

class Car

{

public: // the next five are public

void Start();

void Accelerate();

void Brake();

void SetYear(int year);

int GetYear();

private: // the rest is private

int Year;

Char Model [255];

}; // end of class declaration

Car OldFaithful; // make an instance of car

int bought; // a local variable of type int

OldFaithful.SetYear(84) ; // assign 84 to the year

bought = OldFaithful.GetYear(); // set bought to 84

OldFaithful.Start(); // call the start method

DO declare member variables private. DO use public accessor methods. DON’T try to use private member variables from outside the class. DO access private member variables from within class member functions.

Back to Index