Private and protection in C + + programming and common class member access control _c language

Source: Internet
Author: User
Tags class definition

Private
Grammar

Private:
  [member-list]
private Base-class

Note
When you are in the class member list, the Private keyword specifies that these members are accessible only from member functions and friends of that class. This applies to all members declared to the next access indicator or to the end of the class.
When located before the name of the base class, the Private keyword specifies that the base class's public members and protected members are private members of the derived class.
The default access for members in a class is private. The default access for members in structs or unions is public.
The default access for the base class is private for the class and is public for the struct. A union cannot have a base class.
Example

Keyword_private.cpp
class BaseClass {public
:
  //Privmem accessible to 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 the member in the Member-list until the end of the next access specifier (public or private) or the class definition. Class members declared as protected can be used only through the following items:
The member functions of the class that originally declared these members.
A friend of the class that originally declared these members.
Classes that derive from public or protected access (derived from the class that originally declared these members).
A private-derived direct class that has private access to the protected member.
When you start with the name of the base class, the protected keyword specifies that the public members of the base class and the protected members are protected members of their derived classes.
Protected members are not as private as private members, private members are accessible only to members of the class from which they are declared, but protected members are not as exposed as public members and are accessible in any function.
Protected members that are also declared static are accessible to any friend or member function of a derived class. Protected members that are also declared static are accessible to friend or member functions in a derived class, but only through pointers to derived classes, references to derived classes, or objects of derived classes.
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 you are in front of the class member list, the Public keyword specifies that these members are accessible from any function. This applies to all members declared to the next access indicator or to the end of the class.
When you are in front of the base class name, the Public keyword specifies that the base class's common and protected members are both public and protected members of the derived class.
The default access for members in a class is private. The default access for members in structs or unions is public.
The default access for the base class is private for the class and is public for the struct. A union cannot have a base class.
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 
             //to any  function
  aderived.pubfunc ();  Pubfunc () is still public in 
             //  derived class
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.