C language is process-oriented programming, its most important feature is the function, through the main function to call a sub-function. The order in which the programs are run is the programmer's decision. It was the first program language I learned.
C + + is Object-oriented programming, the class is its main features, the program execution process, the first by the main function into, define some classes, as needed, execute the class member function, the concept of the process is diluted (actually the process is still some, is the main function of those statements), class is the object, so we call object-oriented programming.different points:1. Programming model
All computers are made up of two elements: code and data. To be precise, some programs revolve around "What is happening"And some of it is around"who is being affected"and wrote.
The first type of programming is called a process-oriented model, and programs written in this model feature a series of linear steps (code) that can be understood as code for data. such as C, the process of language.
The second programming method is called "Object-oriented model", and the program is organized around the program's data (object) and the interface strictly defined for the object, which is characterized by the access of the Data control code. By transferring control to the data, the object-oriented model is organized in the following ways:abstraction, encapsulation, inheritance, and polymorphismThe benefits.
2. Abstraction
The basic element of object-oriented programming is abstraction, in which programmers manage complexity through abstraction.
An effective way to manage abstractions is to use hierarchical classification features, which allow users to break down a complex system based on physical meanings and divide it into more manageable chunks. For example, a computer system is a separate object. And inside the computer system consists of several subsystems: monitor, keyboard, hard disk drive, DVD-ROM, floppy disk, sound, etc., each of these subsystems is made up of specialized components. The key is the need to use hierarchical abstraction to manage the complexity of a computer system (or any other complex system).
The nature of object-oriented programming: These abstract objects can be thought of as concrete entities that respond to messages used to tell us what to do.
/* (My understanding)
* The computer is an entity, I want to enter characters, display the display, then
* Computer (object). Input (keyboard properties). Display (Display method)
* Use hierarchies to refer to and manipulate. Without having to control how the computer is handled internally.
* As long as there is a computer object, it can respond to my operation, and I hit the keyboard,
* The computer object transmits this message to the screen, which is displayed on the screen.
*/
The computer object contains all of its properties, as well as its operations, which is one of the three principles of object-oriented programming: encapsulation.
3. Encapsulation
Encapsulation is a mechanism that binds code and code to manipulate data so that it is not subject to external interference or misuse. Encapsulation can be understood as a wrapper for protection, To prevent code and data from being accessed arbitrarily by other code defined outside the wrapper. Access to the wrapper's internal code and data is controlled by a well-defined interface. The benefit of encapsulating code is that everyone knows how to access the code, so that it can be used directly without having to worry about the details of the implementation, without worrying about unforeseen side effects.
In Java, the most basic encapsulation unit is a class, and a class defines the behavior (data and code) that will be shared by a set of objects. Each object of a class contains the structure and behavior it defines, and these objects appear to be cast as a mold. So objects are also called instances of classes.
When defining a class, you specify the code and data that make up the class. In particular, the object defined by the class is called a member variable or an instance variable. The code that operates the data is called a member method. Methods define how member variables are used, which means that the behavior and interfaces of the class are defined by the methods that manipulate the instance data.
Because the purpose of a class is to encapsulate complexity, the inside of a class has a mechanism for hiding implementation complexity. So the private and public access patterns are available in Java, The public interface of a class represents everything that an external user should know or know about. Private method data can only be accessed through the member code of the class. This ensures that unwanted things don't happen.
4. Inheritance
Inheritance is the process by which an object obtains an attribute from another object. It is the second of three principles of object-oriented programming, which supports the concept of hierarchical classification. For example, a Persian cat is a cat, a cat is a mammal, a mammal is a kind of animal. If you don't use the concept of hierarchy, Each object needs to clearly define all of its characteristics. Hierarchical classification means that an object needs to be defined only in its class as the individual properties that it becomes unique, and then inherits its common properties from the parent class. Therefore, it is because of the inheritance mechanism, Enables an object to become a specific instance of a generic class. A subclass of a depth inheritance inherits all of its properties from each ancestor in its class hierarchy.
Inheritance and encapsulation can interact with each other. If a given class encapsulates certain properties, any of its subclasses will have the same attributes. Plus all the attributes of each subclass. This is an important concept in which object-oriented programs are linearly rather than geometrically grown in complexity. The new subclass inherits all of its ancestors ' properties. Subclasses and other code in the system do not produce unpredictable interactions.
5. polymorphic
Polymorphism refers to a method can have only one name, but there are many forms, that is, the program can define multiple methods with the same name, "one interface, multiple methods" to describe. You can refer to the parameter and type of the method.
6. Encapsulation, inheritance, multi-state combination use
In an environment of encapsulation, inheritance, polymorphism, programmers can write programs that are more robust and extensible than process-oriented models. A carefully designed class hierarchy is the basis for reusing code. Encapsulation allows programmers to migrate programs without having to modify the code of the public interface. Polymorphism enables programmers to develop simple, understandable , easy-to-modify code. For example: Automotive
From the perspective of inheritance, drivers rely on inheritance to drive different types (subcategories) of cars, whether the car is a car or a truck, Mercedes or Fiat, the driver can find the steering wheel, handbrake, gear shifters. After a period of driving, you can know the difference between manual and automatic files, Because they actually know the common superclass of both: gearing.
From a package point of view, the driver always sees the encapsulated features. The brakes hide a lot of complexity, and its appearance is so simple that it can be manipulated with its feet. The implementation of the engine, handbrake, tyre size has no effect on the definition of the brake class.
From the multi-state point of view, the braking system has a positive lock locked, the driver only with foot brake parking, the same interface can be used to control a number of different implementations (positive or locked).
So that the individual components are converted to the object of the car. Similarly, by using object-oriented design principles, programmers can combine the various components of a complex program to form a consistent, robust, maintainable program
What is the difference between object-oriented programming and process-oriented programming?