C++题 求更正

#include<iostream>usingnamespacestd;classshape{public:virtualfloatarea()const{return0... #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
{
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;
float 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 void shapename()const{cout <<"circle:";}
friend ostream&operator <<(ostream &,const circle &);
protected:
float radius;
};
circle::circle(float x,float,y,float r):point(x,y),radius(r){}
void circle::setradius(float r):radius(r){}
float circle::getRadius()const{return radius;}
float circle::area()const{return 3.14159*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=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.14159*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 poi(3.2,4.5);
circle cir(2.4,1.2,5.6);
cylinder cyl(3.5,6.4,5.2,10.5);
poi.shapename();
cout <<poi<<endl;

cir.shapename();
cout <<cir<<endl;

cyl.shapename();
cout <<cyl<<endl<<endl;

shape *pt;
pt=&poi;
pt->shapename();
cout <<"x="<<poi.getX()<<",y="<<poi.getY()<<"\narea="<<pt->area()<<"\nvolume="<<pt->volume()<<"\n\n";

pt=○
pt->shapename();
cout <<"x="<<cir.getX()<<",y="<<cir.getY()<<"\narea="<<pt->area()<<"\nvolume="<<pt->volume()<<"\n\n";

pt=&cyl;
pt->shapename();
cout <<"x="<<cyl.getX()<<",y="<<cyl.getY()<<"\narea="<<pt.area()<<"\nvolume="<<pt->volume()<<"\n\n";
return 0;
}

VC6.0的编译系统 如题... 谢了...
展开
 我来答
szdxl
2010-10-03 · TA获得超过773个赞
知道答主
回答量:149
采纳率:0%
帮助的人:179万
展开全部

// try.cpp : 定义控制台应用程序的入口点。

//

#include "stdafx.h"

#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:

//如果不写public:,则默认为private:

 point(float=0.0,float=0.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;

 float 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)

//此时p是对象的引用,而x和y都是保护类型的,所以不能由对象直接访问

{

//output <<"["<<p.x<<","<<p.y<<"]";

 output <<"["<<p.getX()<<","<<p.getY()<<"]";

 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();

 virtual void shapename()const{cout <<"circle:";}

 friend ostream&operator <<(ostream &,const circle &);

protected:

 float radius;

};

circle::circle(float x,float y,float r):point(x,y),radius(r){}

//void circle::setradius(float r):radius(r){}

//构造函数初始值设定项列表只能在构造函数定义中使用

void circle::setradius(float r){ radius = r;}

float circle::getRadius()const{return radius;}

float circle::area(){return 3.14159*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=0,float y=0,float r=0,float h=0);

 void setHeight(float);

 float area();

 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(){return 2*radius*radius*3.14159+2*3.14159*radius*height;}

float cylinder::volume()const 

{// return circle::area()*height;

//只有static函数才能这样用

   return 3.14159*radius*radius*height;

}

ostream&operator<<(ostream &output,const cylinder& cy)

{

 output<<"["<<cy.x<<","<<cy.y<<"],r="<<cy.radius<<",h="<<cy.height;

 return output;

}

int _tmain(int argc, _TCHAR* argv[])

{

 point poi( 3.2, 4.5);

 circle cir(2.4,1.2,5.6);

 cylinder cyl(3.5,6.4,5.2,10.5);

 poi.shapename();

 cout <<poi<<endl;

 cir.shapename();

 cout <<cir<<endl;

 cyl.shapename();

 cout <<cyl<<endl<<endl;

 shape *pt;

 pt=&poi;

 pt->shapename();

 cout <<"x="<<poi.getX()<<",y="<<poi.getY()<<"\narea="<<pt->area()<<"\nvolume="<<pt->volume()<<"\n\n";

 pt=&cir;

 pt->shapename();

 cout <<"x="<<cir.getX()<<",y="<<cir.getY()<<"\narea="<<pt->area()<<"\nvolume="<<pt->volume()<<"\n\n";

 pt=&cyl;

 pt->shapename();

 cout <<"x="<<cyl.getX()<<",y="<<cyl.getY()<<"\narea="<<pt->area()<<"\nvolume="<<pt->volume()<<"\n\n";

 return 0;

}

部分错误给出了注释。。。

推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式