mirror of
https://github.com/huihut/interview.git
synced 2024-03-22 13:10:48 +08:00
为动态多态(晚绑定)例子加上虚析构和delete指针
This commit is contained in:
parent
f11d2f3261
commit
baf036d9b5
|
@ -515,6 +515,7 @@ public:
|
||||||
{
|
{
|
||||||
...
|
...
|
||||||
}
|
}
|
||||||
|
virtual ~Shape();
|
||||||
};
|
};
|
||||||
class Circle : public Shape // 圆形类
|
class Circle : public Shape // 圆形类
|
||||||
{
|
{
|
||||||
|
@ -534,6 +535,10 @@ int main()
|
||||||
Shape * shape2 = new Rect(5.0, 6.0);
|
Shape * shape2 = new Rect(5.0, 6.0);
|
||||||
shape1->calcArea(); // 调用圆形类里面的方法
|
shape1->calcArea(); // 调用圆形类里面的方法
|
||||||
shape2->calcArea(); // 调用矩形类里面的方法
|
shape2->calcArea(); // 调用矩形类里面的方法
|
||||||
|
delete shape1;
|
||||||
|
shape1 = nullptr;
|
||||||
|
delete shape2;
|
||||||
|
shape2 = nullptr;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
Loading…
Reference in New Issue
Block a user