/*
*All rights reserved.
*文件名称:main.cpp
*作 者:姜 甜 甜
*完成日期:2015年5月19日
*版 本 号:v1.0
*
*问题描述:按以下的提示,由基类的设计和测试开始,逐渐地完成各个类的设计,求出圆格柱体的表面积、体积并输出并且完成要求的计算任务:
(1)先建立一个Point(点)类,包含数据成员x,y(坐标点),实现需要的成员函数,并设计main函数完成测试;
(2)以Point为基类,派生出一个Circle(圆)类,增加数据成员r(半径),以及求面积的成员函数area,实现其他需要的成员函数,设计main函数完成测试;
(3)再以Circle类为直接基类,派生出一个Cylinder(圆柱体)类,再增加数据成员h(高),,以及求圆柱表面积的成员函数area和求圆柱体积的成员函数volume,实现需要的成员函数,并设计main函数完成测试。
要求编写程序,设计出各类中“需要的成员函数”,包括构造函数、析构函数、修改数据成员和获取数据成员的公共接口、用于输出的重载运算符“<<”函数等。
(提示:此任务可以分为三个子任务分成若干步骤进行。先声明基类,再声明派生类,逐级进行,分步调试。——这种方法适用于做任何的项目)
*/
#include<iostream>
#include<Cmath>
using namespace std;
class Point //定义坐标点类
{
public:
Point():x(0),y(0) {};
Point(double x0, double y0):x(x0), y(y0) {};
void PrintPoint();
protected:
double x,y;
};
void Point::PrintPoint()
{
cout<<"Point: ("<<x<<","<<y<<")";
}
class Circle:public Point
{
public:
Circle(double a,double b,double c):Point(a,b),r(c) {}
double area();
double getr();
protected:
double r;
};
double Circle::area()
{
return 3.1415*r*r;
}
double Circle::getr()
{
return r;
}
class Cylinder:public Circle
{
public:
Cylinder(double a,double b,double c,double d):Circle(a,b,c),h(d) {}
double area();
double volume();
double geth();
void show();
protected:
double h;
};
double Cylinder::area()
{
return 2*<span style="color:#cc0000;">Circle::area()+</span>2*3.1415*r*h;
}
double Cylinder::volume()
{
return <span style="color:#cc0000;">Circle::area()*</span>h; //注意使用,相同的函数名
}
double Cylinder::geth()
{
return h;
}
void Cylinder::show()
{
cout<<"圆柱体的中点:";
PrintPoint();
cout<<endl;
cout<<"r:"<<getr()<<endl;
cout<<"h"<<geth()<<endl;
cout<<"S:"<<area()<<endl;
cout<<"V:"<<volume()<<endl;
}
int main()
{
Cylinder t(1,2,3,4);
t.show();
return 0;
}
类族的构造函数,只需写上一级的。