C++ in public、protect、private Access control for

Access right

A class of public Member variables 、 Member functions , You can use the member function of the class 、 Class

A class of protected Member variables 、 Member functions , Cannot be accessed through an instance variable of a class . But you can use the friend function of the class 、 Visit the friend class .

A class of private Member variables 、 Member functions , Cannot be accessed through an instance variable of a class . But you can use the friend function of the class 、 Visit the friend class .

Access right demo

#include <iostream>
class ReferencePerission
{
friend class FriendClass;
friend void FriendFunc();
public:
int public_num;
void public_func(){std::cout <<"public_func "<<public_num<<std::endl;}
protected:
int protected_num;
void protected_func(){std::cout <<"protected_func "<<protected_num<<std::endl;}
private:
int private_num;
void private_func(){std::cout <<"private_func "<<private_num<<std::endl;}
}; class FriendClass
{
public:
FriendClass() {
std::cout<<"FriendClass"<<std::endl;
ReferencePerission ref;
ref.public_num = 1;
ref.protected_num = 2;
ref.private_num = 3;
ref.public_func();
ref.protected_func();
ref.private_func();
} ~FriendClass()
{ }
}; void FriendFunc()
{
std::cout<<"FriendFunc"<<std::endl;
ReferencePerission ref;
ref.public_num = 1;
ref.protected_num = 2;
ref.private_num = 3;
ref.public_func();
ref.protected_func();
ref.private_func();
} int main()
{
ReferencePerission ref; ref.public_num = 1;
//ref.protected_num = 2; /* Compilation error declared protected here */
//ref.private_num = 3; /* Compilation error declared private here */
ref.public_func();
//ref.protected_func(); /* Compilation error declared protected here */
//ref.private_func(); /* Compilation error declared private here */ FriendFunc(); /* Friend functions can access public,protected,private Member variables and functions */ FriendClass friendObj; /* Friend classes can visit public,protected,private Member variables and functions */ return 0;
}

Inheritance rights

public Inherit

Derived classes are generated by public Inherit , The permissions of the base class remain unchanged .

Member functions of derived classes , You can access the public member 、protected member , But you can't access the base class's private member .

Instance variables of derived classes , You can access the public member , But there's no access to protected、private member , As if members of the base class were added to derived classes .

Can be public Inheritance is regarded as a derived class, and the base class's public,protected Members are included in derived classes , But not including private member .

protected Inherit

  Derived classes are generated by protected Inherit , The base class public The permission of a member in a derived class becomes protected .protected and private unchanged .

Member functions of derived classes , You can access the public member 、protected member , But you can't access the base class's private member .

Instance variables of derived classes , No members of the base class can be accessed , Because of the base class public The member in the derived class becomes protected.

Can be protected Inheritance is regarded as a derived class, and the base class's public,protected Members are included in derived classes , All as derived classes protected member , But not including private member .

private Members are privacy within the base class , Besides friends , No one is allowed to pry . Friends of derived classes , No access

private Inherit

Derived classes are generated by private Inherit , The permissions of all members of the base class in the derived class become private.

Member functions of derived classes , You can access the public member 、protected member , But you can't access the base class's private member .

Instance variables of derived classes , No members of the base class can be accessed , Because all members of the base class become private.

Can be private Inheritance is regarded as a derived class, and the base class's public,protected Members are included in derived classes , All as derived classes private member , But not including private member .

private Members are privacy within the base class , Besides friends , No one is allowed to pry . Friends of derived classes , No access

summary : Inheritance modifiers , It's like a sieve , Sift the members of the base class into the derived class .public、protected、private, It's the eye of the sieve .

adopt public Inherit , All base class members ( except private),public、protected It's all in derived classes ,public The mesh is bigger , It doesn't change access .

adopt protected Inherit , All base class members ( except private),public、protected It's all in derived classes ,protected The mesh size is moderate , All the members who came here became protected.

adopt private Inherit , All base class members ( except private),public、protected It's all in derived classes ,private The sieve is the smallest , All the members who came here became private.

#include <iostream>
using namespace std;
class InheritPerission
{
public:
string public_str;
void public_func(){std::cout <<"public_func "<<public_str<<std::endl;}
protected:
string protected_str;
void protected_func(){std::cout <<"protected_func "<<protected_str<<std::endl;}
private:
string private_str;
void private_func(){std::cout <<"private_func "<<private_str<<std::endl;}
}; /* Derived classes are generated by public Inherit , The permissions of the base class remain unchanged */
class InheritPublic : public InheritPerission
{
public:
InheritPublic(){
/* Member functions of derived classes , You can access the public member 、protected member , But you can't access the base class's private member */
public_str = "public_str";
protected_str = "protected_str";
//private_str = "private_str";
public_func();
protected_func();
//private_func();
}
~InheritPublic(){};
}; /* Derived classes are generated by protected Inherit , The base class public The permission of a member in a derived class becomes protected .protected and private unchanged */
class InheritProtected : protected InheritPerission
{
public:
InheritProtected(){
/* Member functions of derived classes , You can access the public member 、protected member , But you can't access the base class's private member */
public_str = "public_str";
protected_str = "protected_str";
//private_str = "private_str";
public_func();
protected_func();
//private_func();
}
~InheritProtected(){}
}; /* Derived classes are generated by private Inherit , The permissions of all members of the base class in the derived class become private*/
class InheritPrivate : private InheritPerission
{
public:
InheritPrivate(){
/* Member functions of derived classes , You can access the public member 、protected member , But you can't access the base class's private member */
public_str = "public_str";
protected_str = "protected_str";
//private_str = "private_str";
public_func();
protected_func();
//private_func();
}
~InheritPrivate(){}
}; int main()
{
InheritPublic inheritPublic;
InheritProtected inheritProtected;
InheritPrivate inheritPrivate; /* Instance variables of derived classes , You can access the public member , But there's no access to protected、private member */
inheritPublic.public_func();
//inheritPublic.protected_func();
//inheritPublic.private_func(); /* Instance variables of derived classes , No members of the base class can be accessed , Because of the base class public The member in the derived class becomes protected*/
//inheritProtected.public_func();
//inheritProtected.protected_func();
//inheritProtected.private_func(); /* Instance variables of derived classes , No members of the base class can be accessed , Because all members of the base class become private*/
//inheritPrivate.public_func();
//inheritPrivate.protected_func();
//inheritPrivate.private_func(); return 0;
}

c/c++ To learn :C++ in public、protect、private More related articles on access control for

  1. C++ in public/protect/private Three kinds of access control

    One . Member access control 1.public (1)public Member variables can be accessed by member functions  [ Accessibility ] (2)public Members can be accessed by entity objects  [ Accessibility ] (3)public Members can be subclass members  [ ...

  2. Java in public,private,final,static And so on

    As a beginner Java Little white of , about public,private,final,static I'm always confused about the concept of , What does it all stand for , Here is a simple comb , Share with you , If there is any mistake, please correct it , thank you ~ Access modifier pu ...

  3. [ turn ] Java in public,private,final,static And so on

    As a beginner Java Little white of , about public,private,final,static I'm always confused about the concept of , What does it all stand for , Here is a simple comb , Share with you , If there is any mistake, please correct it , thank you ~ Access modifier pu ...

  4. java in public And private also protect The difference between

    java in public And private also protect The difference between Always forget .

  5. public protect private. draft .

    public protect private. draft . #include <iostream> #include <thread> #include <memory> ...

  6. C++ in public,protected,private Derived class inheritance and access rights

    C++ in public,protected,private Derived class inheritance and access rights When a subclass inherits from a parent class , All members of the parent class become members of the child class , In this case, the access status to the parent class members is determined by the inheritance qualifier used in inheritance . 1. ...

  7. java in 4 The difference of access rights of two kinds of modifiers and the detailed explanation of the whole process

    java in 4 The difference of access rights of two kinds of modifiers and the detailed explanation of the whole process http://jingyan.baidu.com/article/fedf0737700b3335ac8977ca.html java in 4 The modifiers in are ...

  8. ( turn ) elementary analysis Java Access control in

    Original address : http://www.cnblogs.com/dolphin0520/p/3734915.html Today, let's learn about Java Access control in language . Before we talk about access control , Let's talk about it first ...

  9. elementary analysis Java Access control in

    elementary analysis Java Access control in Today, let's learn about Java Access control in language . Before we talk about access control , Let's first discuss why access control is needed . Consider two scenarios : scene 1: The engineer A Write a class ClassA, but ...

Random recommendation

  1. cocoa frame for iOS

    1.Cocoa What is it? ? Cocoa yes OS X and iOS The running environment of the program of the operating system . What makes a program Cocoa The procedure ? Not a programming language , Because in Cocoa You can use all kinds of languages in development : It's not a development tool , You can ...

  2. About string.format() turn

    string.format() Function to generate a string with a specific format , This function has two arguments , The first parameter is the formatted string : Consists of indicators and characters that control the format . The second parameter is the data of each code in the corresponding format . The format string may contain a ...

  3. Razor.js, be based on JavaScript Of Razor Realization

    Hello, everyone , I made it before JS Template wheel Otmpl, It's not bad, though , But and MVC Razor Compared with the simplicity and elegance of , It's just horrible . After a few days of research , Finally, after referring to the code of some foreign cattle people , Showing the first edition , Welcome to discuss . ...

  4. ROC Curve and AUC The evaluation index

    A lot of times , We hope to evaluate the performance of a binary classifier ,AUC It is such a standard to measure the quality of classification model . In reality, samples are unevenly distributed in different categories (class distribution imbalance pro ...

  5. Leetcode: Graph Valid Tree &amp;&amp; Summary: Detect cycle in undirected graph

    Given n nodes labeled from 0 to n - 1 and a list of undirected edges (each edge is a pair of nodes), ...

  6. Python Dictionary method copy() and deepcopy() The difference between

    from copy import deepcopy # import deepcopy modular d = {} d['name'] = ['black', 'guts'] # d = {'name': [' ...

  7. Media Player frame

    Import MediaPlayer.framework frame . // Declare a media player var moviePlayer:MPMoviePlayerController? @IBAction func playM ...

  8. c Factoring an integer

    1 #include <stdio.h> int main(void) { int n,i; scanf("%d",&n); printf("%d=& ...

  9. pygtk Notes (1)

    GTK+ Use C Language development , But its designers use object-oriented technology . Also provided C++(gtkmm).Perl.Ruby.Java and Python(PyGTK) binding , Other bindings are Ada.D.Haskell.PHP And all ...

  10. VMware The driver &quot;vmci.sys&quot; Incorrect version of How to solve

    terms of settlement : 1. After creating the virtual machine , Don't turn on the power , Then go to the virtual machine folder : 2. Find the suffix vmx The file of , Notepad open : 3. find vmci0.present='TRUE', hold true Change it to false: 4. Protect ...