设为首页收藏本站

新微赢技术网

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

怎么就编译不了呢

[复制链接]
跳转到指定楼层
1#
发表于 2009-11-4 01:36:14 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
#include<iostream.h>
#include<math.h>
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=0,float=0);
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 x=0,float y=0,float r=0);
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*pow(radius,2);}
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=0,float y=0,float r=0,float h=0);
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::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;
}
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.5);
Circle circle(2.4,1.2,5.6);
Cylinder cylinder(3.5,6.4,5.2,10.5);
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()<<"\narea="<<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="<<cylinder.getX()<<",y="<<cylinder.getY()<<"\narea="<<pt->area()
<<"\nvolume="<<pt->volume()<<"\n\n";

return 0;
}


错误显示为:
item.cpp: In member function `void Circle::setRadius(float)':
item.cpp:51: error: only constructors take base initializers
2#
发表于 2009-11-4 01:36:15 | 只看该作者
#include<iostream>
#include<math.h>
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=0,float=0);
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 &);
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 x=0,float y=0,float r=0);
void setRadius(float);
float getRadius() const;
virtual float area() const;
virtual void shapeName() const{cout<<"Circle: ";}
friend ostream &operator<<(ostream &,const Circle &);
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*pow(radius,2);}
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=0,float y=0,float r=0,float h=0);
void setHeight(float);
virtual float area() const;
virtual float volume() const;
virtual void shapeName() const{cout<<"Cylinder: ";}
friend ostream &operator<<(ostream &,const Cylinder &);
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;
}
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.5);
Circle circle(2.4,1.2,5.6);
Cylinder cylinder(3.5,6.4,5.2,10.5);
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()<<"\narea="<<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="<<cylinder.getX()<<",y="<<cylinder.getY()<<"\narea="<<pt->area()
<<"\nvolume="<<pt->volume()<<"\n\n";

return 0;
}

你再试下看,你的代码好多错误,再多看看书。
回复 支持 反对

使用道具 举报

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

本版积分规则

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

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

Powered by xuexi

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

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