C++题 求更正
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的编译系统 如题... 谢了... 展开
// 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=○
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;
}
部分错误给出了注释。。。