Read a book, found that C + + and C, although on the hair similar, but the way to solve the problem is not the same, after all, object-oriented and process-oriented are two different ways of thinking. The following is a comparison of C and C + + by an example of the area to be rounded.
Requirements: Enter the radius of the circle to solve the area of the circle
Use the C language to solve: 1. Define two variable radius r, area s;
2. Enter the radius;
3. Print the results.
Here is the source code:
#include <stdio.h>intMain01 () {DoubleR, S;//define the variable circle and radiusprintf ("Please enter the radius of the circle:"); scanf_s ("%LF", &R); printf ("the radius of the circle is R =%f\n", R); S=3.14* R *R; printf ("the area of the circle is s =%f\n", s); return 0;}
Use C + + to solve the problem: 1. Abstract a circle Class (attributes and methods);
2. Instantiate an object;
3. Call the object's member function to calculate the area of the circle
C + + code
#include <iostream>using namespacestd;classCircle//Abstract a Circle class{ Public: DoubleM_r;//radius Doublem_s;//Area Public: voidSETR (Doubler) {M_r=R; } DoubleGetS () {m_s=3.14* M_r *M_r; returnm_s; }};intMainvoid) {Circle C1; C1.SETR (2.0); cout<<"the area of C1 is"<< c1.gets () <<Endl; return 0;}
While solving the same problem, it is clear that this is a different way of thinking
First day of introduction to C + + (area for rounding)