/* *Copyright(c) 2016, 烟台大学计算机与控制工程学院 *All rights reserved. *文件名称:main.cpp *作 者:张珩瑞 *完成日期:2016年5月3日 *版本号:v1.0 * *问题描述:再以Circle类为直接基类,派生出一个Cylinder(圆柱体)类,再增加数据成员h(高),, 以及求圆柱表面积的成员函数area和求圆柱体积的成员函数volume,实现需要的成员函数,并设计main函数完成测试。 *输入描述:无 *输出描述:按要求输出 */ #include<iostream> #include<cmath> #define PI 3.14 using namespace std; class Point { public: Point(double m,double n); void pointshow(); private: double x; double y; }; Point::Point(double m,double n) { x=m; y=n; } void Point::pointshow() { cout<<"所求的圆心坐标为:"<<endl; cout<<x<<" "<<y<<endl; } class Circle:public Point { public: Circle(double m,double n,int r2); void area(); void circleshow(); int getR(); private: int r; }; Circle::Circle(double m,double n,int r2):Point(m,n) { r=r2; } void Circle::area() { double s; s=PI*r*r; cout<<"圆的面积为:"<<s<<endl; } void Circle::circleshow() { pointshow(); } int Circle::getR() { return r; } class Cylinder:public Circle { public: Cylinder(double x1,double y1,int r1,int h1); void biaoarea(); void volume(); private: int h; }; Cylinder::Cylinder(double x1,double y1,int r1,int h1):Circle (x1,y1,r1) { h=h1; } void Cylinder::biaoarea() { int r1; double s1,s2,s3; r1=getR(); s1=PI*r1*r1; s2=2*PI*r1*h; s3=s1+s2; cout<<"圆柱的表面积为:"<<s3<<endl; } void Cylinder::volume() { int r1; double s1,v; r1=getR(); s1=PI*r1*r1; v=s1*h; cout<<"圆柱的体积为:"<<v<<endl; } int main() { double x1,y1; int r1,h1; cout<<"请输入圆心坐标:"<<endl; cin>>x1>>y1; cout<<"请输入圆的半径:"<<endl; cin>>r1; cout<<"请输入圆柱的高:"<<endl; cin>>h1; Circle c(x1,y1,r1); c.circleshow(); c.area(); Cylinder d(x1,y1,r1,h1); d.biaoarea(); d.volume(); }
时间: 2024-10-03 17:48:42