子类父类(虚函数下的 引用指针 对象)->看来没有子类指针这回事

#include<iostream>

using namespace std;

class Father
{
public:
    Father()
    {
        cout << "我是父类构造函数";
    }
    ~Father()
    {
        cout << "我是父类析构函数 ";
    }
    virtual    void print()
    {
        cout << "我是父类的函数";
    }
public:
    int f;
protected:
};

class Child: public Father
{
public:
    Child()
    {
        cout << "我是子类构造函数";
    }
    ~Child()
    {
        cout << "我是子类析构函数 ";
    }
    void print()
    {
        cout << "我是子类的函数";
    }
public:
    int c;
protected:
};

int main()
{
    Father f1; //调用父类的构造函数
    Child c1;//先调用 父类的构造函数 再调用子类的构造函数。
    cout << endl;
    cout << "f1.print()   :";
    f1.print(); //显示调用父类的函数

    cout << endl << "c1.print()  ::";
    c1.print();//显示调用子类的函数
    
    f1.f = c1.f;
    cout <<"\n我是漂亮的分割线1----------------------"<< endl;

    Father *f2;
    Child c2;
    cout << endl;
    f2 = &c2;
    f2->print(); //显示调用子类的函数
    cout << "\n我是漂亮的分割线2----------------------" << endl;

    Father f3;
    Child *c3 = NULL;
//    c3 = &f3;
    c3->c;
    c3->f;
    c3->print(); //这种虽然不报错,但是在变异种出现错误,看来没有子类指针这回事
    cout << "\n我是漂亮的分割线3----------------------" << endl;

    system("pause");
}

 

posted @ 2017-09-17 22:15  小陈同学啦  阅读(223)  评论(0)    收藏  举报