Public, private, and protected inheritance-C ++ primer notes

Source: Internet
Author: User

Protectedinheritance. cpp

Indirect inheritance is inconsistent with the book description. The book says that the second inheritance cannot access base: I ~ because the first inheritance changes to private ~ YesPrivateThe reality is that the prompt base: I isProtected, It seems that the private_derived class is skipped and the base class is directly searched.

Inheritance's protection for the base class is still a general rule and can only be stricter and cannot be loose.

Also, the label does not restrictBase class member(Is it unclear whether it is a base class member or a member inherited from the base class ?) ,

Original statement:All classes that inherit the base haveSame access(What is a member? How can we properly understand that a derived class includes a base class? Therefore, access to the base class contained in the derived class refers to access to the base class ?)

Label is a limitUserAndDerived class(Also as a user ?) PairDerived class(Inherited from the base class)MemberAccess

// Public, private, and protected inheritance # include <iostream> class base {public: void basemem (); protected: int I ;}; // struct directly inherits class? Yes? There are also different default access levels, one public and one private. Do not forget struct public_derived: public base {int use_base () {return I;} // as the name implies, use base to see}; struct protected_derived: protected base {int use_base () {return I ;}}; struct private_derived: Private base {int use_base () {return I ;} // No problem, though private ~~~ Private is not set for the user and the next derived class}; // the next layer derives struct derived_from_private: Public private_derived {int use_base () {return I ;} // The comment in the book, base: I is private in private_derived, // The actual error message is: 'int base: I 'is protected, it may be that the user identity has been directly searched for in the base, which is protected. If it is indirect, this prompt is difficult}; struct derived_from_public: Public public_derived {int use_base () {return I ;} // accessible because base: I is still protected} in the public_derived class; int main () {Base B; public_derived D1; protected_derived D3; private_derived D2; B. basemem (); // B object, certainly can access d1.basemem (); // for D1 object, basemem () or public // d2.basemem (); // basemem () it is private and cannot be accessed. // d3.basemem (); // protected certainly does not work}

Interface inheritance and implementation inheritance:

The class derived from public has the same interface as the base class. InWell-designed hierarchy, The class derived from public can be used in any place where the base class object is required.

The non-public-derived class does not inherit the interface of the base class, which is called implementation inheritance. (the base class is used for its implementation, but the base class is not used as an interface)

Using. cpp

After testing, the usage of using differs greatly from that in the book ~!!!!!!!!!!!!!!!!!! (Mark)

Base protected, inherited by private, the result can be changed to public ~~~!!!!

In addition, we also tested the compatibility and conversion of base classes and derived class pointers, and the results are consistent with my "take for granted" judgment ..

How to define the constructor of a derived class? declare the members of the base class with using, but the initialization list of the constructor will prompt that the member N cannot be found.

// The derived class uses the using declaration to restore the access level of the inherited member. # Include <iostream> class base {public: STD: size_t size () const {return N;} protected: STD: size_t n ;}; class derived: private base {// Private inheritance, all members become private public: using base: size; // using base: N; // magic, can be promoted to public, directly access d1.n. The magic is not that the geany compiler and G ++ are protected: // using base: N; private... What is "cannot make the access level more strict or loose than originally specified in the base class" using base: N; // whether the declaration is placed behind protected or private, d1.n indicates that N is protected. But public ...//~ With a small mistake, set the N of the base class to private. What is the difference in this situation ~ Is it wrong to restore to private ?}; Class public_derived: public base {// Private inheritance, all members become private public: // using base: size; protected: // using base: N; private: // using base: N;}; int main () {/* base B1; derived D1; public_derived pd1; // guess: the Storage Area of an object stores all types of objects sequentially. The first address is the base class sub-object, the second is the extra part of the derived class, and the second is the derived class of the derived class... Base * bp1 = & D1; // Private inheritance has changed the access permission of the base class, so the base class pointer cannot point to it anymore ~ "Base is a part that cannot be accessed in derived", so public inheritance is a prerequisite. Bp1 = & pd1; public_derived * pdp1 = bp1; // prerequisite: public. The pointer of a derived class cannot refer to objects of the base class. It can only be backward compatible with derived * dp1 = & B1; //, so it is not in conflict with the address conjecture, this indicates which sort is likely, so it cannot be divided. */Derived D1; STD: cout <d1.n; // This permission is elevated to public, but cannot be initialized. I Cannot initialize the derived class at all times ...}

The default inheritance protection level (the class and struct in question are resolved ):

Default inheritance. cpp

Simple concept. It is not important by default. You can set it manually.

// The default inheritance protection level class base {}; struct D1: Base {}; // public inheritance by defaultclass D2: Base {}; // Private inheritance by default // This is only the default value. It doesn't matter if you specify it every time... // Original book: the only difference between class and struct is the default Member Protection Level and the default derived protection level (What are these two terms) // The following D3 and D4 definitions share the same class D3: public base {public :/*... * //}; // Equivalent Definition of d3struct D3: Base {// inheritance public by default /*... * // initial number access public by default}; struct D4: Base {PRIVATE :/*... * // Equivalent Definition of d4class D4: Base {// inheritance private by default /*... * // initial member access private by default}; // tips: because private inheritance uses class reserved words as the default condition, it is rare to use them in practice, therefore, you must explicitly use privateint main (){}

Why can access the base class, but cannot use the constructor to initialize the base class members. The base class members in the derived class are also completed using the base class constructor.

class Gif : public Picture_format{public:Gif(size_t i = 20) : pixels(i) {}size_t fcn(){ return pixels; }};

Pe15_11.cpp

Something you can understand:

Inherited member status:

The constructor of the derived class Cannot initialize the inherited members in the initialization list and cannot be recognized.

For a derived class object, the inherited member is alsoConstructor of the base classInitialized, but the derived class can directly read it. pixels is used in FCN ().

In this case, the base class derived classes seem to be truly separate modules.

Class picture_format {public: picture_format (size_t I = 10): pixels (I) {} protected: size_t pixels; // pixel private :}; class GIF: Public picture_format {public: size_t FCN () {return pixels ;}}; // GIF g (); // This G is not a class object of GIF. Is it a function or something ?! GIF g; STD: cout <G. FCN () <STD: Endl;

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.