Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the basic concept of C++ inheritance?

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

What is the basic concept of C++ inheritance, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain in detail for you, people with this need can come to learn, I hope you can gain something.

C++ programming language also has object-oriented related features. So what are its characteristics? Here we will introduce some basic features of C++ inheritance in detail, hoping to help you have a detailed understanding of the language.

1. What are the basic concepts of object-oriented technology?

Objects, classes, and inheritance.

two。 Virtual function and Polymorphism

Virtual functions are member functions that are allowed to be redefined by their subclasses. The practice of subclasses redefining parent virtual functions is called "override".

Polymorphism is a technique that allows you to set a parent object to be equal to one or more of its child objects, and after assignment, the parent object can operate differently according to the child object to which it is currently assigned. To put it simply, it allows pointers of subclass types to be assigned to pointers of parent class types. Polymorphism is realized by virtual functions in C++.

3. What is the role of polymorphism?

Encapsulation can hide implementation details and make the code modular; C++ inheritance can extend existing code modules (classes); they are all for code reuse. The purpose of polymorphism is to achieve another purpose-interface reuse!

4. Interface

C++ actually does not have the keyword interface, it is implemented with pure virtual functions of abstract classes.

5. About CLA

The private internal representation of the class is called encapsulated, while the common part of the class is called the class interface (class interface).

Co-member: can be accessed anywhere in the program. Classes that implement information hiding restrict their public members to member functions. To access member functions, you must define a class object.

Private members: can only be accessed by member functions and friends of classes, and classes that implement information hiding declare their data members as private.

Protected members: treat derived classes like public and behave like private to other programs. In a class without C++ inheritance, there is no difference between the protected and private controllers.

6. Common inheritance, private inheritance, protection inheritance

In the case of public inheritance, objects of a derived class can access public members in the base class, and member functions of a derived class can access public members and protected members in the base class.

In private inheritance, all members of the base class are invisible to the derived class object.

For derived classes, the public and protected members of the base class are visible and can be accessed with member functions.

Private inheritance means that members of the base class cannot be inherited by subsequent classes.

When protecting C++ inheritance, both the common and protected members of the base class are protected members of the derived class, which means that they can be accessed by "classes that later derive from that class" but not outside the hierarchy.

7. Virtual function inheritance and virtual inheritance

The two concepts are easily confused.

Virtual function inheritance is actually virtual function. It's just that the virtual function must be used in C++ inheritance, otherwise it won't be necessary. It refers to a dynamic binding process.

Virtual inheritance is a unique concept in multiple inheritance, which refers to the parent class that only retains the parent class in the subclass. So that the subclass does not contain the same member of the parent class more than once.

Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report