您的位置:首页 > 编程语言 > PHP开发

第十周实验报告3

2012-04-23 18:20 302 查看
/* (程序头部注释开始)

* 程序的版权和版本声明部分

* Copyright (c) 2011, 烟台大学计算机学院学生

* All rights reserved.

* 文件名称:                             

* 作    者:         赵桐辉                    

* 完成日期:         2012年4  月23    日

* 版 本 号:         
* 对任务及求解方法的描述部分

* 输入描述:

* 问题描述:

(1)先建立一个Point(点)类,包含数据成员x,y(坐标点);

(2)以Point为基类,派生出一个Circle(圆)类,增加数据成员 (半径);

(3)再以Circle类为直接基类,派生出一个Cylinder(圆柱体)类,再增加数据成员h(高)。

要求编写程序,设计出各类中基本的成员函数(包括构造函数、析构函数、修改数据成员和获取数据成员的公共接口、用于输出的重载运算符“<<”函数等),使之能用于处理以上类对象,最后求出圆格柱体的表面积、体积并输出。

(提示:此任务可以分为三个子任务分成若干步骤进行。先声明基类,再声明派生类,逐级进行,分步调试。——这种方法适用于做任何的项目)

(1)第1个程序: 基类Point类及用于测试的main()函数

 

(2)第2个程序:声明Point类的派生类Circle及其测试的main()函数

 

(3)第3个程序:声明Circle的派生类Cylinder及测试的main()函数

* 程序输出:

* 程序头部的注释结束

*/

#include<iostream>
#include<Cmath>
using namespace std;

#define pi 3.1415926
class Point //定义坐标点类
{
public:
double x,y;   //点的横坐标和纵坐标
Point(){x=0;y=0;}
Point(double x0,double y0) {x=x0; y=y0;}
~Point ()
{}
double get_x(){return x;}
double get_y(){return y;}
friend ostream &operator << (ostream & output, Point & c);
};

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 & output, Circle & c);
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 & output,Cylinder & c);
double get_h(){return h;}
double superficial_area();     //表面积
double volume();    //体积
};
ostream &operator << (ostream & output, Point & c)
{
output<<"点的横坐标为:"<<c.x<<"     "<<"点的纵坐标为:"<<c.y<<endl;
return output;
}
ostream &operator << (ostream & output, Circle & c)
{
output<<"圆的半径为:"<<c.get_d()<<"圆的圆心为"<<"("<<c.get_x()<<","<<c.get_y()<<")"<<endl;
return output;
}
ostream &operator << (ostream & output,Cylinder & c)
{
output<<"圆的高为:"<<c.get_h()<<"圆的半径为:"<<c.get_d()<<"圆的圆心为"<<"("<<c.get_x()<<","<<c.get_y()<<")"<<endl;
return output;
}
double  Cylinder::superficial_area()     //表面积
{
double s;
s=2*pi*get_d()*get_d()+2*pi*get_d()*get_h();
return s;
}
double  Cylinder::volume()   //体积
{
double v;
v=pi*get_d()*get_d()*get_h();
return v;
}
int main()
{
Point p(4,2);
cout<<p;
Circle ci(4,3,6);
cout<<ci;
Cylinder cy(4,1,5,2);
cout<<cy;
cout<<"圆柱的体积为:"<<cy.volume ()<<endl;
cout<<"圆柱的表面积为:"<<cy.superficial_area ()<<endl;
return 0;

}




上机感言:分步完成挺好的。。。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  output class 任务 测试 c