第十周实验报告任务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型的,不然子类无法识别。
函数声明很重要啊,我偷懒想复制粘贴奇函数中友元函数的声明。结果,后面凌乱了。。。
相关文章推荐
- 第十周实验报告 任务一
- 第十周实验报告 任务三(改)
- C++程序设计实验报告(六十二)---第十周任务1
- 第十周实验报告 任务二
- 第十周实验报告任务1
- 《第十周实验报告任务3》
- 第十周实验报告 任务三
- 第十周实验报告(任务1)
- 第十周实验报告(任务二)【在CPerson类的基础上派生出CEmployee类】
- 第十周实验报告任务1 由坐标点类派生出直线类
- 第十周实验报告(任务3)
- 《第十周实验报告任务1》
- 第十周实验报告(任务2)
- C++程序设计实验报告(六十三)---第十周任务2
- 第十周实验报告任务3 先建一个Point类
- 《第十周实验报告任务2》
- 第十周实验报告任务2 定义一个名为CPerson的类。
- C++程序设计实验报告(六十四)---第十周任务3
- 《第六周实验报告任务2》
- 第六周实验报告 任务二