Bài tập C++ OOP: cài đặt lớp Circle theo sơ đồ lớp cho trước | Codelearn

  • TuanLQ7
  • Easy
  • 100 Points

Task 

Create Circle class in Circle.cpp file with the following information:

Explanation:

  • radius is the attribute that represents the radius of the circle.
  • Circle(radius) is the parameterized constructor which is used to initialize values to radius attribute.
  • getArea() and getCircumference() are two methods that return the area and perimeter of a circle with pi (π) = 3.14.

A program to test Circle class:

#include <iostream>
#include "circle.cpp"

using namespace std;

int main() {
    Circle c(7);
    cout << c.getArea() << endl;
    cout << c.getCircumference() << endl;
    return 0;
}

When the above code is compiled and executed, it produces the following result:

153.86
43.96

Instruction

You need to create Circle class as below:

class Circle {
private:
    double radius;
public:
    Circle(double radius) {
        this->radius = radius;
    }
    double getArea() {
        return 3.14 * radius * radius;
    }
    double getCircumference() {
        return 3.14 * 2 * radius;
    }
};