有重写的单继承
派生类中重写了基类的print()函数。
//Derived_Overwrite.h #pragma once #include "base.h" class Derived_Overrite : public Base { public: Derived_Overrite(int); virtual ~Derived_Overrite(void); virtual void print(void) const; protected: int iDerived; };
//Derived_Overwrite.cpp #include "stdafx.h" #include "Derived_Overwrite.h" Derived_Overrite::Derived_Overrite(int i):Base(0) { iDerived = i; cout<<"Derived_Overwrite::Derived_Overwrite()"<<endl; } Derived_Overrite::~Derived_Overrite(void) { cout<<"Derived_Overwrite::~Derived_Overwrite()"<<endl; } void Derived_Overrite::print(void) const { cout<<"Derived_Overwrite::print(), iDerived " << iDerived << endl; }
Base、Derived_Overwrite的类图如下所示:
重写print()函数在虚函数表中表现如下:
为了验证上述C++对象模型,我们编写如下测试代码。
时间: 2024-10-18 08:05:05