Public, protected, and private usages in C + +

Source: Internet
Author: User
Tags access properties assert

Transferred from: http://www.jb51.net/article/54224.htm

Beginner C + + friends often see public,protected,private in classes and some of the scope of access they represent in inheritance, which is easy to confuse. Today this article will be very analysis of C + + public, protected and private usage. I believe it will be a great help for you to master C + + program design.

Here we first have to understand the following points.

1. One of the characteristics of a class is encapsulation, and public and private functions are to achieve this. So:

User code (outside the class) can access public members without access to private members, and private members can only be accessed by class members (in-Class) and friend.

2. Another feature of the class is inheritance, which is what protected does to achieve this purpose. So:

Protected members can be accessed by derived class objects and cannot be accessed by user code (outside the Class).

Now take a look at the following example:

#include <iostream>#include<assert.h>using namespacestd;classa{ Public:  intA; A () {A1=1; A2=2; A3=3; A=4; }  voidFun () {cout<< a << Endl;//correctcout << A1 << Endl;//correctcout << A2 << Endl;//right, in-class accesscout << A3 << Endl;//right, in-class access  } Public:  intA1;protected:  intA2;Private:  inta3;};intMain () {A itema; ITEMA.A=Ten;//correctITEMA.A1 = -;//correctITEMA.A2 = -;//error, protected members cannot be accessed outside the classITEMA.A3 = +;//error, private member cannot be accessed outside the classSystem"Pause"); return 0;}

Features in the inheritance:

Remember: The above rules always apply regardless of whether or not they inherit!

There are public, protected, private three inheritance methods that change the access properties of the base class members accordingly.

1.public Inheritance: The base class public member, the protected member, and the private member's Access property are changed in the derived class, respectively: public, protected, private

2.protected Inheritance: The base class public member, the protected member, and the private member's Access property are changed in the derived class: protected, Protected, private

3.private Inheritance: The base class public member, the protected member, and the private member's Access property are changed in the derived class, respectively: Private, private, private

But no matter which way of inheritance, the above two points have not changed:

1.private members can only be accessed by members of this class (In-Class) and friends, and cannot be accessed by derived classes;

2.protected members can be accessed by derived classes.

Then take a look at the following code:

1.public inheritance

The code is as follows:

#include <iostream>#include<assert.h>using namespacestd;classa{ Public:  intA; A () {A1=1; A2=2; A3=3; A=4; }  voidFun () {cout<< a << Endl;//correctcout << A1 << Endl;//correctcout << A2 << Endl;//correctcout << A3 << Endl;//correct  } Public:  intA1;protected:  intA2;Private:  inta3;};classD | Publica{ Public:  intA; B (inti)    {A (); A=i; }  voidFun () {cout<< a << Endl;//correct, public membercout << A1 << Endl;//correctly, the public member of the base class is still a public member in the derived class. cout << A2 << Endl;//correctly, the protected member of the base class, which is still protected in the derived class, can be accessed by the derived class. cout << A3 << Endl;//error, the private member of the base class cannot be accessed by a derived class.   }};intMain () {b b (Ten); cout<< B.A <<Endl; cout<< b.a1 << Endl;//correctcout << b.a2 << Endl;//error, protected members cannot be accessed outside the classcout << b.a3 << Endl;//error, private member cannot be accessed outside the classSystem"Pause"); return 0;}

2.protected Inheritance:

The code is as follows:

#include <iostream>#include<assert.h>using namespacestd;classa{ Public:  intA; A () {A1=1; A2=2; A3=3; A=4; }  voidFun () {cout<< a << Endl;//correctcout << A1 << Endl;//correctcout << A2 << Endl;//correctcout << A3 << Endl;//correct  } Public:  intA1;protected:  intA2;Private:  inta3;};classD |protecteda{ Public:  intA; B (inti)    {A (); A=i; }  voidFun () {cout<< a << Endl;//correct, public member. cout << A1 << Endl;//correctly, the public member of the base class becomes protected in the derived class and can be accessed by the derived class. cout << A2 << Endl;//correctly, the protected member of the base class, in a derived class or protected, can be accessed by a derived class. cout << A3 << Endl;//error, the private member of the base class cannot be accessed by a derived class.   }};intMain () {b b (Ten); cout<< B.A << Endl;//correct. Public Memberscout << b.a1 << Endl;//error, protected members cannot be accessed outside the class. cout << b.a2 << Endl;//error, protected members cannot be accessed outside the class. cout << b.a3 << Endl;//error, private members cannot be accessed outside the class. System"Pause"); return 0;}

3.private Inheritance:

The code is as follows:

#include <iostream>#include<assert.h>using namespacestd;classa{ Public:  intA; A () {A1=1; A2=2; A3=3; A=4; }  voidFun () {cout<< a << Endl;//correctcout << A1 << Endl;//correctcout << A2 << Endl;//correctcout << A3 << Endl;//correct  } Public:  intA1;protected:  intA2;Private:  inta3;};classD |Privatea{ Public:  intA; B (inti)    {A (); A=i; }  voidFun () {cout<< a << Endl;//correct, public member. cout << A1 << Endl;//correctly, the base class public member becomes private in the derived class and can be accessed by the derived class. cout << A2 << Endl;//correctly, the protected member of the base class becomes private in the derived class and can be accessed by the derived class. cout << A3 << Endl;//error, the private member of the base class cannot be accessed by a derived class.   }};intMain () {b b (Ten); cout<< B.A << Endl;//correct. Public Memberscout << b.a1 << Endl;//error, private members cannot be accessed outside the class. cout << b.a2 << Endl;//error, private members cannot be accessed outside the class. cout << b.a3 << Endl;//error, private members cannot be accessed outside the class. System"Pause"); return 0;}

The above code is provided with more detailed comments that the reader should be able to understand. A careful look at the code in derived Class B defines a member of the same name as the base class A, at which point a of the base class still exists and can be verified.

int Main () {  sizeof(A) << Endl;   sizeof (B) << Endl;   System ("pause");   return 0 ;}

Output:

16

20

So the derived class contains all the members of the base class and the new members, the members of the same name are hidden, and only the members in the derived class are called when called.

If you want to invoke a member of the same name as the base class, you can use the following methods:

int Main () {   b b (ten);   << B.A << Endl;   b.a::a << Endl;   System ("pause");   return 0 ;}

Output:

10

4

Remember that this is accessed outside the class, and a is public in the base class, so the inheritance should be public, so that a is still public in the derived class and accessible outside the class.

Public, protected, and private usages in C + +

Related Article

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.