C++ 定义1个点类Point,一个圆类Circle,一个圆柱体类Cylinder,点类派生圆类,圆类派生圆
定义1个点类Point,一个圆类Circle,一个圆柱体类Cylinder,点类派生圆类,圆类派生圆柱体类。点类中含有x、y坐标,构造函数,显示点坐标的函数show;圆类...
定义1个点类Point,一个圆类Circle,一个圆柱体类Cylinder,点类派生圆类,圆类派生圆柱体类。点类中含有x、y坐标,构造函数,显示点坐标的函数show;圆类中含有构造函数、求圆面积函数area和周长的函数perimeter以及显示圆心坐标、圆的半径、圆的面积和周长的show函数;圆柱体类含有构造函数、求圆柱体表面积函数area和体积的函数volume以及显示表面积和体积的函数show。注:求Cylinder类表面积和体积函数要使用Circle类的相应函数。
展开
展开全部
#include <iostream>
using std::cout;
using std::endl;
#define PI 3.14159
struct point {
int x, y;
point(int _x, int _y) : x(_x), y(_y) {}
virtual void show() const { cout << "(" << x << ", " << y << ")" << endl; }
};
struct circle : public point {
int radius;
circle(int _x, int _y, int r) : point(_x, _y), radius(r) {}
double area() const { return PI * radius * radius; }
double perimeter() const { return 2 * PI * radius; }
void show() const {
point::show();
cout << "radius: " << radius << " area: " <<禅漏禅贺尘 area() << " perimeter: " << perimeter() << endl;
}
};
struct cylinder : public circle {
int height;
cylinder(int _x, int _y, int r, int h) : circle(_x, _y, r), height(h) {}
double area() const {
return circle::area() * 2 + perimeter() * height;
}
double volume() const { return circle::area() * height; }
void show() const {
circle::show();
cout << "area: " << area() << "搜绝 volume: " << volume() << endl;
}
};
int main()
{
cylinder c(1, 2, 3, 4);
c.show();
return 0;
}
using std::cout;
using std::endl;
#define PI 3.14159
struct point {
int x, y;
point(int _x, int _y) : x(_x), y(_y) {}
virtual void show() const { cout << "(" << x << ", " << y << ")" << endl; }
};
struct circle : public point {
int radius;
circle(int _x, int _y, int r) : point(_x, _y), radius(r) {}
double area() const { return PI * radius * radius; }
double perimeter() const { return 2 * PI * radius; }
void show() const {
point::show();
cout << "radius: " << radius << " area: " <<禅漏禅贺尘 area() << " perimeter: " << perimeter() << endl;
}
};
struct cylinder : public circle {
int height;
cylinder(int _x, int _y, int r, int h) : circle(_x, _y, r), height(h) {}
double area() const {
return circle::area() * 2 + perimeter() * height;
}
double volume() const { return circle::area() * height; }
void show() const {
circle::show();
cout << "area: " << area() << "搜绝 volume: " << volume() << endl;
}
};
int main()
{
cylinder c(1, 2, 3, 4);
c.show();
return 0;
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询