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


    #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 <<"
    我是漂亮的分割线1----------------------"<< endl;
    
        Father *f2;
        Child c2;
        cout << endl;
        f2 = &c2;
        f2->print(); //显示调用子类的函数
        cout << "
    我是漂亮的分割线2----------------------" << endl;
    
        Father f3;
        Child *c3 = NULL;
    //    c3 = &f3;
        c3->c;
        c3->f;
        c3->print(); //这种虽然不报错,但是在变异种出现错误,看来没有子类指针这回事
        cout << "
    我是漂亮的分割线3----------------------" << endl;
    
        system("pause");
    }
  • 相关阅读:
    条款1:理解模板类型推导
    非受限联合体
    整型
    vector作为函数返回类型
    SQL Server数据库空间管理 (1)
    1085 PAT单位排行 (25 分
    1084 外观数列 (20 分)
    1083 是否存在相等的差 (20 分)
    1082 射击比赛 (20 分)
    1081 检查密码 (15 分)
  • 原文地址:https://www.cnblogs.com/xiaochige/p/7538197.html
Copyright © 2020-2023  润新知