The access permission of the public protected private member function and member variables after the public protected private is inherited

Source: Internet
Author: User
Tags types of functions

 

First, the access range of the private, public, and protected access labels.

PRIVATE: It can only be accessed by 1. functions in this class, 2. Other functions.

Cannot be accessed by any other user, nor can the object of this class be accessed.

Example:

// Public_protect_private.cpp: defines the entry point of the console application.

//

 

# Include "stdafx. H"

# Include <iostream>

Using namespace STD;

 

Class base

{

Public:

Base (int I)

{

TMP = I;

};

~ Base ()

{

Cout <"program exit \ n" <TMP <Endl;

}

 

PRIVATE:

Int TMP;

Int tmp2;

 

Public:

Void setvalue (Int J)

{

Tmp2 = J;

Return;

}

 

Void printvalue ()

{

Cout <"tmp2 is" <tmp2 <Endl;

}

 

};

 

Int _ tmain (INT argc, _ tchar * argv [])

{

Base P (1 );

P. setvalue (100 );

P. printvalue ();

// P. tmp2 = 455; // error c2248: "Base: tmp2": unable to access private members (declared in the "base" class)

// P. printvalue ();

 

Return 0;

}

 

However, note that private variables can be accessed in the same class. Pay special attention to the thread, such as passing this to the thread. This-> flag can be used in the thread. Flag is a private variable.

Protected: it can be accessed by 1. functions in this class, 2. Sub-class functions, and 3. Its membership functions.

But cannot be accessed by objects of this class.

 

If

PRIVATE:

Int TMP;

Int tmp2;

Change to protected: the object of this class cannot be accessed. Error c2248: "Base: tmp2": the protected member cannot be accessed (declared in the "base" class). Private and protected are the same for the base class, protected has different access permissions on the derived class and private. Let's take a look at it. Can the member function of the derived class access the protected data member of the base class?

 

Protected:

Int TMP;

Int tmp2;

The procedure is as follows:

// Public_protect_private.cpp: defines the entry point of the console application.

//

 

# Include "stdafx. H"

# Include <iostream>

Using namespace STD;

 

Class base

{

Public:

Base (int I)

{

TMP = I;

};

~ Base ()

{

Cout <"program exit \ n" <TMP <Endl;

}

 

Protected:

Int TMP;

Int tmp2;

 

Public:

Void setvalue (Int J)

{

Tmp2 = J;

Return;

}

 

Void printvalue ()

{

Cout <"tmp2 is" <tmp2 <Endl;

}

 

};

 

Class inherit: protected base

{

Public:

Inherit (int I): Base (I)

{

Tmp3 = I;

};

 

~ Inherit ()

{

Cout <"inherit program exit \ n" <tmp3 <Endl;

}

 

PRIVATE:

Int tmp3;

Int tmp4;

 

Public:

Void inheritsetbasevalue (Int J)

{

Tmp2 = J;

Return;

}

 

Void inheritprintbasevalue ()

{

Cout <"tmp2 is" <tmp2 <Endl;

}

 

Void inheritsetvalue (Int J)

{

Tmp4 = J;

Return;

}

 

Void inheritprintvalue ()

{

Cout <"tmp4 is" <tmp4 <Endl;

}

Void inheritprintthoughbase ()

{

Printvalue ();

}

 

};

Int _ tmain (INT argc, _ tchar * argv [])

{

Base P (1 );

P. setvalue (100 );

P. printvalue ();

// P. tmp2 = 455; // error cannot access member variables protected in base class

 

Inherit Q (200 );

Q. inheritsetvalue (300 );

Q. inheritprintvalue ();

 

Q. inheritsetbasevalue (400); // OK. A common member function in the derived class accesses the protected member variable of the base class. Because protected inherits, the protected variable of the base class is also protected in the derived class.

Q. inheritprintbasevalue (); // OK,

 

Q. inheritprintthoughbase (); // OK the common member functions of the derived class can access the public of the base class, and the protected member functions can all be used, because they are protected for the derived class.

// Q. printvalue (); // error cannot access the protection member function of the base class

 

Return 0;

}

The result is as follows:


Public: it can be accessed by 1. functions in this class, 2. Sub-class functions, 3. Its friends functions, or 4. objects in this class.

 

Note: There are three types of functions: normal non-member functions set as friends, member functions of other classes set as friends, and all member functions in the friends class.

 

 

Second, the method property changes after the class is inherited.

Private attributes cannot be inherited.
Using private inheritance, the protected and public attributes of the parent class are changed to private in the subclass;
Using Protected inheritance, the protected and public attributes of the parent class are changed to protected in the subclass;
Use public inheritance. The protected and public attributes of the parent class are not changed;

 

As follows:

Public: protected: Private:
Public inherits from public protected and is unavailable.
Protected inherits protected. Protected is unavailable.
Private inherits Private unavailability

Protected inheritance and private inheritance can reduce access permissions.

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.