c++计算出圆和圆柱体的表面积和体积 下载本文

内容发布更新时间 : 2024/5/17 11:46:25星期一 下面是文章的全部内容请认真阅读。

1.编写一个程序计算出圆和圆柱体的表面积和体积。 要求:(1)定义一个点(point)类,包含数据成员x,y(坐标点),以它为基类,派生出一个circle类(圆类),增加数据成员r(半径),再以circle作为直接基类,派生出一个cylinder(圆柱体)类,再增加数据成员h(高)。设计类中数据成员的访问属性。

(2)定义基类的派生类圆、圆柱都含有求表面积和体积的成员函数和输出函数。

(3)定义主函数,求圆、圆柱的面积和体积。

#include class point {

private:

float x,y; public: point() { x=0; y=0; }

point(float x1,float y1) {

x=x1; y=y1; } };

class circle:public point {

public:

circle(float x2,float y2,float r1):point(x2,y2) {

r=r1;

}

void ci_area() {

area1=r*r*3.14; }

void ci_output() {

cout<<\圆面积=\ }

float r;

double area1; };

class cylinder:public circle {

private: float h;

double area2,vo; public:

cylinder(float x3,float y3,float r2,float h1):circle(x3,y3,r2) {

h=h1; }

void cy_area() {

area2=(2*3.14*r*h+2*3.14*r*r); }

void cy_volume() {

vo=3.14*r*r*h; }

void cy_outout() {

cout<<\圆柱表面积=\圆柱体积=\ } };

void main() {

circle c1(1.0,1.0,2.0); c1.ci_area(); c1.ci_output();

cylinder c2(1.0,1.0,2.0,4.0); c2.cy_area(); c2.cy_volume(); c2.cy_outout(); }