In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "C++ class inheritance and derivation and pointer security how to refer", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let Xiaobian take you to learn "C++ class inheritance and derivation and pointer security how to refer"!
1. Inheritance and paisheng1. Basic concepts of inheritance and derivation
Inheritance refers to obtaining properties from an existing class, and derivation refers to generating a new class from an existing class. The original class is called the base class or parent class, and the new class is called the child class or derived class. When a subclass has only one parent, it is called single inheritance, and when it has multiple parents, it is called multiple inheritance. If base class A participates directly in the derivation of derived class B, class A is called a direct base class; the base class of the base class is called an indirect base class.
2. How to define subclasses
When defining a subclass, you need to specify its parent class and how it inherits from the parent class (public, private, protect):
class subclass name: inherit from parent class 1, inherit from parent class 2{}
Subclasses not only inherit all the members of the parent class, but also define new data members and function members. The new members are called subclass members (or derived class members).
3. Absorb and transform parent class members, add new members
In the process of child inheritance parent class, there are the following points:
Subclasses inherit all members of their parent class except constructors and destructors;
Subclasses transform members inherited from parent classes, including access control, overriding, and hiding;
Add new members that the parent class does not have;
The Animal class defined below, as the parent class of the Duck class, is defined as follows:
class Animal {public: Animal(int age_) { age = age_; printf("Init Animal \n"); } void eat() { printf("Animal eating! \n"); } void run() { printf("Duck Running! \n"); }private: int age=0;};class Duck:public Animal {public: ///Subclasses need to complete initialization tasks of parent classes /// Duck class calls initialization function of Animal class Duck(int age_) :Animal(age_) { printf("init Duck! \n"); }; void eat() { printf("Duck eating! \n"); }};
In the above code, the corresponding three processes are as follows:
(1) Absorption: Duck class inherits all members of Animal class except constructor and destructor function, so Duck class object can use run() function of Animal class:
duck.run();
(2) Transformation: Duck class defines the eat() function with the same name and return type as the parent class, belonging to the function coverage (if the return type and parameter table are not overloaded), directly using the function name to access the eat() function of Duck class, called the same name hidden:
duck.eat();
(3) Add: The syntax for adding ordinary function members and data members is the same as when adding members to general classes. The difficulty lies in constructors and destructors, because subclasses involve constructors and destructors of parent classes.
1. The difference between a pointer and a reference
In C++, pointers and references coexist, and they seem to have many similarities, but they are not exactly the same. In some cases, pointers can do what references can do, so when is it appropriate to use pointers and when is it appropriate to use references? The following is not classified according to the differences in nature between the two, but directly from the perspective of use.
First of all, analyze the similarities between the two: pointers and references are not passed values when they are passed as function parameters. It can be considered that both of them pass addresses, that is to say, both of them can effectively reduce the memory overhead of the output transmission process.
The second difference is that references must be initialized at the same time as they are declared, pointing to an existing object. Pointers do not have to be initialized, and pointers can be defined as null pointers, meaning they do not point to any object. Another significant difference is that once a reference points to an object, it cannot point to another object, whereas a pointer can change the object it points to.
As you can see from the similarities and differences above, a reference is more like a constant pointer. Therefore, the pointer can implement all the functions that can be implemented by reference. C++ does not use the concept of references for C compatibility, so references must have their advantages. According to Zheng Li teacher book introduced,"function parameters bi-directional transmission and avoid using pointer arithmetic operation" operation, reference has the effect of safe processing data, at this time using pointer will increase the complexity of the code. (Personally think: the role of reference is still not obvious, but will cause different programmers 'code readability is poor, may be that they have not really mastered the power of reference).
Pointers and references still have a lot to discuss, according to the syntax and definition of both can be very natural reasoning out, such as new application memory address can only be assigned to pointers, and so on.
2. Safety hazards of pointers
Pointer although powerful, but this operation for the address of the programmer's level requirements are also relatively high, so there are many security risks. For example, when using pointers to handle arrays, and may cause out-of-bounds problems, which can have unpredictable effects if subscript checking is not performed.
Of course, there were still many hidden safety problems with pointers, but he had only understood a little so far. He would record them later when he understood them more deeply.
At this point, I believe that we have a deeper understanding of "C++ class inheritance and derivation and pointer safety how to refer", may wish to actually operate it! Here is the website, more related content can enter the relevant channels for inquiry, pay attention to us, continue to learn!
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.