设为首页 加入收藏

TOP

9.6.7 虚析构函数(1)
2013-10-07 12:42:15 来源: 作者: 【 】 浏览:68
Tags:9.6.7 函数

9.6.7  虚析构函数(1)

当使用基类指针处理派生类对象时,出现的一个问题是正确的析构函数可能没有被调用。通过修改上一个示例,我们就能看到这样的结果。

试一试:调用错误的析构函数

在本示例中,我们只需给每个类添加一个输出消息的析构函数,以便了解销毁对象时被调用的是哪一个析构函数。该示例的Container.h文件如下所示:

  1. // Container.h for Ex9_12  
  2. #pragma once  
  3. #include <iostream> 
  4. using std::cout;  
  5. using std::endl;  
  6.  
  7. class CContainer // Generic base class for specific containers  
  8. {  
  9. public:  
  10. // Destructor  
  11. ~CContainer()  
  12. { cout << "CContainer destructor called" << endl; }  
  13.  
  14. // Function for calculating a volume - no content  
  15. // This is defined as a 'pure' virtual function, signified by '= 0'  
  16. virtual double Volume() const = 0;  
  17.  
  18. // Function to display a volume  
  19. virtual void ShowVolume() const  
  20. {  
  21. cout << endl 
  22. << "Volume is " << Volume();  
  23. }  
  24. }; 

该示例的Can.h文件内容如下:

  1. // Can.h for Ex9_12  
  2. #pragma once  
  3. #include "Container.h" // For CContainer definition  
  4. extern const double PI;  
  5.  
  6. class CCan: public CContainer  
  7. {  
  8. public:  
  9. // Destructor  
  10. ~CCan()  
  11. { cout << "CCan destructor called" << endl; }  
  12.  
  13. // Function to calculate the volume of a can  
  14. virtual double Volume() const  
  15. { return 0.25*PI*m_Diameter*m_Diameter*m_Height; }  
  16.  
  17. // Constructor  
  18. CCan(double hv = 4.0, double dv = 2.0): m_Height(hv), m_Diameter(dv){}  
  19.  
  20. protected:  
  21. double m_Height;  
  22. double m_Diameter;  
  23. }; 

Box.h文件的内容应该是:

  1. // Box.h for Ex9_12  
  2. #pragma once  
  3. #include "Container.h" // For CContainer definition  
  4. #include <iostream> 
  5. using std::cout;  
  6. using std::endl;  
  7.  
  8. class CBox: public CContainer // Derived class  
  9. {  
  10. public:  
  11. // Destructor  
  12. ~CBox()  
  13. { cout << "CBox destructor called" << endl; }  
  14.  
  15. // Function to show the volume of an object  
  16. virtual void ShowVolume() const  
  17. {  
  18. cout << endl 
  19. << "CBox usable volume is " << Volume();  
  20. }  
  21.  
  22. // Function to calculate the volume of a CBox object  
  23. virtual double Volume() const  
  24. { return m_Length*m_Width*m_Height; }  
  25.  
  26. // Constructor  
  27. CBox(double lv = 1.0, double wv = 1.0, double hv = 1.0)  
  28.                                   :m_Length(lv), m_Width(wv), m_Height(hv){}  
  29.  
  30. protected:  
  31. double m_Length;  
  32. double m_Width;  
  33. double m_Height;  
  34. }; 

GlassBox.h头文件应该包含下面的代码:

  1. // GlassBox.h for Ex9_12  
  2. #pragma once  
  3. #include "Box.h" // For CBox  
  4.  
  5. class CGlassBox: public CBox // Derived class  
  6. {  
  7. public:  
  8. // Destructor  
  9. ~CGlassBox()  
  10. { cout << "CGlassBox destructor called" << endl; }  
  11.  
  12. // Function to calculate volume of a CGlassBox  
  13. // allowing 15% for packing  
  14. virtual double Volume() const  
  15. { return 0.85*m_Length*m_Width*m_Height; }  
  16.  
  17. // Constructor  
  18. CGlassBox(double lv, double wv, double hv): CBox(lv, wv, hv){}  
  19. }; 

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇9.6.7 虚析构函数(2) 下一篇9.9.1 装箱与拆箱

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: