为动态多态(晚绑定)例子加上虚析构和delete指针

This commit is contained in:
huihut 2018-05-02 11:35:32 +08:00
parent f11d2f3261
commit baf036d9b5

View File

@ -515,6 +515,7 @@ public:
{
...
}
virtual ~Shape();
};
class Circle : public Shape // 圆形类
{
@ -534,6 +535,10 @@ int main()
Shape * shape2 = new Rect(5.0, 6.0);
shape1->calcArea(); // 调用圆形类里面的方法
shape2->calcArea(); // 调用矩形类里面的方法
delete shape1;
shape1 = nullptr;
delete shape2;
shape2 = nullptr;
return 0;
}
```