C++ Note 005: Solving the Area of a Circle with Procedural and Object-Oriented Approaches

After finishing the first hello world program, let's solve the problem of the area of ​​a circle with two methods: process-oriented and object-oriented, so that we can understand object-oriented and process-oriented more clearly.

First, the process-oriented calculation of the area of ​​the circle

In the program, we see a standard input stream cin. We have touched the standard output cout before. Let's talk about these two things! Just for understanding!

What does cout do? cout is the standard output stream, which means that so-and-so is output to the screen, cout<< so-and-so, the left shift operator has been overloaded here, which means different meanings, such as the stream of water, this << The direction of the arrow is the direction of the water flow, that is to say, if so-and-so flows to cout, it also flows to the screen, that is, it is displayed on the screen. For the time being, cout can be understood as a screen.

what is cin? cin is the standard input stream, cin >> XX, the >> right shift operator is also overloaded here, and it is not what it meant before. The arrow indicates the direction of the flow, that is, the cin thing flows to XX In a certain, it can be understood that cin is a keyboard, that is, the content of the keyboard input flows to such and such!

Process-oriented programs are relatively simple, as long as there are some basics in C language, plus input and output streams for C++!

Second, the object-oriented calculation of the area of ​​the circle

Object-oriented method to find the area of ​​a circle, first of all to do an abstraction, what is abstraction?

Object-oriented is to abstract people or things in real life. For example, I now ask for the area of ​​a circle. First, I need to define a custom data type such as "circle", which is similar to the structure in C language. The body is similar. In Note 4, the class in C++ has been introduced with the help of the structure.

I define a "circle" class whose properties (member variables) are the radius and area, and its behavior (member functions) can include setting the radius, calculating the area of ​​the circle, and so on.

In general , it is divided into several steps

The first step, class abstraction (member variables and member functions, define a class)

The second step, the instantiation of the class (define variables with classes, that is, objects)

The third step is to find the area

Summary :

Process-oriented processing is a function, object-oriented processing is a class (constantly modify the class).

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325076372&siteId=291194637