65,184
社区成员




// BaseDerive.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream.h>
#include <string>
class Base
{
public:
virtual void f(float x){ cout << "Base::f(float) " << x << endl; }
void g(float x){ cout << "Base::g(float) " << x << endl; }
void h(float x){ cout << "Base::h(float) " << x << endl; }
};
class Derived : public Base
{
public:
virtual void f(float x){ cout << "Derived::f(float) " << x << endl; }
void g(int x){ cout << "Derived::g(int) " << x << endl; }
void h(float x){ cout << "Derived::h(float) " << x << endl; }
};
void main(void)
{
Derived d;
cout<<sizeof(Base)<<endl;
cout<<sizeof(Derived)<<endl;
Base *pb = &d;
Derived *pd = &d;
//
// pb->f(3.14f); //运行结果: Derived::f(float) 3.14
pd->f(3.14f); //运行结果: Derived::f(float) 3.14
pd->f('3);
// Bad : behavior depends on type of the pointer
pb->g(3.14f); //运行结果: Base::g(float) 3.14
pd->g(3.14f); //运行结果: Derived::g(int) 3
// Bad : behavior depends on type of the pointer
pb->h(3.14f); //运行结果: Base::h(float) 3.14
pd->h(3.14f); //运行结果: Derived::h(float) 3.14
}