流水笔记

面向免费零食和饮料的编程

C++中的虚函数

直接上代码

[code]

include <iostream>

include <stdlib.h>

using namespace std;

class Base { public: virtual void virtualFunc() { cout << "BaseVirtualFunc" << endl; } void func() { cout << "BaseFunc" << endl; } void call() { virtualFunc(); func(); } };

class Derive : public Base { public: void virtualFunc() { cout << "SubClassVirtualFunc" << endl; } void func() { cout << "SubClassFunc" << endl; } };

int main(int argc, char argv[]) { Derive d; d.call(); /-------------------------output-------------------------/ /SubClassVirtualFunc                                     / /BaseFunc                                                / /--------------------------------------------------------*/ system("PAUSE"); }

[/code]