#include<iostream.h>
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 =0,float =0);
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.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() 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.14159*radius*radius;}
ostream &operator<<(ostream &output,Circle &c)
{output<<"["<<c.getX()<<","<<c.getY()<<"],r="<<c.getradius();
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 &,Cylinder &);
protected:
float heigth;
};
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()
{return Circle::area()*height;
}
ostream &operator<<(ostream &output,Cylinder &cy)
{output<<"["<<cy.getX()<<","<<cy.getY()<<"],r="<<cy.getradius()<<",h="<<cy.getheight();
return output;
}
int main()
{Point point(3.2,4.5);
Circle circle(2.4,1.2,5.6);
Cylinder(3.5,6.4,5.2,10.5);
point.shapename();
cout<<point<<endl;
circle.shapename();
cout<<circle<<endl;
cylinder.shapename();
cout<<cylinder<<endl;
Shape *p;
p->shapename();
cout<<"x="<<point.getX()<<"y="<<point.getY()<<"\narea="<<p->area()<<"\nvolume="<<p->volume()<<"\n\n";
p=&circle;
p->shapename();
cout<<"x="<<circle.getX()<<"y="<<circle.getY()<<"\nnarea="<<p->area()<<"\nvolume="<<p->volume()<<"\n\n";
p=&cylinder;
cout<<"x="<<cylinder.getX()<<"y="<<cylinder.getY()<<"\narea"<<p->area()<<"\nvolume="<<p->volume()<<"\n\n";
return 0;
}
C++6.0中文版,编译通不过。
:\我的文档\桌面\xx.cpp(44) : warning C4244: 'return' : conversion from 'double' to 'float', possible loss of data
d:\我的文档\桌面\xx.cpp(60) : error C2065: 'height' : undeclared identifier
d:\我的文档\桌面\xx.cpp(60) : warning C4244: '=' : conversion from 'float' to 'int', possible loss of data
d:\我的文档\桌面\xx.cpp(62) : warning C4244: '=' : conversion from 'float' to 'int', possible loss of data
d:\我的文档\桌面\xx.cpp(64) : warning C4244: 'return' : conversion from 'double' to 'float', possible loss of data
d:\我的文档\桌面\xx.cpp(66) : error C2511: 'volume' : overloaded member function 'float (void)' not found in 'Cylinder'
d:\我的文档\桌面\xx.cpp(49) : see declaration of 'Cylinder'
d:\我的文档\桌面\xx.cpp(69) : error C2039: 'getheight' : is not a member of 'Cylinder'
d:\我的文档\桌面\xx.cpp(49) : see declaration of 'Cylinder'
d:\我的文档\桌面\xx.cpp(73) : warning C4305: 'argument' : truncation from 'const double' to 'float'
d:\我的文档\桌面\xx.cpp(74) : warning C4305: 'argument' : truncation from 'const double' to 'float'
d:\我的文档\桌面\xx.cpp(74) : warning C4305: 'argument' : truncation from 'const double' to 'float'
d:\我的文档\桌面\xx.cpp(74) : warning C4305: 'argument' : truncation from 'const double' to 'float'
d:\我的文档\桌面\xx.cpp(75) : warning C4305: 'argument' : truncation from 'const double' to 'float'
d:\我的文档\桌面\xx.cpp(75) : warning C4305: 'argument' : truncation from 'const double' to 'float'
d:\我的文档\桌面\xx.cpp(80) : error C2065: 'cylinder' : undeclared identifier
d:\我的文档\桌面\xx.cpp(80) : error C2228: left of '.shapename' must have class/struct/union type
d:\我的文档\桌面\xx.cpp(88) : error C2440: '=' : cannot convert from 'int *' to 'class Shape *'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
d:\我的文档\桌面\xx.cpp(89) : error C2228: left of '.getX' must have class/struct/union type
d:\我的文档\桌面\xx.cpp(89) : error C2228: left of '.getY' must have class/struct/union type
执行 cl.exe 时出错.
xx.obj - 1 error(s), 0 warning(s)
高手指教。谢谢。我都对C++开始灰心丧气了。