第十一周(项目四)——类族的设计。

/*    
02.*烟台大学计算机学院学生    
03.*All right reserved.    
04.*文件名称*烟台大学计算机学院学生    
05.*All right reserved.    
06.*文件名称类族的设计
07.*作者:王洪海    
08.*完成日期:2013年5月13日    
09.*版本号:v1.0    
10.*对任务及求解方法的描述部分:类族的设计 
11.*我的程序:    
12.*/   
#include <iostream>

using namespace std;

class point
{
public:
  point (double x0,double y0):x(x0),y(y0){}
  ~point(){}
  void setpoint(double ,double);
  friend ostream &operator <<(ostream &,point &);
protected:
  double x;
  double y;
};
void point::setpoint(double x0,double y0)
{
    x=x0;
    y=y0;
}
ostream &operator <<(ostream &output,point &p)
{
    output<<"("<<p.x<<","<<p.y<<")"<<endl;
    return output;
}

class circle:public point
{
 public:
   circle(double x0,double y0,double r0):
     point(x0,y0),r(r0){}
   ~circle(){}
   void setr(double);
   double area();
   friend ostream &operator <<(ostream &,circle &);
 protected:
   double r;
};
void circle::setr(double r0)
{
    r=r0;
}
double circle::area()
{
    double a;
    a=3.14*r*r;
    return a;
}
ostream &operator <<(ostream &output,circle &c)
{
    output<<"heart=("<<c.x<<", "<<c.y<<"), r="<<c.r<<", area="<<c.area( )<<endl;
    return output;
}

class cylinder:public circle
{
public:
  cylinder(double x0,double y0,double r0,double h0):
    circle(x0,y0,r0),h(h0){}
  ~cylinder(){}
  void seth(double );
  double area();
  double volume();
  friend ostream &operator <<(ostream &,cylinder &);
protected:
  double h;
};
void cylinder::seth(double h0)
{
    h=h0;
}
double cylinder::volume()
{
    double s,t;
    s=3.14*r*r;
    t=s*h;
    return t;
}
double cylinder::area()
{
    double a;
    a=3.14*r*r*2+2*3.14*r*h;
    return a;
}
ostream &operator <<(ostream &output,cylinder &c)
{
    output<<"herat=("<<c.x<<","<<c.y<<")  r="<<c.r<<endl;
    output<<"表面积为:"<<c.area()<<"   体积为:"<<c.volume()<<endl;
    return output;
}
int main()
{
    point p1(2,3);
    circle c(2,3,4);
    cylinder z(2,3,4,5);
    cout<<"原点坐标为:";
    cout<<p1;
    cout<<endl;
    cout<<"此圆的圆心,半径,面积为:"<<endl;
    cout<<c;
    cout<<endl;
    cout<<"此圆柱体的底面圆心,底面半径,圆柱体体积为:"<<endl;
    cout<<z;
}

运行结果,如下图:


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值