新微赢技术网
标题:
简单问题
[打印本页]
作者:
梦缘o☆无恒
时间:
2009-11-4 00:18
标题:
简单问题
#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;
}
找了半天不知道错在哪啊 我按书上打的啊
大家帮我看看
作者:
べ.べ凡士林
时间:
2009-11-4 00:18
呵呵,你可真喜欢偷懒,以后调试吧。
程序代码:
#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;
}
作者:
惠儿雨点
时间:
2009-11-4 00:18
wfpb:你解没解决也不说,解决了我就不看了
哎,就当你解决了,先不看了,对了,你快毕业了吧
怎么样了
欢迎光临 新微赢技术网 (http://bbs.weiying.cn/)
Powered by Discuz! X3.2