Guest User

Untitled

a guest
May 27th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.92 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3. class Base
  4. {
  5. public:
  6. Base()
  7. {
  8. cout <<"constructor base\n";
  9. }
  10. virtual ~Base()
  11. {
  12. cout <<"Destructor base \n";
  13. }
  14.  
  15. };
  16.  
  17. class Derived:public Base
  18. {
  19. public:
  20. Derived()
  21. {
  22. cout <<"constructor Derived\n";
  23.  
  24. }
  25. ~Derived() override
  26. {
  27. cout <<"Destructor Derived\n";
  28. }
  29.  
  30.  
  31. };
  32.  
  33. int main()
  34. {
  35.  
  36. Base * base = new Base();
  37. delete base;
  38. cout <<"---------------"<<endl;
  39. Derived *derived = new Derived();
  40. delete derived;
  41. cout <<"---------------"<<endl;
  42. Base* poly = new Derived();
  43. //注意当析构函数前面不加virtual keyword,析构时的第三种情况
  44. delete poly;
  45. return 0;
  46. /**
  47. * output: base class 析构函数不加virtual
  48. constructor base
  49. Destructor base
  50. ---------------
  51. constructor base
  52. constructor Derived
  53. Destructor Derived
  54. Destructor base
  55. ---------------
  56. constructor base
  57. constructor Derived
  58. Destructor base
  59. *
  60. */
  61.  
  62.  
  63.  
  64. }
Add Comment
Please, Sign In to add comment