Free Hosting

Public vs private access specifiers


Access specifiers
Consider the following struct:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
struct DateStruct
{
    int Month;
    int Day;
    int Year;
};
int main()
{
    DateStruct sDate;
    sDate.nMonth = 10;
    sDate.nDay = 14;
    sDate.nYear = 2020;
    return 0;
}
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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Date
{
    int m_nMonth;
    int m_nDay;
    int m_nYear;
};
int main()
{
    Date cDate;
    cDate.m_nMonth = 10;
    cDate.m_nDay = 14;
    cDate.m_nYear = 2020;
    return 0;
}
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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Date
{
public:
    int m_nMonth; // public
    int m_nDay; // public
    int m_nYear; // public
};
int main()
{
    Date cDate;
    cDate.m_nMonth = 10; // okay because m_nMonth is public
    cDate.m_nDay = 14;  // okay because m_nDay is public
    cDate.m_nYear = 2020;  // okay because m_nYear is public
    return 0;
}
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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
class Access
{
   int m_nA; // private by default
   int GetA() { return m_nA; } // private by default
private:
   int m_nB; // private
   int GetB() { return m_nB; } // private
protected:
   int m_nC; // protected
   int GetC() { return m_nC; } // protected
public:
   int m_nD; // public
   int GetD() { return m_nD; } // public
};
int main()
{
    Access cAccess;
    cAccess.m_nD = 5; // okay because m_nD is public
    std::cout << cAccess.GetD(); // okay because GetD() is public
    cAccess.m_nA = 2; // WRONG because m_nA is private
    std::cout << cAccess.GetB(); // WRONG because GetB() is private
    return 0;
}
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.

0 comments:

Blogger Template by Clairvo