/* (程序头部注释开始)
* 程序的版权和版本声明部分
* Copyright (c) 2011, 烟台大学计算机学院学生
* All rights reserved.
* 文件名称:
* 作 者: 李超
* 完成日期: 2012 年 4 月 25 日
* 版 本 号: 01.10.03
* 对任务及求解方法的描述部分
主函数main.cpp
* 程序的版权和版本声明部分
* Copyright (c) 2011, 烟台大学计算机学院学生
* All rights reserved.
* 文件名称:
* 作 者: 李超
* 完成日期: 2012 年 4 月 25 日
* 版 本 号: 01.10.03
* 对任务及求解方法的描述部分
* 程序输出:
坐标为:(1,2)
圆ci:坐标为:(1,2)半径为:3
改变后的圆ci:坐标为:(4,5)半径为:6
圆柱cl:坐标为:(2,3)半径为:1高为:4
改变后的圆柱cl:坐标为:(4,5)半径为:6高为:7
* 程序头部的注释结束
*感言:该题 好麻烦, 最终还是不知道析构函数怎么弄
*/
头文件LCylinder.h
#include <iostream>
using namespace std;
double P = 3.14159;
class Point
{
public:
Point(double x1 = 0, double y1 = 0): x(x1), y(y1){}//数据初始化
friend ostream & operator << (ostream &, Point &);//重载<<
void Change1(double x1,double y1){x = x1;y = y1;}//改变数据
double GetX(){return x;}//得到x
double GetY(){return y;}//得到y
private:
double x;//横坐标
double y;//纵坐标
};
class Circle:public Point
{
public:
Circle (double x1 = 0, double y1 = 0, double r1 = 1):Point(x1, y1),r(r1){}
double GetR(){return r;}//得到半径
void Change2 (double x1,double y1,double r1){Change1(x1,y1);r = r1;}//改变数据
friend ostream & operator <<(ostream &, Point &);//重载<<
private:
double r;//半径
};
class Cylinder:public Circle
{
public:
Cylinder(double x1 = 0, double y1 = 0,double r1 = 1, double h1 = 1):Circle (x1, y1, r1), h(h1){}
double GetH(){return h;}//得到高
double Area();//计算圆柱面积
double Volume();//计算圆柱体积
void Change3(double x1,double y1,double r1,double h1){Change2(x1,y1,r1);h = h1;}//改变数据
friend ostream & operator << (ostream &, Cylinder & c);//重载<<
private:
double h;//高
};
ostream & operator << (ostream &output, Point & c)
{
output <<"坐标为:" <<'(' <<c.GetX ()<<',' <<c.GetY () <<')';
return output;
}
ostream & operator << (ostream &output, Circle & c)
{
output <<"坐标为:" <<'(' <<c.GetX () <<',' <<c.GetY () <<')' <<"半径为:" <<c.GetR ();
return output;
}
ostream & operator << (ostream &output, Cylinder & c)
{
output <<"坐标为:" <<'(' <<c.GetX () <<',' <<c.GetY () <<')' <<"半径为:" <<c.GetR () <<"高为:" <<c.GetH ();
return output;
}
double Cylinder::Area()
{
return (2 * GetR () * GetR () * P + 2 * P * GetR () * h);
}
double Cylinder::Volume()
{
return (2 * GetR () * GetR () * P * h);
}
主函数main.cpp
#include <iostream>
#include"LCylinder.h"
using namespace std;
void main()
{
Point p1(1,2);
cout <<p1 <<endl;
Circle ci(1,2,3);
cout <<"圆ci:" <<ci <<endl;
ci.Change2 (4,5,6);
cout<<"改变后的圆ci:" <<ci <<endl;
Cylinder cl(2,3,1,4);
cout <<"圆柱cl:";
cout <<cl <<endl;
cl.Change3 (4,5,6,7);
cout <<"改变后的圆柱cl:";
cout <<cl <<endl;
system("pause");
}