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

第十周任务三:派生类的派生

2012-04-24 17:02 99 查看
* (程序头部注释开始)

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

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

 * All rights reserved.

 * 文件名称:

 * 作者:吴瑕

 * 完成日期: 2012年 04月 24日

 * 版本号:

 *对任务及求解方法的描述部分

 * 输入描述:(1)先建立一个Point(点)类,包含数据成员x,y(坐标点);

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

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

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

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

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

#include <iostream>
#include <string.h>
#define PI 3.14
using namespace std;
class Point
{
protected:
double x,y;
public:
Point(double x0,double y0){x=x0;y=y0;}
~ Point(){}
double Getx(){return x;}
double Gety(){return y;}

};

class Circle:public Point
{
protected:
double r;

public:
Circle(double x0,double y0,double r1):Point(x0,y0){r=r1;}
~Circle(){}
double Getr(){return r;}
};
class Cylinder:public Circle
{
private:
double h;

public:
Cylinder(double x0,double y0,double r1,double height):Circle(x0,y0,r1){h=height;}
~ Cylinder(){}
double Area();
double V();
friend ostream&operator << (ostream&,Cylinder&);

};

double Cylinder::Area()
{
double Area,s1,s2;
s1=PI*Getr()*Getr();
s2=2*PI*h*Getr();
Area=2*s1+s2;
return Area;

}

double Cylinder:: V()
{
double v;
v=PI*Getr()*Getr()*h;
return v;

}
ostream&operator<<(ostream&output,Cylinder&C)
{
output<<"圆心坐标为:"<<"("<<C.Getx()<<","<<C.Gety()<<")"<<"半径为:"<<C.Getr()<<"高为:"<<C.h;
return output;
}

int main()
{
Cylinder C1(5,4,2,1);

cout<<"此圆柱体的信息为:"<<endl<<C1<<endl;

cout<<"此圆柱体的表面积为:"<<C1.Area()<<endl;

cout<<"此圆柱体的体积为:"<<C1.V()<<endl;

system("pause");

return 0;

}


运行结果:

此圆柱体的信息为:

圆心坐标为:(5,4)半径为:2高为:1

此圆柱体的表面积为:37.68

此圆柱体的体积为:12.56

请按任意键继续. . .

上机感言:

编这个感觉还好,就是要注意细节!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  任务 output class 测试 c
相关文章推荐