关于const的用法
double Area() const {return 0.0}//z这了为什么要加const,不加可以么
接上面
class Shape
{
public:
Shape(double x,double y){xCoord=x,yCoord=y;}
virtual double Area() const{return 10.0;}//有什么作用
protected:
double xCoord,yCoord;
};
class Circle:public Shape
{
public:
Circle(double x,double y,double r):Shape(x,y),radius(r){}
double Area() const{return 3.14*radius*radius;}//有什么作用
protected:
double radius;
};
class Rectangle:public Shape
{
public:
Rectangle(double x1,double y1,double x2,double y2):Shape(x1,y1),x2Coord(x2),y2Coord(y2){}
double Area() const{return fabs((xCoord-x2Coord)*(yCoord-y2Coord));} //const有什么作用
protected:
double x2Coord,y2Coord;
};
void fun(Shape &s)
{
cout<<s.Area()<<endl;
}