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 concept of object-oriented programming in C++

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

What this article shares with you is about the concept of object-oriented programming in C++. The editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article. Let's have a look together with the editor.

One. Object (Object), this concept can be said to be the core concept of object-oriented, if you can not find the object, how to object-oriented? Object, that is, you have to deal with a number of factors designed in the problem, such as you do student performance statistics, then the student is of course the object you want to consider.

Class (Class), in essence, there are objects before classes, because when dealing with actual programming problems, you are faced with specific objects one by one, too many, what to do? Classify, birds of a feather flock together, by the way, consider objects with the same attributes as a category, Zhang San or Li Si, they are all examples of people. In a specific language implementation, the order of the two needs to be reversed, that is, there must be a class before you can instantiate the generated object.

Three. three major characteristics of object-oriented:

1. Encapsulation

The so-called encapsulation is to implement different permission settings for data and methods within the class when designing the class. We do not want users to call some data and methods outside the class through instantiation, so we declare them as private (private), so that the data and methods can only be accessed by member functions within the class, and if they want to be accessed by instances, they are declared public. As for the third access separator (protected) protected type, which is applied in the inheritance of the class, let's go to the inheritance to explain.

One concept related to encapsulation is "abstract", which represents the most important functions of the classes to be designed, which are accessed by other classes and functions.

two。 Inherit

The concept related to inheritance is relationship, which describes the relationship between classes. There are four basic types, namely, inheritance, combination, utilization, and instantiation. Inheritance means that one class has all the data and methods of another class, of course, you can modify some of them and add new data and methods. There are two types of inheritance, single inheritance and multiple inheritance.

Single inheritance, that is, class Derived inherits class Base, defined as follows:

Class Base {}; class Derived:public Base {}

It can be seen that when defining an inherited class, it is necessary to specify which class is inherited and the inheritance level (public,protected,private). Through the modification of these three access identifiers, the access permission of the base class data in the derived class can be changed, and public inheritance will not change. If protected inherits, the original public data and methods will become protected data and methods of the derived class, and the rest will remain the same. Private inheritance sets the access rights of all data and methods to private, that is, none of these data and methods can be accessed outside the class.

The construction and destructing order of the derived class, first construct the base class, and then construct the derived class, in the middle is the function of the class, and the destructing order is just the opposite.

Multiple inheritance means that a class inherits data or methods from two or more classes. The main problems foreseen here in multiple inheritance are two ambiguities: first, when two inherited base classes contain the same data or methods, calls in derived classes cannot be distinguished.

Solution: specify the member variables and methods of which class to call, use the scope decomposition operator; redefine the data or method in the derived class. The second kind of ambiguity occurs in multi-tier inheritance, BMague C inherits A, and D inherits BMagne C, just as rectangle and diamond inherit quadrilateral while square inherits rectangle and rhombus, two inheritance paths make base class inherit twice, direct access can not be distinguished, solution: payout inheritance path, use virtual base class (virtual) method, add virtual when defining inheritance relationship so that base class always has only one copy.

3. Polymorphisms

There are two kinds of polymorphism, namely static polymorphism and dynamic polymorphism. The so-called polymorphism, that is, obviously the same thing has different performance in different situations.

Static polymorphism, that is, pre-defined, is realized through function overloading (funcition overload). The function contains the return value, function name and function parameter. The name often indicates the function. The distinction of the return value cannot distinguish the function (imagine the forced conversion of the data class). Therefore, overloading means that the name of the function is the same but the parameters are different, and the difference can be shown in the number of parameters, the type of parameters, and the order of parameters.

These are the concepts of object-oriented programming in C++. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

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