找回密码
 注册
搜索
热搜: 回贴
  • 前程无忧官网首页 有什么好的平台可以
  • 最新的销售平台 互联网营销的平台有哪
  • 制作网页的基本流程 网页制作和网页设
  • 【帝国CMS】输出带序号的列表(数字排
  • 网站建设公司 三一,中联,极东泵车的
  • 织梦 建站 织梦网站模版后台怎么更改
  • 云服务官网 哪些网站有免费的简历模板
  • 如何建网站要什么条件 建网站要用什么
  • 吉林市移动公司电话 吉林省退休人员网
  • 设计类毕业论文 网站设计与实现毕业论
查看: 1627|回复: 2

望XDJM指教

[复制链接]
发表于 2009-11-5 00:19:39 | 显示全部楼层 |阅读模式 IP:江苏扬州
这是个由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;
}
发表于 2009-11-5 00:19:41 | 显示全部楼层 IP:江苏扬州
难道没人帮帮我么?~~
回复

使用道具 举报

发表于 2009-11-5 00:19:43 | 显示全部楼层 IP:江苏扬州
只有构造函数能用初始化列表
回复

使用道具 举报

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

本版积分规则

QQ|小黑屋|最新主题|手机版|微赢网络技术论坛 ( 苏ICP备08020429号 )

GMT+8, 2024-10-1 07:41 , Processed in 0.187636 second(s), 13 queries , Gzip On, MemCache On.

Powered by Discuz! X3.5

© 2001-2023 Discuz! Team.

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