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

第十周实验报告任务3

2012-04-24 16:27 351 查看
/* (程序头部注释开始)
* 程序的版权和版本声明部分
* Copyright (c) 2011, 烟台大学计算机学院学生
* All rights reserved.
* 文件名称:
* 作    者:王引琳
* 完成日期:   2012      年   4   月    24日
* 版 本 号:

* 对任务及求解方法的描述部分
* 输入描述:
* 问题描述:
* 程序输出:
* 程序头部的注释结束
*/
#include <iostream>
#include<Cmath>
#define PI 3.1415926
using namespace std;
class Point
{
public:
Point(){x=0;y=0;}
Point(double x0,double y0) {x=x0;y=y0;}
~Point(){};
double getX(){return x;}
double getY(){return y;}
friend ostream &operator<<(ostream& output,Point &c);
protected:
double x,y;
};
ostream &operator<<(ostream& output,Point &c)
{
output<<"Point:("<<c.x <<","<<c.y<<")";
return output;
}

class Circle:public Point
{
public:
Circle(){r=0;}
Circle(double x0,double y0,double r0):Point(x0,y0){r=r0;}
~Circle(){};
double getR(){return r;}
friend ostream &operator<<(ostream& output,Circle &c);
double perimeter();
double area();
protected:
double r;
};
ostream &operator<<(ostream& output,Circle  &c)
{
output<<"圆心为:"<<"("<<c.getX()<<","<<c.getY()<<"),"<<"半径为:"<<c.r;
return output;
}
double Circle::perimeter()
{
return 2*PI*r;
}

double Circle::area()
{
return PI*r*r;
}

class Cylinder : public Circle
{
public:
Cylinder(){h=0;}
Cylinder(double x0,double y0,double r0,double h0):Circle(x0,y0,r0){h=h0;}
~Cylinder(){};

friend ostream &operator<<(ostream& output,Cylinder &c);
double areab();
double volume();
protected:
double h;
};
ostream &operator<<(ostream& output,Cylinder &c)
{
output<<"圆心为:"<<"("<<c.getX()<<","<<c.getY()<<"),"<<"半径为:"<<c.getR()<<"高为:"<<c.h;
return output;
}
double Cylinder::areab()
{
return area()*2+perimeter()*h;
}
double Cylinder:: volume()
{
return area() * h;
}

void main()
{
Cylinder cy(1, 2, 3, 4);
cout<<cy<<endl;
cout<<"表面积为:"<<cy.areab()<<endl;
cout<<"体积为:"<<cy.volume()<<endl;
system("pause");
}
运行结果:
圆心为:(1,2),半径为:3高为:4

表面积为:131.947

体积为:113.097

请按任意键继续. . .

 上机感言:多层派生时的构造函数,成员函数都要定义成protected型的,不然子类无法识别。

                    函数声明很重要啊,我偷懒想复制粘贴奇函数中友元函数的声明。结果,后面凌乱了。。。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  任务 output class c