C++实例之虚函数(析构函数和普通函数)
来源:互联网 发布:如何安装cad软件 编辑:程序博客网 时间:2024/06/05 17:44
#include "stdafx.h"#include <iostream>using namespace std;/* 情形一// 输出为:// Output from the constructor of class ClxBase!// Output from the constructor of class ClxDerived!// Do something in class ClxDerived!// Output from the destructor of class ClxDerived!// Output from the destructor of class ClxBase!*/class ClxBase{//本例父类中析构函数是否为virtual对输出结果没有影响public: ClxBase() { cout << "Output from the constructor of class ClxBase!" << endl; }; virtual ~ClxBase() { cout << "Output from the destructor of class ClxBase!" << endl; }; virtual void DoSomething() { cout << "Do something in class ClxBase!" << endl; };};class ClxDerived : public ClxBase{public: ClxDerived() { cout << "Output from the constructor of class ClxDerived!" << endl; }; ~ClxDerived() { cout << "Output from the destructor of class ClxDerived!" << endl; }; void DoSomething() { cout << "Do something in class ClxDerived!" << endl; };};int _tmain(int argc, _TCHAR* argv[]){ClxDerived *pTest = new ClxDerived;pTest->DoSomething();delete pTest;return 0;}/* 情形二// 输出为:// Output from the constructor of class ClxBase!// Output from the constructor of class ClxDerived!// Do something in class ClxDerived!// Output from the destructor of class ClxDerived!// Output from the destructor of class ClxBase!*/class ClxBase{//本例父类中析构函数是否为virtual对输出结果没有影响public: ClxBase() { cout << "Output from the constructor of class ClxBase!" << endl; }; virtual ~ClxBase() { cout << "Output from the destructor of class ClxBase!" << endl; }; void DoSomething() { cout << "Do something in class ClxBase!" << endl; };};class ClxDerived : public ClxBase{public: ClxDerived() { cout << "Output from the constructor of class ClxDerived!" << endl; }; ~ClxDerived() { cout << "Output from the destructor of class ClxDerived!" << endl; }; void DoSomething() { cout << "Do something in class ClxDerived!" << endl; };};int _tmain(int argc, _TCHAR* argv[]){ClxDerived *pTest = new ClxDerived;pTest->DoSomething();delete pTest;return 0;}/* 情形三// 输出为:// Output from the constructor of class ClxBase!// Output from the constructor of class ClxDerived!// Do something in class ClxBase!// Output from the destructor of class ClxDerived!// Output from the destructor of class ClxBase!*/class ClxBase{//本例父类中析构函数和DoSomething()函数是否为virtual对输出结果没有影响public: ClxBase() { cout << "Output from the constructor of class ClxBase!" << endl; }; virtual ~ClxBase() { cout << "Output from the destructor of class ClxBase!" << endl; }; virtual void DoSomething() { cout << "Do something in class ClxBase!" << endl; };};class ClxDerived : public ClxBase{public: ClxDerived() { cout << "Output from the constructor of class ClxDerived!" << endl; }; ~ClxDerived() { cout << "Output from the destructor of class ClxDerived!" << endl; }; };int _tmain(int argc, _TCHAR* argv[]){ClxDerived *pTest = new ClxDerived;pTest->DoSomething();delete pTest;return 0;}
- C++实例之虚函数(析构函数和普通函数)
- 【C++】普通函数、虚函数和纯虚函数
- 虚函数和普通函数调用
- 虚拟析构函数和虚拟普通函数
- 宽字符c函数和普通字符C函数
- static 函数和普通函数
- 普通函数和构造函数
- 写String类的普通构造函数,析构函数,拷贝构造函数和赋值函数
- C/C++之宏、内联函数和普通函数的区别
- c语言中static 函数和普通函数的区别?
- c语言中static 函数和普通函数的区别?
- c语言中static 函数和普通函数的区别
- c语言中static 函数和普通函数的区别?
- c语言中static 函数和普通函数的区别?
- c语言中static 函数和普通函数的区别?
- c/c++ 静态函数(static)和普通函数
- c中静态函数和普通函数区别
- c语言中static 函数和普通函数的区别?
- js实现右键菜单
- POCO C++库导游
- draw9patch详解
- Ubuntu的debootstrap
- 完数
- C++实例之虚函数(析构函数和普通函数)
- Use exceptions to watch for and handle I/O errors.
- android_launcher的源码详细分析
- c# ref参数使用
- KMP算法
- 系统提示SVCHOST错误,该内存不能为“read”解决办法
- diff和patch使用指南
- Use eof() to read and display a text file.
- 请热爱c++(转自人人)