尝试创建c ++接口,现在进行测试。
/*
* Simple interface tests
*/
#include <iostream>
using namespace std;
class Shape {
public:
virtual int area() = 0;
};
class Square: public Shape {
public:
int side;
int height;
Square (int a, int b){
side = a;
height = b;
}
~Square(){cout << "destructor" << endl;}
int area(){
return side * height;}
};
int main(int argc, char **argv){
{
Shape * s1 = new Square(2, 2);
cout << s1->area() << endl;
delete s1;
}
return 0;
}
析构函数似乎无法正常工作,我只删除了指针。这是内存泄漏吗?如果是,如何避免呢?当然可以的话
Square s1(2,2);
那就很好了,但是我不确定创建指向抽象Shape类的指针时会发生什么。