![]() ![]() |
![]() ![]() ![]() ![]() |
![]() ![]() |
![]() | Section 7: Class Declarations |
At the beginning of Section 7, we used the following example of a class declaration:
class Player { int health; int strength; int agility; void move(); void attackMonster(); void getTreasure(); };Although this is perfectly legal C++, we left out an important part of class declarations to simplify the example. Every data member and member function in a class is either
private
,
public
, or protected
. We'll explain the
meaning of each in this chapter.
Specifying that a data member or member function is
private
means that it can only be accessed from
within the class. For data members, this means that the data can be
accessed or modified only while inside a member function of the class.
For member functions, this means that the function can be called only
while inside another member function of the class. This is the
default, if no specifiers are used.
Specifying that a data member or member function is
public
means that it can be accessed from anywhere
in your code. The public
specifier is less restrictive
than private
.
Specifying that a data member or member function is
protected
means that it can only be accessed from within
the class or a subclass. We haven't yet talked about subclasses
(Section 10), so don't worry about using protected
just
yet. The protected
specifier is less restrictive than
private
but more restrictive than public
.
Using private
, public
, and
protected
are easy. The specifier affects all data
members and member functions until the next occurrence of a specifier.
Here's the Player
class, with protection specifiers
added:
class Player { private: int health; int strength; int agility; public: void move(); void attackMonster(); void getTreasure(); };In this example, the
private
keyword begins a
private
section encompassing the three data members. The
public
keyword specifies that the next three member
functions should be public
. So, only code which is in a
Player
member function can access the data members, while
any code in the program is free to call the member functions in the
Player
class.
Specifiers allow a class to be very complex, with many member
functions and data members, while having a simple public
interface that other classes can use. A class which has two
hundred data members and one hundred member functions can be very
complicated to write; but if there are only three or four
public
member functions, and the rest are all
private
, it can be easy for someone to learn how to use
the class.
He only needs to understand how to use a small handful of
public
functions, and doesn't need to bother with the two
hundred data members, because he's not allowed to access this data.
He can only access the private
data through the class'
public interface.
Without a doubt, in a small program, using these specifiers may seem
unnecessary. However, they are worth understanding if you plan to do
any program of reasonable size (more than a couple hundred lines). In
general, it is good practice to make data members
private
. Member functions which must be called from
outside the class should be public
, and member functions
which are only called from within the class (also known as "helper
functions") should probably be private
. These specifiers
are especially useful in a large program involving more than one
programmer.
![]() ![]() |
![]() ![]() ![]() ![]() |
![]() ![]() |