zoukankan      html  css  js  c++  java
  • C++多态--虚函数

    虚函数

    1.如果基类中某个成员函数被声明为虚函数,那么子类中和该函数同名的成员函数也变成虚函数,并且对基类中版本形成覆盖即函数重写 

    2.满足虚函数覆盖后,通过指向子类对象的基类指针或者通过引用子类对象的基类引用,去调用虚函数,实际被执行的将是子类中的覆盖版本,而不是基类中的原始版本,这种语法现象称为多态

    不是虚函数时

    #include<iostream>
    
    class A {   
    public:
        A(int x = 0, int y = 0) :m_x(x), m_y(y) {    }
        void print(void) {  
            std::cout << "A类:" << m_x << ", " << m_y << std::endl;
        }
    protected:
        int m_x;  
        int m_y;  
    
    };
    class B :public A {  
    public:
        B(int x, int y, int w, int h) :A(x, y), m_w(w), m_h(h) {}
        void print(void) {  
            std::cout << "B类:" << m_x << ", " << m_y << ", " << m_w << ", " << m_h << std::endl;
        }
    private:
        int m_w;
        int m_h;
    };
    
    
    int main()
    {
        B b(10,20,30,40);
        b.print();
        A* a = &b; //向上造型
        a->print();  //这里输出基类A的函数
        
    
        return 0;
    }

    虚函数时

    #include<iostream>
    
    class A {   
    public:
        A(int x = 0, int y = 0) :m_x(x), m_y(y) {    }
        virtual void print(void) {       //虚函数
            std::cout << "A类:" << m_x << ", " << m_y << std::endl;
        }
    protected:
        int m_x;  
        int m_y;  
    
    };
    class B :public A {  
    public:
        B(int x, int y, int w, int h) :A(x, y), m_w(w), m_h(h) {}
        void print(void) {  
            std::cout << "B类:" << m_x << ", " << m_y << ", " << m_w << ", " << m_h << std::endl;
        }
    private:
        int m_w;
        int m_h;
    };
    
    
    int main()
    {
        B b(10,20,30,40);
        b.print();
        A* a = &b; //向上造型
        a->print();  //这里输出子类B的函数
        
    
        return 0;
    }

    多态 

    多态的条件:
    1.除了在基类中设置为虚函数,还必须通过指针或引用调用虚函数才能表现出来

    #include<iostream>
    
    class Base {
    public:
        virtual int cal(int x, int y) {
            return x + y;
        }
        //void func(Base* this)
        void func(void) {  //间接调用虚函数
            std::cout << cal(4, 5) << std::endl;
        }
    
    };
    
    class Der :public Base {
        int cal(int x, int y) {
            return x * y;
        }
    };
    
    int main()
    {
        Der d;
        Base b = d;
        std::cout << b.cal(10, 20) << std::endl; //30
        //没有使用指针或引用调用,调用的还是基类函数---没有多态现象
    
        Der dd;
        dd.func();  //20
        //原因:this=&dd   所以调用的是dd的函数---有多态现象--80%这么用【重点掌握】
        //可以把虚函数写成保护,防止出现上面的错误
        
        
        return 0;
    }

    注意:虚函数不能内联优化 

  • 相关阅读:
    DAY9学习笔记
    DAY8课下作业
    DAY8学习笔记
    DAY7课下作业
    github clone 开源代码报Permission denied (publickey)
    grandle构建工程报:javax.net.ssl.SSLException
    centos安装redash
    centos安装nodejs
    CURL执行请求
    flume定制拦截器Interceptor
  • 原文地址:https://www.cnblogs.com/liming19680104/p/13595494.html
Copyright © 2011-2022 走看看