C + + Easy to mix knowledge point 5: Example explain public Protected private scope, the difference and intention of inheritance __c++

Source: Internet
Author: User
University career, involving the scope of the class, inheritance are used by the public common inheritance, at that time did not think so much, feel the common inheritance more convenient, how simple, anyway, there is no too many restrictions, whether it is a member of the class or a member function of the base class can be accessed. Didn't delve into it. In fact, this is really involved in the C + + object-oriented design package features. Exposes only those members and member functions that are required, without exposing all members too much.
Reprint Please indicate the source: Http://blog.csdn.net/elfprincexu

First: Private, public, protected the scope of the visit. Private: can only be accessed by 1, the function in the class, 2, and its friend function. cannot be accessed by any other object and cannot be accessed by any of the class objects.

Private members are accessible in the class that defines them
protected: Can be accessed by 1, functions in the class, 2, subclasses, and 3 friend functions. The class object cannot access the

Protected memebers are accessible in the class, that defines them, and in classes, that's inherit from, that class (at least pro tected inheritance). instances of this class can not access them
public: 1, functions in the class, 2, subclasses, 3, friend functions, 4 object access to the class.

The public memebers are accessible is in the class this defines them, in classes this inherit from this class (at least protected inheritance), and also accessible by instances to the class that defines them.

Note: The friend function includes a normal, non member function that is set as a friend function in 3, a member function of another class that is set as a friend, and is set to all member functions in a friend class


Second: Changes in method Properties after the inheritance of the class. Private attributes cannot be inherited and are not visible to all subclasses. with private inheritance, the protected and public properties of the parent class become private in subclasses, and the protected and public properties of the parent class become protected in subclasses, using protected inheritance; With public inheritance, the protected and public properties of the parent class do not change;

not much, let's take a look at the following example:

#include <iostream> using namespace std; Class A//Parent class {Private:int Privateda
Tea;
Protected:int Protecteddatea;
Public:int Publicdatea;
}; Class B:p ublic A//base class A derived class B (shared inheritance) {Publi
        C:void funct () {int B;   B=privatedatea; Error: Private members in the base class are invisible b=protecteddatea in derived classes;    OK: the protection member of the base class is b=publicdatea for the protected member in the derived class;
OK: The public members of the base class are public members in the derived class}};
Class C:p rivate A//base class A derived class C (private inheritance) {
        Public:void funct () {int C;    C=privatedatea;  Error: Private members in the base class are invisible c=protecteddatea in derived classes;     OK: the protection members of the base class are c=publicdatea for private members in derived classes;
OK: The public members of the base class are private members in the derived class}}; Class D:p rotected A//base class A derived class D (Protection inheritance) {
Public:void funct () {int D;   D=privatedatea; Error: Private members in the base class are invisible d=protecteddatea in derived classes;    OK: the protection member of the base class is d=publicdatea for the protected member in the derived class;
OK: The public members of the base class are protected members} in the derived class;
 
    int main () {int A;
    B OBJB;   A=objb.privatedatea; Error: Private members in the base class are not visible in the derived class and are invisible to the object A=objb.protecteddatea;    Error: The protection member of the base class is a protected member in a derived class and is not visible to the object A=objb.publicdatea;
    OK: The public members of the base class are public members in the derived class, and C OBJC is visible to the object;   A=objc.privatedatea; Error: Private members in the base class are not visible in the derived class and are invisible to the object A=objc.protecteddatea;    Error: The protection member of the base class is a private member in a derived class and is not visible to the object A=objc.publicdatea;
    Error: The public members of the base class are private members in the derived class, and D OBJD is not visible to the object;   A=objd.privatedatea; Error: Private members in the base class are not visible in the derived class and are invisible to the object A=objd.protecteddatea;    Error: The protection member of the base class is a protected member in a derived class and is not visible to the object A=objd.publicdatea;
Error: The public members of the base class are protected members in the derived class, and return 0 is not visible to the object; }


Common inheritance (public), private inheritance (private), and protection Inheritance (protected) are three inherited methods that are commonly used.
1. Communal Inheritance (public)
Public inheritance is characterized by the presence of public members of the base class and the protection of members as members of derived classes, while the private members of the base class remain private and cannot be accessed by subclasses of this derived class.
2. Personal Inheritance (private)
Private inheritance is characterized by that both the public and the protected members of the base class are private members of the derived class and cannot be accessed by subclasses of this derived class.
3. Protection Inheritance (protected)
The characteristic of protecting inheritance is that all public and protected members of the base class are protected members of the derived class and can only be accessed by its derived class member functions or friends, and the private members of the base class are still private.


The following lists the base class attributes and derived class attributes for three different inheritance methods.

Public protected private

Total inherited public protected not visible
Private inheritance private private not visible
Protect inheritance protected protected not visible


In the figure above:

base class members are all derived classes: Shared and protected members are visible, and private members are invisible. the base class member is for the object of the derived class: To see what type of member the base class's members have become in the derived class. For example, when private inheritance, both the common and private members of the base class become private members of the derived class, so that the common members and private members of the base class are invisible to the objects in the derived class.





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.