zoukankan      html  css  js  c++  java
  • C++构造函数虚函数例题

    虚函数:

    #include <iostream>
    
    class A
    {
    public:
        A ():m_iVal(0)
        {
            test();
        }
        virtual void func()
        {
            std::cout<<m_iVal<<' ';
        }
        void test()
        {
            func();
        }
    public:
        int m_iVal;
    };
    
    
    class B : public A
    {
    public:
        B()
        {
            test();
        };
        virtual void func()
        {
            ++m_iVal;
            std::cout<<m_iVal<<' ';
        }
    };
    int main(int argc ,char* argv[])
    {
        A*p = new B;
        p->test();
        return 0;
    }
     输出结果 0 1 2;
    A*p = new B;
    B的构造函数先要调用A的构造函数
    A中
    m_iVal初始化为0,输出0
    然后调用自己的构造函数
    自己的构造函数有个test()函数,自然只能去调用父类中的函数test,但要注意这个test调用func确实B的

     此时的B中的A类子对象已经构造好了,所以这个func()函数将是B的,这里是B的并不是因为多态,而是因为在构造函数中调用虚函数就是这样
    最后一个p->test();这个最简单多态的原因当然是调用B的



    如果把A中的Virtual去掉

    void func()
    {
    std::cout<<m_iVal<<' ';
    }

    则结果是 0 0 0;

    这个是因为所有的都是调用A的func



    3 代码在做修改
    #include <iostream>
    
    class A
    {
    public:
        A ():m_iVal(0)
        {
            test();
        }
        void func()
        {
            std::cout<<m_iVal<<' ';
        }
        void test()
        {
            func();
        }
    public:
        int m_iVal;
    };
    
    
    class B : public A
    {
    public:
        B()
        {
            func();
        };
        virtual void func()
        {
            ++m_iVal;
            std::cout<<m_iVal<<' ';
        }
    };
    int main(int argc ,char* argv[])
    {
        A*p = new B;
        p->func();
        return 0;
    }

    输出 0 1 1.

    特别要注意 p->func();这个在C++中是不表现出多态的,C++中的多态需要父类为虚函数

    4、输出 
    bar foo b_bar

    #include <iostream>
    #include <stdlib.h>
    #include <stdio.h>
    using namespace std;
    
    struct A
    {
        void foo(){printf(" foo");}
        virtual void bar(){printf(" bar");}
        A(){bar();}
    
    };
    struct B: A
    {
        void foo(){printf(" b_foo");}
        void bar(){printf(" b_bar");}
    
    };
    
    
    int main()
    {
    
    
        A* p=new B;
        p->foo();
        p->bar();
        return 0;
    
    }

    这个尤其能说明上面的问题, fool没有virtual 因而不表现多态,输出还是A的

    5 再来看个构造和析构的问题

    #include <iostream>
    #include <stdlib.h>
    #include <stdio.h>
    using namespace std;
    class A
    {
    public:
        A()
        {
            cout<<"construct A"<<endl;
        }
        ~A()
        {
            cout<<"destruct A"<<endl;
        }
    };
    
    class B:public A
    {
    public:
        B()
        {
            cout<<"construct B"<<endl;
        }
        ~B()
        {
            cout<<"destruct B"<<endl;
        }
    };
    int main()
    {
    
    
        B* p=new B;
        delete(p);
        return 0;
    
    }

    construct A
    construct B
    destruct B
    destruct A

    而如果改成

    A* p=new B;

    construct A
    construct B
    destruct A



  • 相关阅读:
    数据库权限分配操作
    1130-host ... is not allowed to connect to this MySql server
    ubuntu 14.04安装mysql-python
    Ubuntu中的MySQL修改root密码的多种方法
    ubuntu下安装mysql及卸载mysql方法
    XShell本地上传文件到Ubuntu上及从Ubuntu下载文件到本地
    深度学习-1
    html-新闻滚动条
    Django部署uwsgi 与 nginx配置
    二叉树层次遍历
  • 原文地址:https://www.cnblogs.com/hixin/p/4784146.html
Copyright © 2011-2022 走看看