设为首页收藏本站

新微赢技术网

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

简单问题

[复制链接]
跳转到指定楼层
1#
发表于 2009-11-4 00:18:07 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
#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;
}
找了半天不知道错在哪啊 我按书上打的啊
大家帮我看看
2#
发表于 2009-11-4 00:18:08 | 只看该作者
呵呵,你可真喜欢偷懒,以后调试吧。


程序代码:

#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 &output,const point &p)
{
output<<"["<<p.x<<","<<p.y<<"]";
return output;
}
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;
}

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 &output,const circle &c)
{
output<<"["<<c.x<<","<<c.y<<"],r="<<c.radius;
return output;
}
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;
}

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 &output,const cylinder &cy)
{
output<<"["<<cy.x<<","<<cy.y<<"],r="<<cy.radius<<",h="<<cy.height;
return output;
}
protected:
float height;
};
cylinder::cylinder(float a,float b,float r,float h)
: circle(a,b,r)
, height(h)
{

}
void cylinder::setheight(float h)
{
height=h;
}
float cylinder::area()const
{
return 2*circle::area()+2*3.14*radius*height;
}
float cylinder::volume()const
{
return circle::area()*height;
}
int main()
{
point point(3.2f,4.3f);
circle circle(2.4f,1.2f,5.6f);
cylinder cylinder(3.2f,4.4f,5.4f,4.6f);
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;
}
回复 支持 反对

使用道具 举报

3#
发表于 2009-11-4 00:18:10 | 只看该作者
wfpb:你解没解决也不说,解决了我就不看了
哎,就当你解决了,先不看了,对了,你快毕业了吧
怎么样了
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-11-19 04:26 , Processed in 0.110728 second(s), 9 queries , Gzip On, Memcache On.

Powered by xuexi

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

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