纯虚函数与抽象类


#include <iostream>
#include <cmath>
using namespace std;
class Geometry{
public:
virtual ~Geometry(){
}
public:
virtual float getArea()=0;
};
class Rectangle:public Geometry{
public:
Rectangle(float minX,float minY,float maxX,float maxY)
:minx(minX),miny(minY),maxx(maxX),maxy(maxY)
{
type = new char[20];
}
~Rectangle(){
if (type){
delete []type;
}
}
public:
float getArea(){
return fabs((minx-maxx)*(miny-maxy));
}
private:
float minx,miny,maxx,maxy;
char* type;
};
int main(int argc, const char * argv[]) {
Geometry *g = new Rectangle(0,0,3,4);
cout<< g->getArea()<<endl;
delete g;
return 0;
}