01./*
02.* 程序的版权和版本声明部分
03.* Copyright (c)2013, 烟台大学计算机学院学生
04.* All rightsreserved.
05.* 文件名称: point.cpp
06.* 作 者:赵冠哲
07* 完成日期:2013年5月9日
08.* 版本号: v1.0
09.* 输入描述:
10.* 问题描述:
11.*/
#include<iostream.h>
#include<Cmath>
#define PI 3.1415926//定义符号常量
class Point //定义坐标点类
{
private:
double x;//横坐标
double y;//纵坐标
public:
Point(double x0,double y0) {x=x0; y=y0;}
~Point ()
{cout<<"Destructor called"<<endl;}
double get_x(){return x;} //公用基类的私有数据成员在派生类中不能访问,需要获取私有数据成员的公共接口
double get_y(){return y;}
friend ostream &operator << (ostream &, Point &);//声明运算符"<<"的重载
};
//利用坐标点类定义圆类, 其基类的数据成员表示圆的中心
class Circle: public Point
{
private:
double d;
public:
Circle(double xx,double yy,double dd): Point(xx,yy)
{
d=dd;
}
~Circle()
{
}
friend ostream &operator << (ostream &, Circle &);
double get_d(){return d;}
};
class Cylinder: public Circle
{
private:
double h;
public:
Cylinder(double xx,double yy,double dd,double hh): Circle (xx,yy,dd)
{
h=hh;
}
~Cylinder()
{
}
friend ostream &operator << (ostream &,Cylinder &);
double get_h(){return h;}
double superficial_area();//表面积
double volume();//圆柱体的体积
};
ostream &operator << (ostream & output, Point & c)
{
output<<"点的横坐标为:"<<c.x<<endl;
output<<"点的纵坐标为:"<<c.y<<endl;
return output;
}
ostream &operator << (ostream & output, Circle & c)
{
output<<"圆的半径为:"<<c.get_d()<<endl;
output<<"圆的圆心为"<<"("<<c.get_x()<<","<<c.get_y()<<")"<<endl;
return output;
}
ostream &operator << (ostream & output,Cylinder & c)
{
output<<"圆柱的高为:"<<c.get_h()<<endl;
output<<"圆柱底面圆的半径为:"<<c.get_d()<<endl;
output<<"圆柱底面圆的圆心为"<<"("<<c.get_x()<<","<<c.get_y()<<")"<<endl;
return output;
}
//圆柱体的表面积
double Cylinder::superficial_area()
{
double s=2*PI*get_d()*get_d()+2*PI*get_d()*get_h();
return s;
}
//圆柱体的体积
double Cylinder::volume()
{
double v=PI*get_d()*get_d()*get_h();
return v;
}
int main()
{
Point p(1,1);
cout<<p;
Circle ci(1,2,3);
cout<<ci;
Cylinder cy(1,2,4,6);
cout<<cy;
cout<<"圆柱的体积为:"<<cy.volume ()<<endl;
cout<<"圆柱的表面积为:"<<cy.superficial_area ()<<endl;
return 0;
}
运行结果: