Object-oriented understanding in C ++, and object-oriented understanding in C ++

Source: Internet
Author: User

Object-oriented understanding in C ++, and object-oriented understanding in C ++
Zookeeper

1. Not everyone thinks the meaning of OO is the same. Even now, if you ask ten people, you may get 15 different answers. Almost all people agree that inheritance and polymorphism are concepts in OO. Most people will add encapsulation. Some others will add exception handling, but some may not include the template. The key to the problem is: there are often different opinions on whether a feature belongs to OO, and each view has enthusiastic advocates.
2. The most important contribution of C ++ standardization to C ++ is that it provides more powerful support for powerful abstract mechanisms, thus reducing the complexity of software. C ++ is not just an object-oriented language. It supports several programming styles, including object-oriented and generic programming styles. These programming styles are very important, because each programming style provides a flexible way to organize code through an abstract mechanism. Object-Oriented Programming enables us to bind object states and functions that process these States together, while encapsulation and inheritance allow us to manage mutual dependencies, and enables code reuse in a clearer and easier way. Generic programming is a new style that enables us to write some functions and classes, they can operate on other "undefined, unrelated, and unknown types" functions and objects, providing a unique way to reduce coupling and mutual dependency in the program. Currently, generic support is also provided in some other languages, but it is not as powerful as C ++'s support for generics. In fact, the current generic programming may all be attributed to the unique template form of C ++. Today, many powerful methods are provided in C ++ to express abstract mechanisms. The resulting flexibility is the most important achievement in C ++ standardization.
3. There is no Module System in C ++ (both complete and incomplete); it lacks other important features, such as the memory garbage collection mechanism, it has a static type check, but it is not necessarily a "strong" type check.
1. Strong/weak type indicates the type check degree. There are no types, weak types, and strong types of languages. If there is no type, do not check or even distinguish between commands and data. Weak type checks are weak, and only commands and data can be strictly distinguished. If the type is strong, it is strictly checked during the compilation period.

Strong Type Definition Language: language that enforces data type definition. That is to say, once a variable is specified with a data type, if it is not forcibly converted, it will always be the data type. For example, if you define an integer variable a, it is impossible for the program to treat a as a string type. A strongly typed language is a type-safe language.

Weak Type Definition Language: language in which data types can be ignored. It is opposite to a strongly typed definition language. A variable can assign values of different data types.

2. Dynamic type language: a dynamic type language is a language that performs data type checks during runtime. That is to say, when programming in a dynamic type language, you never need to specify a data type for any variable. This language records the data type in the internal part when you assign a value to the variable for the first time.

Static type language: the static type language is the opposite to the dynamic type language. Its data type is checked during compilation, that is, the Data Types of all variables must be declared during program writing, c/C ++ is a typical representative of static language. Other static language types include C # and JAVA.

The speed of a strong-type definition language may be slightly inferior to that of a weak-type definition language, but the rigor of a strong-type definition language can effectively avoid many errors. In addition, there is no connection between "is this language a Dynamic Language" and "is this language a type of security!

3. Encapsulation

Encapsulation is the basis for ensuring the excellent controllability of software components. The purpose of encapsulation is to achieve "High Cohesion and low coupling" of software components to prevent the impact of changes caused by the mutual dependence of programs. In object-oriented programming languages, objects are the most basic unit of encapsulation. Object-oriented encapsulation is clearer and more powerful than traditional language encapsulation. Object-oriented encapsulation is to encapsulate the code describing the attributes and behaviors of an object in a "module", that is, a class. attributes are defined by variables and behaviors are defined by methods, you can directly access the attributes of the same object.

First, good encapsulation can reduce coupling.
Second, the internal implementation of the class can be freely modified.
Third, the class has clear external interfaces.
The benefits of encapsulation are well understood. Our house is a class instance. The interior decoration and decoration can only be enjoyed and used by the indoor residents. If there is no wall covering, all activities in the room are visible to outsiders. With encapsulation, all the furnishings in the house can be changed at will without affecting others. However, if there are no doors and windows, a black box is wrapped strictly, even if its space is wider, there is no practical value. The doors and windows of a house encapsulate the properties and methods exposed by the object for people to access, as well as air circulation and sunlight.

