024:Fun和Do

#include <iostream>
using namespace std;
class A {
private:
    int nVal;
public:
    void Fun()
    { cout << "A::Fun" << endl; };
    void Do()
    { cout << "A::Do" << endl; }
};
class B:public A {
public:
    virtual void Do()
    { cout << "B::Do" << endl;}
};
class C:public B {
public:
    void Do( )
    { cout <<"C::Do"<<endl; }
    void Fun()
    { cout << "C::Fun" << endl; }
};
void Call(B &p) {
    p.Fun(); p.Do();
}
int main() {
    C c;
//    B *p ;
//    p = &c;
//    Call( c);
    B &p = c;
    p.Fun();
    p.Do();
    return 0;
}
/*
A::Fun
C::Do

 */

不能写成B p = c;

只能定义B &p = c;

对应 B *p = &c;  

上一篇:024.讲MFC_窗口指针


下一篇:[Python3] 024 面向对象 第四弹