c/c++课的作业合集,都是很简单的文件。
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
cpp-work/test1/20200311-2.cpp

80 lines
1.4 KiB

#include "iostream"
using namespace std;
class Circle {
public:
Circle(int r);
Circle();
const double PI = 3.1415926;
double getLength();
double getArea();
double getRadius();
private:
double radius = 0;
};
double Circle::getLength() {
return radius * 2 * PI;
}
double Circle::getArea() {
return radius * radius * PI;
}
double Circle::getRadius() {
return radius;
}
Circle::Circle(int r) : radius(r) {}
Circle::Circle() : radius(0) {}
int main() {
Circle circle(17);
cout << "Area | Length | Radius" << endl;
cout << circle.getArea() << endl << circle.getLength() << endl << circle.getRadius();
}
// --------------------------------------------------------------
class Circle2 {
public:
const double PI = 3.1415926;
void printArea();
void printLength();
void printRadius();
Circle2(double radius) : radius(radius) {
area = radius * radius * PI;
length = radius * 2 * PI;
}
Circle2(const Circle2 &c2) {
radius = c2.radius;
length = c2.length;
area = c2.area;
}
private:
double radius = 0;
double length = 0;
double area = 0;
};
void Circle2::printArea() {
cout << "Area: " << area << endl;
}
void Circle2::printLength() {
cout << "Length: " << length << endl;
}
void Circle2::printRadius() {
cout << "Radius: " << radius << endl;
}