In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
Today Xiaobian to share with you C++ single inheritance and multiple inheritance how to use the relevant knowledge points, detailed content, clear logic, I believe most people still know too much about this knowledge, so share this article for everyone to refer to, I hope you read this article after some harvest, let's learn about it together.
preface
C++'s inheritance mechanism is relatively complex compared to other languages. Unlike java, which only supports single inheritance, C++ not only supports single inheritance, but also supports multiple inheritance. For the diamond problem in multiple inheritance, it will cause a series of troubles. C++ has two important defects, one is multiple inheritance, and the other is garbage collector. This article will explain in detail C++ single inheritance and multiple inheritance, as well as diamond inheritance solutions and principles.
1.(1) The concept of inheritance
Inheritance is an important means of object-oriented design to make code reusable. It allows programmers to extend existing classes. An extended class is called a base class or parent class, an extended class is called a subclass or derived class, and inheritance is reuse at the class design level.
Inheritance allows a subclass to include members of its parent class as well as its own.
(2)Inherited definition method class Person{private: string _name; int _age;};class Student :public Person{private: int _id;};
Look at this code, where the subclass Student inherits the parent class Person, and the public after Student indicates the inheritance method.
(2)Member types of inherited subclasses
The inheritance method and the member attributes of the parent class together determine the member attributes of the child class. Let's use a table to show the relationship between the three.
class member/inheritance method public inheritance protected inheritance private inheritance public member of base class public member of derived class protected member of derived class private member of base class protected member of derived class protected member of derived class private member of base class invisible in derived class
We only need two points to memorize this table:
1. Private members of a base class are invisible in derived classes, regardless of how they are inherited.
2. The member attribute in the subclass takes the inheritance method and the member attribute of the parent class with the lower permission: public>protected>private
Description of table:
1. Invisible does not mean that it is not inherited, but that it cannot be used. It is more convenient to inherit it at the bottom than not to inherit it.
2. In parent classes there is no difference between private and protected, but in child classes protected members can be accessed within the class, while private cannot, so protected can be said to exist for inheritance.
3. If you do not write inheritance, if the subclass is class defined, then the default is private inheritance, struct defined, default is public inheritance.
4. The difference between invisible and private members: invisible means that they cannot be used inside and outside the class, private members can be used inside the class, and they cannot be used outside the class.
5. Members we don't want to give subclasses access to are set to private.
2. The assignment conversion between base class and derived class (1) assigns derived class to base class
We define a parent class person and its derived class student, and these are their respective members.
When we assign an object of a derived class to an object of a base class, the process that occurs is called slicing. That is, only the parent members of the subclass are assigned to the past. When there are private members in the parent class, they will also be sliced, but they will not be displayed, so try not to define private members in inheritance.
Note that this assignment compatibility is limited to public inheritance.
Private inheritance does not support slicing, because for public members in the parent class, private or protected inheritance will be converted to private/protected type, and assignment will occur when private/protected members in the derived class object are assigned to public members in the parent class object, but private/protected members cannot be accessed outside the class, so private inheritance is not supported.
Person b; Student a; b = a; Person* ptr = &a; Person& ref = a;
Note a detail, we can use reference assignment, indicating that there is no type conversion behavior, because the type conversion will generate temporary variables in the middle, you need to use const reference.
double d;const int& r=d;//type conversion occurred. (2)Base class to derived class
Let's start with the conclusion:
Parent objects cannot be assigned directly to child objects.
This is because subclass objects have types whose parent class does not exist and cannot be assigned. Nor can assignments be made through so-called cast types.
But C++ supports pointer and reference assignments:
Person b; Student a; a = (Student)b;//Incorrect Student* ptr = (Student*)&b;//Support Student& ref = (Student&)b;//Support
While pointers and references are fine, problems arise when pointers go down past parent class objects.
There will be a point to nothing situation.
3. Scope in Inheritance (1) Hidden Concepts
Base classes and derived classes have their own independent scopes.
If there are members with the same name in different domains, we specify the location of the members according to the proximity principle or the way the scope is specified.
Hidden: Subclass and parent class members appear in the same name, child members will block direct access to the parent class members of the same name, this situation is called hidden, also known as redirection.
Note that if it is a member function hidden, as long as the function name is the same, it will constitute a hidden, independent of parameters.
Take an example:
class Person{protected: string _name = "small six child"; int _num = 111;};class Student :public Person{public: void Print() { cout
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.