U72024 C + + initial knowledge class

subject

(Circle) a circular ice and dry site, plastered with cement, with the cost of 20 yuan per square meter. The fence is made of wooden strips, with the cost of 35 yuan per meter. Design a Circle class, you can find out the area and side length of the Circle, users can find out the cost of the dry ice field. Please complete the program on the basis of the following prompts, including: (1) declare the relevant public member functions in the class declaration; (2) define the declared member functions between the class declaration and the main() function. PI = 3.1415926

I / O format

Input format:

Please enter the radius of the rink

Output format:

The cost of the ice rink, 2 decimal places after the decimal point

Example of input and output

Input example ා 1:

3.5

Output example:

1539.38

Answer

Shape.h

#ifndef CPPLEARNING_SHAPE_H
#define CPPLEARNING_SHAPE_H

#include <iostream>
using namespace std;

class Shape {
public:
    Shape();
    virtual ~Shape();
    virtual double calcArea() = 0;
    virtual double calcLen() = 0;
};


#endif //CPPLEARNING_SHAPE_H

Circle.h

#include <iostream>
#include "Shape.h"

using namespace std;

class Circle : public Shape {
public:
    Circle(double radius);
    virtual ~Circle();
    virtual double calcArea();
    virtual double calcLen();

protected:
    double radius;
};

Circle.cpp

#include "Circle.h"

Circle::Circle(double radius) {
    cout << "Circle()" << endl;
    this->radius = radius;
}

Circle::~Circle() {
    cout << "~Circle()" << endl;
}

double Circle::calcArea() {
//    cout << "Circle::calcArea()" << endl;
    return 3.1415926 * radius * radius;
}

double Circle::calcLen() {
//    cout << "Circle::calcLen()" << endl;
    return 3.1415926 * radius * 2;
}

main.cpp

#include <iostream>
#include "Shape.h"
#include "Circle.h"

using namespace std;

int main(int argc, char* args[]) {
    double r;
    cin >> r;
    Shape *circle = new Circle(r);
    cout << 35 * circle->calcArea() << endl;
    return 0;
}

Tags: Programming

Posted on Wed, 06 Nov 2019 08:24:16 -0800 by tony_l