第十周实验报告任务3

/* (程序头部注释开始)
* 程序的版权和版本声明部分
* 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型的,不然子类无法识别。

                    函数声明很重要啊,我偷懒想复制粘贴奇函数中友元函数的声明。结果,后面凌乱了。。。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值