/*
*Copyright (c) 2016,烟台大学计算机与控制工程学院
- *All rights reserved.
- *文件名称:point.cpp
- *作 者:郑志金
- *完成日期:2016年5月19日
- *版 本 号:v1.0
- *
- *问题描述:以circle类为基类,派生出一个cylinder类,在增加数据成员h,求圆柱表面积和体积。
- *输入描述:x,y,r,h。
- *输出描述:输出圆柱的表面积和体积。
-
#include<iostream>
using namespace std;
class point
{
protected:
double x;
double y;
public:
point(double x=0,double y=0);
void setPoint(double,double);
void show();double returnx()
{
return x;
}
double returny()
{
return y;
}};
point::point(double a,double b)
{
x=a;
y=b;
}
void point::setPoint(double a,double b)
{
x=a;
y=b;
}
void point::show()
{
cout<<x<<' '<<y;
}class circle :public point
{
protected:
double r;
public:
circle(double a=2,double b=2,double c=2);
void setr(double);
double area();
void show1();};
circle::circle(double a,double b,double c):point(a,b),r(c){}
double circle::area()
{
return 3.14159*r*r;
}
void circle::show1()
{
cout<<"圆的半径:"<<r<<endl<<"圆的面积:"<<area()<<endl;
}
class cylinder:public circle
{
private:
double h;
public:
cylinder(double a,double b,double c,double d);
double area1();
double volume();
void show2();
};
cylinder::cylinder(double a,double b,double c,double d):circle(a,b,c),h(d){}
double cylinder::area1()
{
return area()+area()+2*r*3.14159*h;
}
double cylinder::volume()
{
return area()*h;
}
void cylinder::show2()
{
cout<<"圆柱的表面积为:"<<area1()<<"圆柱的体积为:"<<volume()<<endl;}
运行结果:
int main()
{
double a,b,c,d;
cin>>a>>b>>c>>d;
cylinder A(a,b,c,d);
A.show2();
return 0;
}