/* Copyright (c) 2014, 烟台大学计算机学院
* All rights reserved.
* 文件名称:test.cpp
* 作 者:刘佳琦
* 完成日期:2015年 5 月 18 日
* 版 本 号:v1.0
*
* 问题描述: (1)先建立一个Point(点)类,包含数据成员x,y(坐标点),实现需要的成员函数,并设计main函数完成测试;
(2)以Point为基类,派生出一个Circle(圆)类,增加数据成员r(半径),以及求面积的成员函数area,实现其他需要的成员函数,设计main函数完成测试;
(3)再以Circle类为直接基类,派生出一个Cylinder(圆柱体)类,再增加数据成员h(高),,以及求圆柱表面积的成员函数area和求圆柱体积的成员函数volume,实现需要的成员函数,并设计main函数完成测试。
* 程序输入:圆的半径,圆柱的高
* 程序输出:圆的面积,圆柱的面积和体积
*/
#include<iostream>
using namespace std;
class Point //定义坐标点类
{
public:
Point():x(0),y(0) {};
Point(double x0=0, double y0=0):x(x0), y(y0) {};
double getX()
{
return x;
}
double getY()
{
return y;
}
void PrintPoint(); //输出点的信息
protected:
double x,y; //点的横坐标和纵坐标
};
void Point::PrintPoint()
{
cout<<"Point:("<<x<<","<<y<<")"<<endl;
}
class Circle: public Point
{
public:
Circle(double rr,Point p):Point(p.getX(),p.getY())
{
r=rr;
}
double area();
void display();
double getR()
{
return r;
}
private:
double r;
};
class Cylinder:public Circle
{
public:
Cylinder(Circle c,double h,Point p):Circle(c.getR(),Point(p.getX(),p.getY())){height=h;}
double areac(Circle c);
double volume(Circle c);
void input(Circle c);
private:
double height;
};
double Circle::area( )
{
return 3.14*r*r;
}
double Cylinder::areac(Circle c)
{
return 2*c.area()+2*3.14*c.getR()*height;
}
double Cylinder::volume(Circle c)
{
return c.area()*height;
}
void Circle::display()
{
cout<<"该圆的面积为:"<<area()<<endl;
}
void Cylinder::input(Circle c)
{
cout<<"该圆柱的表面积为:";
cout<<areac(c)<<endl;
cout<<"该圆柱的体积为:";
cout<<volume(c)<<endl;
}
int main()
{
double r1,h;
Point ps(-2,5);
ps.PrintPoint();
cout<<"请输入半径:";
cin>>r1;
Circle c(r1,ps);
c.display();
cout<<"请输入圆柱的高:";
cin>>h;
Cylinder cy(c,h,ps);
cy.input(cy);
return 0;
}
运行结果:
学习心得:
写的太快,细节没注意到,到后面一调试,一堆小问题,然后就一个个的改...改完派生类改基类,因为输出的访问权限问题...后面发现其实可以重载个输出流...