First day of introduction to C + + (area for rounding)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.