Tuesday, January 18, 2011

Public vs private access specifiers

Access specifiers
Consider the following struct:
01struct DateStruct
02{
03    int nMonth;
04    int nDay;
05    int nYear;
06};
07
08int main()
09{
10    DateStruct sDate;
11    sDate.nMonth = 10;
12    sDate.nDay = 14;
13    sDate.nYear = 2020;
14
15    return 0;
16}
In this program, we declare a DateStruct and then we directly access it’s members in order to initialize them. This works because all members of a struct are public members. Public members are members of a struct or class that can be accessed by any function in the program.
On the other hand, consider the following almost-identical class:
01class Date
02{
03    int m_nMonth;
04    int m_nDay;
05    int m_nYear;
06};
07
08int main()
09{
10    Date cDate;
11    cDate.m_nMonth = 10;
12    cDate.m_nDay = 14;
13    cDate.m_nYear = 2020;
14
15    return 0;
16}
If you were to compile this program, you would receive an error. This is because by default, all members of a class are private. Private members are members of a class that can only be accessed by other functions within the class. Because main() is not a member of the Date class, it does not have access to Date’s private members.
Although class members are private by default, we can make them public by using the public keyword:
01class Date
02{
03public:
04    int m_nMonth; // public
05    int m_nDay; // public
06    int m_nYear; // public
07};
08
09int main()
10{
11    Date cDate;
12    cDate.m_nMonth = 10; // okay because m_nMonth is public
13    cDate.m_nDay = 14;  // okay because m_nDay is public
14    cDate.m_nYear = 2020;  // okay because m_nYear is public
15
16    return 0;
17}
Because Date’s members are now public, they can be accessed by main().
One of the primary differences between classes and structs is that classes can explicitly use access specifiers to restrict who can access members of a class. C++ provides 3 different access specifier keywords: public, private, and protected. We will discuss the protected access specifier when we cover inheritance.
Here is an example of a class that uses all three access specifiers:
01class Access
02{
03   int m_nA; // private by default
04   int GetA() { return m_nA; } // private by default
05
06private:
07   int m_nB; // private
08   int GetB() { return m_nB; } // private
09
10protected:
11   int m_nC; // protected
12   int GetC() { return m_nC; } // protected
13
14public:
15   int m_nD; // public
16   int GetD() { return m_nD; } // public
17
18};
19
20int main()
21{
22    Access cAccess;
23    cAccess.m_nD = 5; // okay because m_nD is public
24    std::cout << cAccess.GetD(); // okay because GetD() is public
25
26    cAccess.m_nA = 2; // WRONG because m_nA is private
27    std::cout << cAccess.GetB(); // WRONG because GetB() is private
28
29    return 0;
30}
Each of the members “acquires” the access level of the previous access specifier. It is common convention to list private members first.
Why would you want to restrict access to class members? Oftentimes you want to declare members that are for “internal class use only”. For example, when writing a string class, it is common to declare a private member named m_nLength that holds the length of the string. If m_nLength were public, anybody could change the length of the string without changing the actual string! This could cause all sorts of bizarre problems. Consequently, the m_nLength is made private so that only functions within the String class can alter m_nLength.
The group of public members of a class are often referred to as a “public interface”. Because only public members can be accessed outside of the class, the public interface defines how programs using the class will interface with the class.

No comments: