Home > Back-end >  (C) private, public, protected the specific role is what (what is vivid and short instance)?
(C) private, public, protected the specific role is what (what is vivid and short instance)?

Time:10-23


Class of struct in c + +, upgrades to more private public protected three key words,

Although know the use of the three key words, but I still can't understand,

Turn to the great god

Cite an instance, help me understand once,

Thank you very much!!

CodePudding user response:

Private can oneself this type visit
Proteced and inherit their own class read access to
It can access the public who,

CodePudding user response:

Example is very simple, remove the comment place will be the compiler error
 class AA 
{
Private:
Int p1=1;
Protected:
Int p2=2;
Public:
Int p3=3;
Virtual void test () {
Printf (" % d \ n ", (p1);//private only you can access
Printf (" % d \ n ", p2);//access to
Printf (" % d \ n ", p3);//access to
};
};

Class AB: AA
{
Virtual void test () {
//printf (" % d \ n ", (p1);//here will compile error, not the parent class private
Printf (" % d \ n ", p2);//here will not compile errors that can access the parent class of protected
Printf (" % d \ n ", p3);//here will not compile errors that can access the public of the parent
}
};

Int main (int arg c, const char * argv []) {
AA a;
//printf (" % d \ n ", Amy polumbo 1);//here will compile error, not the parent class private
//printf (" % d \ n ", Amy polumbo 2);//here will compile error, not the parent class protected
Printf (" % d \ n ", Amy polumbo 3);//here will not compile errors that can access the public of the parent
return 0;
}

CodePudding user response:

@ qybao
First of all, thank you for answering my questions

The second
Actually I understand it, just don't know what are the specific role in practical application
Examples of general examples are too small, the data quantity is not enough, that is not the problem

Can you give a bigger and vivid examples explain

CodePudding user response:

@ qybao
First of all, thank you for answering my questions

The second
Actually I understand it, just don't know what are the specific role in practical application
Examples of general examples are too small, the data quantity is not enough, that is not the problem

Can you give a bigger and vivid examples explain