Access control for private and protected and public class members in C++ programming

  • 2020-05-07 20:13:49
  • OfStack

private
grammar


private:
  [member-list]
private base-class

note
When placed before a list of class members, the private keyword specifies that these members are accessible only from member functions and friends of the class. This applies to all members that declare to the end of the next access indicator or class.
When placed before the name of the base class, the private keyword specifies that the public and protected members of the base class are private members of the derived class.
The default access to a member of a class is private. The default access to members in a structure or union is public.
The default access to a base class is private to the class and public to the structure. Unions cannot have base classes.
example


// keyword_private.cpp
class BaseClass {
public:
  // privMem accessible from member function
  int pubFunc() { return privMem; }
private:
  void privMem;
};

class DerivedClass : public BaseClass {
public:
  void usePrivate( int i )
   { privMem = i; }  // C2248: privMem not accessible
             // from derived class
};

class DerivedClass2 : private BaseClass {
public:
  // pubFunc() accessible from derived class
  int usePublic() { return pubFunc(); }
};

int main() {
  BaseClass aBase;
  DerivedClass aDerived;
  DerivedClass2 aDerived2;
  aBase.privMem = 1;   // C2248: privMem not accessible
  aDerived.privMem = 1; // C2248: privMem not accessible
             //  in derived class
  aDerived2.pubFunc();  // C2247: pubFunc() is private in
             //  derived class
}


protected
grammar


protected:
  [member-list]
protected base-class

note
The protected keyword specifies access to members in member-list up to the end of the next access specifier (public or private) or class definition. Class members declared as protected can only be used by:
The member function of the class that initially declares these members.
Friends of the classes that initially declare these members.
A class derived using public or protected access (derived from the class in which these members were originally declared).
Private derived direct class that also has private access to protected members.
When starting with the name of the base class, the protected keyword specifies that the public and guard members of the base class are the guard members of its derived class.
Protected members are not as exclusive as members of private, which are only accessible to members of the classes from which they are declared, but protected members are not as public as members of public, which are accessible in any function.
A guard member also declared as static is accessible to any friend or member function of a derived class. A guard member also declared as static is accessible to a friend or member function in a derived class, but only through a pointer to a derived class, a reference to a derived class, or an object of a derived class.
example


// keyword_protected.cpp
// compile with: /EHsc
#include <iostream>

using namespace std;
class X {
public:
  void setProtMemb( int i ) { m_protMemb = i; }
  void Display() { cout << m_protMemb << endl; }
protected:
  int m_protMemb;
  void Protfunc() { cout << "\nAccess allowed\n"; }
} x;

class Y : public X {
public:
  void useProtfunc() { Protfunc(); }
} y;

int main() {
  // x.m_protMemb;     error, m_protMemb is protected
  x.setProtMemb( 0 );  // OK, uses public access function
  x.Display();
  y.setProtMemb( 5 );  // OK, uses public access function
  y.Display();
  // x.Protfunc();     error, Protfunc() is protected
  y.useProtfunc();   // OK, uses public access function
            // in derived class
}


public
grammar


public:
  [member-list]
public base-class

note
When in front of a list of class members, the public keyword specifies that these members can be accessed from any function. This applies to all members that declare to the end of the next access indicator or class.
When placed in front of the base class name, the public keyword specifies that the public and protected members of the base class are the public and protected members of the derived class, respectively.
The default access to a member of a class is private. The default access to members in a structure or union is public.
The default access to a base class is private to the class and public to the structure. Unions cannot have base classes.
example


// keyword_public.cpp
class BaseClass {
public:
  int pubFunc() { return 0; }
};

class DerivedClass : public BaseClass {};

int main() {
  BaseClass aBase;
  DerivedClass aDerived;
  aBase.pubFunc();    // pubFunc() is accessible 
             //  from any function
  aDerived.pubFunc();  // pubFunc() is still public in 
             //  derived class
}


Related articles: