Object Oriented vs Process Oriented Programming

From a linguistic point of view:

C is procedure-oriented programming;

C++ is half process-oriented programming and half object-oriented programming;

Java is object-oriented programming.

1. What is object-oriented programming and process-oriented programming?

Procedure Oriented (PO for short): It is a process-centered programming idea, which analyzes the steps to solve the problem, and then uses functions to realize these steps step by step. In process-oriented programming, data and operations on data are separated.

Object Oriented (OO for short): Object Oriented abstracts things into the concept of objects, first abstracts objects, then assigns some properties and methods to objects, and then lets each object execute its own methods. In object-oriented programming, data and operations on data are bound together.

for example:

1. Laundry

Process-oriented:

Put clothes (method) --> add washing powder (method) --> add water (method) --> rinse (method) --> wash (method) --> dry (method)

Object-oriented:

new produces two objects "person" and "washing machine"

"People" add attributes and methods: put clothes (method), add washing powder (method), add water (method)

"Washing machine" adds properties and methods: rinse(method), wash(method), spindry(method)

Then execute:

Person. Put clothes (method) -> Person. Add washing powder (method) -> Person. Add water (method) -> Washing machine. Rinse (method) -> Washing machine. Clean (method) -> Washing machine. Spin dry (method
 

2. Find the perimeter and area of ​​a rectangle


Process-oriented programming method:
1. Algorithms for determining the perimeter and area of ​​a rectangle.
2. Write two methods (functions) to calculate the perimeter and area of ​​a rectangle respectively.
3. The method (function) to find the perimeter and the method (function) to find the area need two parameters, which are the length and width of the rectangle.

Object-oriented programming method:
1. A rectangle can be regarded as a rectangular object.
2. A rectangular object has two states (length and width) and two behaviors (perimeter and area).
3. Extract the commonality of all rectangles and design a rectangle class.
4. Through the behavior of the rectangular object, the perimeter and area of ​​a specific rectangular object can be calculated.
 

2. Advantages and disadvantages of process-oriented and object-oriented

Process-oriented:

Pros: Efficient because no object needs to be instantiated.

Disadvantages: high coupling, poor scalability, difficult to maintain (for example: every step must have, otherwise it will not work)

Object-oriented:

Advantages: low coupling (easy to reuse), strong scalability, and easy maintenance. Because object-oriented has the characteristics of encapsulation, inheritance, and polymorphism, a low-coupling system can be designed to make the system more flexible and easier to maintain.

Disadvantages: less efficient than process-oriented.
 

3. Object-oriented programming: the relationship between classes and objects

Object
An object is a package composed of data (describing the attributes of things) and operations on the data (reflecting the behavior of things). It describes an entity of objective things and is the basic unit of the system.

Class A
class can be regarded as an upgraded version of the structure ( struct) in the C language. In the C language structure, only variables can be defined. In C++, a class can not only define variables, but also define functions. 

A class is the definition of a group of objects with the same data and the same operations. It is the template of the object, and the methods and data it contains describe the common behavior and attributes of a group of objects. A class is an abstraction above an object, and an object is the embodiment of a class, an instance of a class. A class can have its subclasses, as well as other classes, forming a class hierarchy.

The difference between a class and an object:

1) A class is an abstraction of an object, and an object is a concrete instance of a class. Classes are abstract and take up no memory, while objects are concrete and take up storage space.

2) A class is an abstraction of a group of objects with the same properties and behavior. We can think of a class as a blueprint for creating an object , and the object implements something according to this blueprint.

for example:

Sun umbrellas and sword umbrellas have some common attributes: sunshade, rainshade, etc. Umbrella is an abstraction of Sunny Umbrella and Sword Umbrella, and Sunny Umbrella and Sword Umbrella are two concrete implementations of the umbrella class.

Therefore, the instantiation result of a class is an object, and the abstraction of a class of objects is a class, which describes a group of objects with the same attributes and methods.

insert image description here

Three, the characteristics of object-oriented programming

The three characteristics of object-oriented programming are: encapsulation, inheritance, and polymorphism .

Among them, encapsulation and inheritance are for code reuse, and polymorphism is for interface reuse.

encapsulation

Organically combine the data and the method of operating the data, hide the properties and implementation details of the object, and only expose the interface to interact with the object. Encapsulation is essentially a management that makes it easier for users to use classes.

insert image description here

Example:

For a complex device such as a computer, the only thing provided to the user is the power button, keyboard input, monitor, USB jack, etc., allowing the user to interact with the computer and complete daily tasks. But in fact, the real work of the computer is some hardware components such as CPU, graphics card, and memory. For computer users, they don’t need to care about internal core components, such as how the circuits on the motherboard are laid out, how the CPU is designed inside, etc. Users only need to know how to turn on the computer and how to interact with the computer through the keyboard and mouse. Therefore, when the computer manufacturer leaves the factory, it puts a shell on the outside to hide the internal implementation details, and only provides the switch, mouse and keyboard jacks to the outside, so that users can interact with the computer.

The way of C++ to achieve encapsulation: use classes to combine the properties and methods of the object to make the object more complete, and selectively provide its interface to external users through access rights .

The encapsulation of the class provides public (public), default (default default), protected (protected) and private (private) access rights for the members of the class. The purpose is to hide the private variables in the class and the implementation details of the methods in the class.

  • public : Visible to all classes. Objects used: classes, interfaces, variables, methods
  • protected : Visible to classes and all subclasses in the same package. Objects used: variables, methods. Note: Classes (external classes) cannot be modified.
  • default (i.e. default, write nothing): Visible within the same package, without any modifiers. Use objects: classes, interfaces, variables, methods.
  • private : Visible within the same class. Objects used: variables, methods. Note: Classes cannot be modified (external classes)

classThe default access authority is private( because structit is compatible with C language)publicstruct

Access rights are illustrated by the following table:

Implementation:

– Attributes use private permissions
– methods use public permissions 

inherit

It is allowed to generate a brand new class by inheriting some or all of the characteristics of the original class. The original class is called the parent class, and the new class generated is called the subclass. Subclasses can not only directly inherit the commonality of the parent class, but also create its unique personality.

polymorphism

Concept: the same thing, showing different states at different moments. It means that after the attributes and methods defined in the base class are inherited by subclasses, they can have different data types or exhibit different behaviors. Polymorphism has two manifestations: overloading and coverage. The biggest feature of polymorphism is upward modeling .

Three necessary conditions for polymorphism to exist

  1. There must be inheritance (including the implementation of the interface) (precondition)
  2. must be rewritten (precondition)
  3. Parent class references point to subclass objects

Guess you like

Origin blog.csdn.net/MWooooo/article/details/129005065