[C++] 1-1.2 c++ programming paradigm

1. What is a programming paradigm

Programming paradigm: programming paradigm;
different programming methods can be adopted to achieve the same function to form different programming styles, that is, programming paradigm.
The following introduces structured programming, object-oriented programming, generic programming, and functional programming;

2. Structured programming

Structured programming: structural programming;
programs can be regarded as functions and data;
in structured programming, all functions can access data, that is, data is exposed to all functional logic.
Since the data is completely exposed, this will cause some side effects: For
example, the data is incorrectly modified by the function, causing other functions to not get the required data.

3. Object-oriented programming

Object-oriented programming: object-oriented programming is OOP;
because structured programming will cause some side effects, especially when writing large programs, it is especially obvious.
To solve this side effect, object-oriented programming was introduced.

Unlike structured programming, the data and functions of object-oriented programming are encapsulated in a specific environment.
Usually only the function in the specific environment can directly access the data; the external function wants to access the data, it needs to pass the interface encapsulated in this specific environment, so that the data can be accessed by the external function under the premise of being protected.

In general, object-oriented programming makes development and maintenance easier!

4. Generic programming

Generic programming: generic programming is GP;
because in structured programming and object-oriented programming, data types correspond directly to functions.
When the data type is changed, the logic function corresponding to the processing data is changed, so the function needs to be rewritten.

Therefore, generic programming is introduced. Generic programming mainly focuses on data type changes and has done some work. In generic programming, there is no need to care about the specific type of data. When the program encounters a specific type, the compiler will generate a function to process the specific type according to the generic code written by the program.

In generic programming, the data type changes, the compiler will handle it automatically!

This makes the program universal enough and avoids a lot of rewriting!

5. Functional programming

Functional programming: functional programming is FP;
C++ introduces lambda expressions, that is, functional programming.
The function accepts data, processes the data, and then outputs the data. In non-functional programming, function processing may bring side effects; while functional programming has its own independent scope and will not have additional influence on the outside.

A note on side effects:
that is, the function not only returns a value, but also has an impact on the outside, such as modifying external variables, throwing exceptions, and so on.

For functional programming, as long as it receives a fixed input, the function will produce the same output. At the same time, it can accept a function as a parameter and output a function as an output result!

The Haskell language is purely functional programming and has no object-oriented concept.

Guess you like

Origin blog.csdn.net/jn10010537/article/details/115034199