Analysis of C++:private, protected and public

Source: Internet
Author: User

One: access rights for private, protected, and public issues:

Private:1, a member function in a class can be called, 2, a friend function of a class can be called, 3, but the class object cannot be called directly. 4. For derived classes, neither the member function nor the object of the derived class can be called.

A member function in a protected:1 class cannot be called, 2, a friend function of a class can be called, and 3, a class object cannot be called. 4. For derived classes, member functions can be called, but objects of derived classes cannot be called.

Public:1 can be called by a member function in the class, 2, the friend function of the class can be called, 3, the class object can also be called directly. 4, for derived classes, member functions and derived classes of objects can be called.

Note: The friend function consists of 1, a normal non-member function set as a friend. 2. member functions of other classes that are set as friends. 3, set as all member functions of the friend class.

Two: Questions about the succession of private, protected and public:

1, whether private, protected, or public, the subclass inherits the base class, which inevitably inherits all the members of the base class (except the constructor and destructor of the base class), and inherits the size of the base class space.

#include <iostream>using namespacestd;classa{ Public: A () {cout<<"this is class A."<<Endl; }Private:    intA; inttemp;};classD |Privatea{ Public: B () {cout<<"this is class B."<<Endl; }Private:    intb;};voidMain () {cout<<sizeof(A) <<Endl; cout<<sizeof(B) <<Endl;}
example of change in capacity size after inheritance:

2. After the class inherits, the property changes.

The private property can be inherited, but it cannot be directly invoked under any circumstances (except the public member function of the base class and the friend function of the base class);
With private inheritance, the protected and public properties of the parent class become private in subclasses;
With protected inheritance, the protected and public properties of the parent class become protected in the subclass;
With public inheritance, the protected and public properties in the parent class do not change;

As shown below: Public:protected:private:
Public inherited public protected not available
Protected inherited protected protected not available
Private private is not available

Analysis of C++:private, protected and public

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.