DreamsofJAVAD 2010-12-18
class 类名{ public: virtual 成员函数说明; } class 类名:基类名{ public: virtual 成员函数说明; }三、 虚函数在内存中的结构
#include "iostream.h" #include "string.h" class A { public: virtual void fun0() { cout << "A::fun0" << endl; } }; int main(int argc, char* argv[]) { A a; cout << "Size of A = " << sizeof(a) << endl; return 0; }结果如下:Size of A = 4
class A { public: void fun0() { cout << "A::fun0" << endl; } }; int main(int argc, char* argv[]) { A a; cout << "Size of A = " << sizeof(a) << endl; return 0; }结果如下:Size of A = 1
class A { public: virtual void fun0() { cout << "A::fun0" << endl; } int a; int b; }; int main(int argc, char* argv[]) { A a; cout << "Size of A = " << sizeof(a) << endl; return 0; }结果如下:Size of A = 12
class A { public: virtual void fun0() { cout << "A::fun0" << endl; } virtual void fun1() { cout << "A::fun1" << endl; } int a; int b; }; int main(int argc, char* argv[]) { A a; cout << "Size of A = " << sizeof(a) << endl; return 0; }结果如下:结果如下:
继承对象模型在 C++ 编译器的内部类可以理解为结构体子类是由父类成员叠加子类新成员得到的class Dervied : public Demo. << "mk = " << mk << endl;c