注册 登录
编程论坛 VC++/MFC

基类与派生类的指针问题

laznrbfe 发布于 2011-05-31 20:17, 589 次点击
#include<iostream>
using namespace std;

class BaseClass
{
public:
    virtual void fn1();
    void fn2();

};

void BaseClass::fn1()
{
    cout<<"调用基类的虚函数fn1()"<<endl;
}

void BaseClass::fn2()
{
    cout<<"调用基类的非虚函数fn2()"<<endl;
}
class DerivedClass
{
public:
    void fn1();
    void fn2();
};

void DerivedClass::fn1()
{
    cout<<"调用派生类的函数fn1()"<<endl;
}

void DerivedClass::fn2()
{
    cout<<"调用派生类的函数fn2()"<<endl;
}

int main()
{
    DerivedClass aDerivedClass;
    DerivedClass *pDerivedClass=&aDerivedClass;
    BaseClass *pBaseClass=&aDerivedClass;//BaseClass *pBaseClass=pDerivedClass;
    pBaseClass->fn1();
    pBaseClass->fn2();
    pDerivedClass->fn1();
    pDerivedClass->fn2();
    return 0;

}
4 回复
#2
laznrbfe2011-05-31 21:09
各位老大帮帮忙~
#3
踏魔狼2011-06-01 14:39
class DerivedClass???没见有派生-
#4
wojiaoguowei2011-06-02 16:12
很简单的问题
调用派生类的函数fn1()
调用派生类的非虚函数fn2()
调用派生类的函数fn1()
调用派生类的函数fn2()
#5
wojiaoguowei2011-06-02 16:13
对了,你忘写从基类继承的派生类了
1