第十周实验报告3

01.实验内容:实现Time类中的运算符重载  
02.* 程序的版权和版本声明部分   
03.* Copyright (c) 2011, 烟台大学计算机学院学生   
04.* All rights reserved.   
05.* 文件名称:                         
06.* 作    者: 刘文英                              
07.* 完成日期:  2012       年  4  月  23日   
08.* 版 本号:  vc2008           
09.*/  
10.源程序:  

#include<iostream>  
#include<Cmath>    
#define pi 3.1415926
using namespace std;

class Point //定义坐标点类  
{  
public:  
    double x,y; //点的横坐标和纵坐标  
    Point(){x=0;y=0;}  
    Point(double x0,double y0) {x=x0; y=y0;}   
    ~Point ()  
    {  
        cout<<"析构函数执行完毕(Destructor function performs finished)"<<endl;  
    }  
    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(1,1);  
    cout<<p;  
    Circle ci(1,2,6);  
    cout<<ci;  
    Cylinder cy(1,2,3,4);  
    cout<<cy;  
    cout<<"圆柱的体积为:"<<cy.volume ()<<endl;  
    cout<<"圆柱的表面积为:"<<cy.superficial_area ()<<endl; 
    system("pause"); 
    return 0;           
}  

运行结果:

点的横坐标为:1     点的纵坐标为:1
圆的半径为:6圆的圆心为(1,2)
圆的高为:4圆的半径为:3圆的圆心为(1,2)
圆柱的体积为:113.097
圆柱的表面积为:131.947
请按任意键继续. . .

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值