Public versus Private members in C++

By: Baski Emailed: 1590 times Printed: 2041 times    

Latest comments
By: rohit kumar - how this program is work
By: Kirti - Hi..thx for the hadoop in
By: Spijker - I have altered the code a
By: ali mohammed - why we use the java in ne
By: ali mohammed - why we use the java in ne
By: mizhelle - when I exported the data
By: raul - no output as well, i'm ge
By: Rajesh - thanx very much...
By: Suindu De - Suppose we are executing

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


C++ Home | All C++ Tutorials | Latest C++ Tutorials

Sponsored Links

If this tutorial doesn't answer your question, or you have a specific question, just ask an expert here. Post your question to get a direct answer.



Bookmark and Share

Comments(12)


1. View Comment

good.

View Tutorial          By: Karthik at 2007-10-20 07:03:20
2. View Comment

really great, goes more in depth than cplusplus.com

View Tutorial          By: ss80 at 2008-07-02 06:45:47
3. View Comment

better

View Tutorial          By: sanjeev at 2010-06-12 10:38:04
4. View Comment

its really helpfull to understand.

View Tutorial          By: rakhi at 2010-08-08 12:45:05
5. View Comment

it is really BEST answer of my question.
MR.Ramesh genwa.


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

nice... thanks for the explain, you safe my day... XD

View Tutorial          By: Joseph Louis at 2011-05-11 08:53:53
7. View Comment

its superb

View Tutorial          By: chotu jain at 2011-05-24 08:47:22
8. View Comment

Impressing answer guys...

View Tutorial          By: Jayeeta Pal at 2011-06-28 03:18:26
9. View Comment

It is really very good good notes of Public versus Private.
I am very happy of this.


View Tutorial          By: Nitin Gavande at 2011-07-16 08:47:56
10. View Comment

Very good.
But is this (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.) the only thing that we cannot refer to private member through a statement like this:
Cat Boots;
Boots.itsAge=5; // error! can't access private data!


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

Is it possible to define a class such that the private members are not visible in the class definition?

View Tutorial          By: Smithy at 2012-05-01 22:50:58
12. View Comment

In listing 6.2 I don't see the variable name itsAge in lines 9 or 10 or variable name itsWeight in lines 12 and 13. How does the accessor method correctly identify the member variables defined in lines 20 and 21? Is this a typo or massive ignorance on my part?

View Tutorial          By: JC at 2013-04-16 01:49:52

Your name (required):


Your email(required, will not be shown to the public):


Your sites URL (optional):


Your comments:



More Tutorials by Baski
Compiling multiple source files and specifying classpath using javac
The equals() Method example in Java
Default Values for Data Types in Java
JSP Example to connect to MS SQL database using Tomcat Connection Pool
<convertNumber> and <convertDateTime> in JSF
Enable/Disable Scripting Elements in JSP
Using calloc() Function in C
lseek() sample program in C
Arrays of Structures example program in C
The Basic Syntax Expression Language in JSP
Initialization of Pointer Arrays in C
Functions returning non-integer values in C
Increment and Decrement Operators in C
Getting Started with C
What is JSF (JavaServer Faces)?

More Tutorials in C++
Two-Dimensional Array Manipulation in C++
Calculate average using Two-Dimensional Array in C++
Compute the square root of the sum of the squares of an array in C++
Matrix using nested for loops in C++
Sorting an array of Strings in C++
Calculating total based on the given quantity and price in C++
Compiling and Linking Multiple Source Files in C++
Enumerations in C++
Program to add two numbers in C++
Comments in C++
while loop in C++
for loop in C++
Programming errors a compiler will detect in C++
if in C++
Using the Built-in Arithmetic Types in C++

More Latest News
Most Viewed Articles (in C++ )
Difference between Procedural, Structured, and Object-Oriented Programming
Compute the square root of the sum of the squares of an array in C++
Using cout.width() in C++
C++ Recursion function explained using Fibonacci series
cin.ignore() in C++
Sorting an array of Strings in C++
Getting Started with C++
Dot (.) vs Arrow (->) to access data members in C++
Stray or Dangling Pointers in C++
Compiling and Linking Multiple Source Files in C++
Calculating total based on the given quantity and price in C++
Using Comments in a C++ Program
Constructors and Destructors in C++
for Loops in C++
What Is a Reference in C++?
Most Emailed Articles (in C++)
Converting C++ Multiple-Inheritance Hierarchies to Java
Returning values from a function in C++
C++ and Object-Oriented Programming
Advanced if Statements in C++
do...while Loops in C++
Using cout.fill() in C++
Lvalues and Rvalues in C++
Using cout in C++
C++ Recursion function explained using Fibonacci series
Public versus Private members in C++
Converting Pointer Parameters in C++ to Java
Getting Started with C++
Using Comments in a C++ Program
The if Statement in C++
Passing parameters to a function by value in C++