Class default access modifier
Access modifier is the rule on how an attribute of Class can be accessed from outside the scope of the class. Basically it defines how the outside world can access the member attributes. We are discussing the default access rules of a C++ class. The default access modifier for class member variable and member functions is "private" in C++ class. This means if we do not provide any access modifier then by default the members are considered as private.
Private members are the class members either data members of member functions those are hidden from the outside world. The private members implement the concept of data hiding in the object oriented programming.
Class default access (private)
Compilation
The above program can not compile since all default access modifier are private. Let's see what all compilation error we will be getting.
$ g++ class.cpp class.cpp:19:13: error: calling a private constructor of class 'student' s1 = new student(); ^ class.cpp:4:4: note: implicitly declared private here student(); ^ class.cpp:20:7: error: 'm_Member1' is a private member of 'student' s1->m_Member1 = 0; ^ class.cpp:3:7: note: implicitly declared private here int m_Member1; ^ class.cpp:21:10: error: calling a private destructor of class 'student' delete s1; ^ class.cpp:5:4: note: implicitly declared private here ~student(); ^ 3 errors generated. $
Class default access override with "public"
Having "public" access can pass the compilation.
Sctucture default access modifier
Now we know the default access modifier of C++ class. Is this same rule applicable for C++ structures? The answer is No, structure follows the concept of C with no object oriented concept by default. Thus members can be accessed outside by default until access modifiers are made private.
Know more about this. See also: What is the default access modifier for structure members and member functions in c++?
About our authors: Team EQA
You have viewed 1 page out of 62. Your C++ learning is 0.00% complete. Login to check your learning progress.