| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 504 人关注过本帖
标题:简单问题
取消只看楼主 加入收藏
心动音符
Rank: 1
等 级:禁止访问
威 望:1
帖 子:832
专家分:0
注 册:2005-9-15
收藏
 问题点数:0 回复次数:0 
简单问题

#include <iostream>
using namespace std;
class shape
{public:
virtual float area() const {return 0.0;}
virtual float volume() const {return 0.0;}
virtual void shapename() const =0;
};

class point:public shape
{public:
point(float,float);
void setpoint(float,float);
float getX()const {return x;}
float getY()const {return y;}
virtual void shapename() const{cout<<"point:";}
friend ostream & operator << (ostream &,const point &);
protected:
float x,y;
};

point::point(float a,float b)
{x=a;y=b;}

void point::setpoint(float a,float b)
{x=a;y=b;}

ostream&operator<<(ostream &output,const point &p)
{
output<<"["<<p.x<<","<<p.y<<"]";
return output;
}
class circle:public point
{public:
circle(float,float,float);
void setRadius(float);
float getradius()const;
virtual float area() const;
virtual void shapename()const {cout<<"circle:";}
friend ostream&operator<<(ostream &,const circle &);
protected:
float radius;
};
circle::circle(float a,float b,float r):point(a,b),radius(r){}
void circle::setRadius(float r){radius=r;}
float circle::getRadius()const{return radius;}
float circle::area()const {return 3.14*radius*radius;}

ostream&operator<<(ostream &output,const circle &c)
{output<<"["<<c.x<<","<<c.y<<"],r="<<c.radius;
return output;
}
class cylinder:public circle
{public :
cylinder(float x,float y,float r,float h);
void setheight(float);
virtual float area()const;
virtual float volume()const;;
virtual void shapename()const{cout<<"cylinder:";}
friend ostream&operator<<(ostream &,const cylinder&);
protected:
float height;
};
cylinder::cylinder(float a,float b,float r,float h):circle(a,b,r),height(h){}
void cylinder::getheight(flaot h){height=h;}
float cylinder::area()const
{return 2*circle::area()+2*3.14*radius*height;}
float cylinder::volume()const
{return circle::area()*height;}
ostream&operator<<(ostream &output,const cylinder &cy)
{output<<"["<<cy.x<<","<<cy.y<<"],r="<<cy.radius<<",h="<<cy.height;
return output;
}
int main()
{point point(3.2,4.3);
circle circle(2.4,1.2,5.6);
cylinder cylinder(3.2,4.4,5.4,4.6);
point.shapename();
cout<<point<<endl;
circle.shapename();
cout<<circle<<endl;
cylinder.shapename();
cout<<cylinder<<endl<<endl;
shape *pt;
pt=&point;
pt->shapename();
cout<<"x="<<point.getX()<<",y="<<point.getY()<<"\area="<<pt->area()
<<"\nvolume="<<pt->volume()<<"\n\n";
pt=&circle;
pt->shapename();
cout<<"x="<<circle.getX()<<",y="<<circle.getY()<<"\narea="<<pt->area()
<<"\nvolume="<<pt->volume()<<"\n\n";
pt=&cylinder;
pt->shapename();
cout<<"x="<<circle.getX()<<",y="<<circle.getY()<<"\narea="<<pt->area()
<<"\nvolume="<<pt->volume()<<"\n\n";
return 0;
}
找了半天不知道错在哪啊 我按书上打的啊
大家帮我看看

搜索更多相关主题的帖子: 简单问题 
2006-10-01 19:51
快速回复:简单问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.016683 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved