设为首页收藏本站

新微赢技术网

 找回密码
 注册
搜索
热搜: 回贴
查看: 1622|回复: 2
打印 上一主题 下一主题

望XDJM指教

[复制链接]
跳转到指定楼层
1#
发表于 2009-11-5 00:19:39 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
这是个由Point为基类派生出 class Circle ,class Cylinder输出点的坐标,以该点为圆心的圆的面积,和以该圆为底高为height的圆柱体的表面积和体积;
#include<iostream>
using namespace std;
class Point//define class Point;
{public:
Point(float x=0,float y=0);
void setPoint(float a,float b);
float getX() const{return x;};
float getY() const{return y;};
friend ostream & operator <<(ostream &,const Point &);
protected:
float x,y;
};

//define class Point member function;
Point::Point(float a,float b):x(a),y(b){}
void Point::setPoint(float a,float b):x(a),y(b){}//为什么不能这么写?而只能写成 void Point::setPoint(float a,float b){x=a;y=b;}是不是跟x,y是protected有关?同样还有下面几个函数也一样的问题;
ostream & operator <<(ostream &putout,const Point &p)
{ putout<<"["<<p.x<<","<<p.y<<"]"<<endl;
return putout;
}
class Circle:public Point//define class Circle;
{public:
Circle(float x=0,float y=0,float r=0);
void setRadius(float);
float getRadius() const{return radius;}
float area() const;
friend ostream & operator <<(ostream &,const Circle &);
protected:
float radius;
};

//define class member function;
Circle::Circle(float x,float y,float r):Point(x,y),radius(r){}
void Circle::setRadius(float r)
{radius=r;}//只能这样写么?不能用参数初始化表么? 同上函数:void Point::setPoint(float a,float b);
float Circle::area() const {return 3.14159*radius*radius;}

ostream & operator <<(ostream &putout,Circle &c)
{ putout<<"Center:["<<c.getX()<<","<<c.getY()<<"],r="<<c.getRadius()<<"area="<<c.area()<<endl;
return putout;
}

//define class Cylinder;
class Cylinder:public Circle
{public:
Cylinder(float x=0,float y=0,float r=0,float h=0);
void setHeight(float);
float getHeight() const{return height;}
float area() const{return 2*Circle::area()+2*3.14159*radius*height;}
float volume() const{return Circle::area()*height;}
friend ostream & operator <<(ostream &,Cylinder &);
protected:
float height;
};
//define member function ;
Cylinder::Cylinder(float a,float b,float r,float h):Circle(a,b,r),height(h){}
void Cylinder::setHeight(float h)
{h=height;}//只能这样写么?不能用参数初始化表么? 同上函数:void Point::setPoint(float a,float b);

ostream & operator <<(ostream & putout,Cylinder & cy)
{putout<<"Center=["<<cy.getX()<<","<<cy.getY()<<"],r="<<cy.getRadius()<<",h="<<cy.height<<",area="<<cy.area()<<",volume="<<cy.volume()<<endl;
return putout;
}
int main()
{Cylinder cy1(3.5,6.4,5.2,10);
cout<<"\noriginal cylinder:\nx="<<cy1.getX()<<",y="<<cy1.getY()<<",r="<<cy1.getRadius()<<",h="<<cy1.getHeight()<<",area=";
cout<<cy1.area()<<",volume="<<cy1.volume()<<endl;
cy1.setHeight(15);
cy1.setRadius(7.5);
cy1.setPoint(5,5);
cout<<"\nnew cylinder\n"<<cy1;
Point &pRef=cy1;
cout<<"\npRef as a point:\n"<<pRef;
Circle &cRef=cy1;
cout<<"\ncRef as a circle:\n"<<cRef;

system("pause");
return 0;
}
2#
发表于 2009-11-5 00:19:41 | 只看该作者
难道没人帮帮我么?~~
回复 支持 反对

使用道具 举报

3#
发表于 2009-11-5 00:19:43 | 只看该作者
只有构造函数能用初始化列表
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

申请友链|小黑屋|最新主题|手机版|新微赢技术网 ( 苏ICP备08020429号 )  

GMT+8, 2024-11-19 03:07 , Processed in 0.108376 second(s), 9 queries , Gzip On, Memcache On.

Powered by xuexi

© 2001-2013 HaiAn.Com.Cn Inc. 寰耽

快速回复 返回顶部 返回列表