Public versus Private members in C++

By: Baski Viewed: 193 times  Printer Friendly Format    


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:

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.

The program below shows the declaration of a Cat class with public member variables.

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.

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.

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."



Most Viewed Articles (in C++ )

Latest Articles (in C++)

Comment on this tutorial

Subscribe to Tutorials

Related Tutorials

Archived Comments

1. good.
View Tutorial          By: Karthik at 2007-10-20 07:03:20

2. really great, goes more in depth than cplusplus.co
View Tutorial          By: ss80 at 2008-07-02 06:45:47

3. better
View Tutorial          By: sanjeev at 2010-06-12 10:38:04

4. the way they explain those difference with same e.
View Tutorial          By: sahil at 2010-07-04 06:13:01

5. its really helpfull to understand.
View Tutorial          By: rakhi at 2010-08-08 12:45:05

6. good really helpfull
View Tutorial          By: Haribabu at 2010-09-08 22:33:36

7. it is really BEST answer of my question.
MR

View Tutorial          By: Ramesh genwa at 2010-09-09 07:15:37

8. Good
View Tutorial          By: Manjeet kumar at 2010-11-20 19:26:16

9. nice... thanks for the explain, you safe my day...
View Tutorial          By: Joseph Louis at 2011-05-11 08:53:53

10. its superb
View Tutorial          By: chotu jain at 2011-05-24 08:47:22

11. Impressing answer guys...
View Tutorial          By: Jayeeta Pal at 2011-06-28 03:18:09

12. Impressing answer guys...
View Tutorial          By: Jayeeta Pal at 2011-06-28 03:18:26

13. It is really very good good notes of Public versus
View Tutorial          By: Nitin Gavande at 2011-07-16 08:46:33

14. It is really very good good notes of Public versus
View Tutorial          By: Nitin Gavande at 2011-07-16 08:47:56

15. Very good.
But is this (Accessor functions

View Tutorial          By: peter at 2011-07-20 13:13:44

16. very good session
View Tutorial          By: prashanth v at 2011-09-22 18:38:10

17. crystal clear. even some experienced C++ programme
View Tutorial          By: Bala at 2011-09-27 10:19:35

18. Is it possible to define a class such that the pri
View Tutorial          By: Smithy at 2012-05-01 22:50:58

19. Muchas gracias, me ayudo mucho ...
View Tutorial          By: Martin at 2012-05-08 03:02:52

20. In listing 6.2 I don't see the variable name itsAg
View Tutorial          By: JC at 2013-04-16 01:49:52

21. My brotheer suggested I may like this webb site. H
View Tutorial          By: fifa17 at 2017-05-11 12:30:46

22. Vou imediatamente apreender seu rss como eu no pod
View Tutorial          By: como emagrecer em 2 semanas at 2017-05-11 17:34:38

23. Hi, i think that i saw you visited my web site so
View Tutorial          By: cheap rs gold at 2017-05-13 03:05:11

24. This paragraph will assist the internet visitors <
View Tutorial          By: __________ ________ at 2017-05-15 03:16:51

25. My bother recommended I may like this website. He
View Tutorial          By: mmorpg where you can fly at 2017-05-22 10:06:53