4. Inheritance

When defining and implementing a class, you can base on an existing class and take the content defined by this existing class as your own content, some new content can be added, or the original method can be modified to make it more suitable for special needs. This is inheritance. Inheritance is the mechanism by which child classes automatically share parent class data and methods. This is a is-a relationship between classes and improves software reusability and scalability.

1. subclass has non-private attributes and functions of the parent class;
Second, subclass has its own attributes and functions, that is, subclass can expand the attributes and functions that the parent class does not have;
Third, subclass can also implement the function of the parent class in its own way (method rewriting ).
A constructor cannot be inherited but can only be called. Inheritance destroys encapsulation, and the Implementation Details of the parent class are exposed to the Child class. This actually increases the coupling between the two classes and is strongly coupled.

5. Polymorphism

The same operation acts on instances of different classes and produces different execution results, that is, different results are obtained when objects of different classes receive the same message. Polymorphism is one of the important features of object-oriented programming, and is another major manifestation of scalability after "inheritance. An object acts based on the received message. The same message may lead to completely different behaviors when it is accepted by different objects. This phenomenon is called polymorphism.

Virtual methods are dynamically bound and called Based on the runtime type rather than the compile-time type.

6. Abstraction

Abstraction is to find out the similarities and commonalities of some things, and then classify these things into a class. This class only considers the similarities and commonalities of these things, in addition, those aspects irrelevant to the current topic and goal will be ignored and the focus will be on the aspects related to the current goal.

1. abstract classes cannot be instantiated;
2. the abstract method must be overwritten by the quilt class;
Third, if a class contains abstract methods, the class must be defined as an abstract class, whether or not it can contain other general methods.

7. Interfaces

An interface combines implicit public methods and attributes to encapsulate a set of specific functions. Once the class implements the interface, the class can support all attributes and members specified by the interface. The Declaration interface is syntactically identical to the declaration abstract class, but it cannot provide the execution method of any member of the interface. Therefore, the interface cannot be instantiated, there cannot be constructor methods and fields, there cannot be modifiers, and it cannot be declared as virtual or static. Classes that implement interfaces must implement all methods and attributes of interfaces. A class can support multiple interfaces, and multiple classes can also support the same interface.
Abstract classes can be implemented by some Members, but interfaces do not contain Member implementations. Abstract members of abstract classes can be partially implemented by the quilt class. Interface members must implement classes completely, A class can inherit only one abstract class, but can implement multiple interfaces.

Zookeeper
How can process-oriented and object-oriented programming be visually understood in C and C ++?

Process-oriented is a unit of function modules, typical of which are functions and processes,
Object-oriented is the unit of class/object, typical of which is Class
Object-oriented is a process-oriented development. It binds related data and operations through classes.

To put it simply
If you want to build an airline system website, such as a passenger, you can write all attributes and methods of all passengers, regardless of whether the project needs these functions.
The process-oriented approach is the attribute method you want to write.
When you need to add some passenger functions in this project, if you want to use the object-oriented thinking, you can simply write code in that function, the process-oriented approach requires re-writing, which is a lot of trouble. It cannot reflect some of the reuse, encapsulation, and other performance of the program itself.
Process-oriented code implementation is much faster than object-oriented code implementation, because it can be written when it comes to thinking ~
Do you understand?

C ++ is object-oriented, and C is process-oriented. What is this object and process?

Object-oriented refers to encapsulating attributes and methods into classes. After an object is instantiated, you can directly call the corresponding methods in the class to complete an operation. Process-oriented data is not encapsulated. to complete any function, you need to write the algorithm in detail. For example, if I want to complete the shopping task, the object-oriented implementation method is to first train someone under my opponent, teach them how to buy (equivalent to defining the attributes and methods of the class). If they want to buy things in the future, they just need to shout "James (or Lisi, equivalent to instantiating objects ), you can use the method I taught you last time to buy something. The process-oriented approach does not provide training. Every time you go to shopping, you need to contact Michael and teach him how to buy it. But next time you call him to buy it, you have to teach him again.

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.