C ++ class inheritance relationships

Source: Internet
Author: User

Http://www.cppblog.com/chemz/archive/2007/05/31/25189.html

C ++ class inheritance relationships
There are three main inheritance relationships in C ++: public, protected, and private. Public in the three inheritance relationships
Inheritance is the most commonly used inheritance relationship, which represents the meaning of interface inheritance. What exactly do they mean?
1. Public
In terms of semantics, public inheritance is an interface inheritance. According to the relationship in the object-oriented model, it is a subclass.
It can replace the parent class to complete the behavior declared by the parent class interface, that is, it must comply with the "liskov replacement principle (LSP )",
In this case, the subclass can be automatically converted to the interface of the parent class to complete Interface Conversion.
In terms of syntax, public inheritance retains the visibility of the members (including functions and variables) in the parent class,
That is to say, if a function in the parent class is public, it is still public after the quilt class is inherited.

2. Protected
From a semantic point of view, protected inheritance is an implementation inheritance. According to the relationship in object-oriented systems,
Subclass cannot replace the parent class to complete the behavior declared by the parent class interface, that is, it does not comply with the "liskov replacement principle (LSP )",
At this time, the subclass cannot be automatically converted to the interface of the parent class, even if the type conversion (static_cast and dynamic_cast)
A null pointer is also obtained.
In terms of syntax, protected inheritance modifies the public visibility member in the parent class to protected.
Visibility, which is equivalent to introducing the protected member in the subclass, so that the parent can still be called in the subclass.
The protected and public members of the class. The subclass can also call the protected of the parent class inherited by protected.
And public members.
For example:
Class csample1 {
Protected:
Void printprotected (){}
Public:
Void printpublic (){}
};
Class csample2: protected csample1 {

};
Class csample3: Public csample2 {
Void print3 (){
Printprotected ();
Printpublic ();
}
};
3. Private
From the semantic point of view, private inheritance is an implementation inheritance. According to the relationship in object orientation,
Subclass cannot replace the parent class to complete the behavior declared by the parent class interface, that is, it does not comply with the "liskov replacement principle (LSP )",
At this time, the subclass cannot be automatically converted to the interface of the parent class, even if the type conversion (static_cast and dynamic_cast)
A null pointer is also obtained.
In terms of syntax, private inheritance modifies the public and protected visibility members in the parent class
Private visibility, so that although the Child class can also call the protected and public members of the parent class,
However, in the subclass, you cannot call the members of the parent class inherited by private.
Class csample1 {
Protected:
Void printprotected (){}
Public:
Void printpublic (){}
};
Class csample2: Private csample1 {

};
Class csample3: Public csample2 {
Void print3 (){
Printprotected (); // compilation error. This function cannot be called.
Printpublic (); // compilation error. This function cannot be called.
}
};
 
There are two concepts in the object-oriented theory: interfaces and implementations, so the so-called interface inheritance and implementation
. Protected and private are used to implement inheritance. In fact, protected and private
The two constraint inheritance does not form two different inheritance classes, but only to facilitate the transfer of C ++ class methods
In fact, in a language with stricter object-oriented requirements such as Java, it does not implement inheritance and must
This concept is achieved through delegation. If you are familiar with Java, you will understand that if an object needs to use another object
But cannot act as the role of the object, it is done through delegation.
You must include a delegate object in the object and use the object call syntax to complete the function. In C ++, you can use
Protected and private inheritance are used to complete the delegate relationship in Java (of course, C ++ can also form an object delegate relationship ),
In this case, protected inheritance allows the delegate to be passed (that is, it is called by multi-level subclass), while private
Inheritance does not allow delegation to be passed.

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.