📜  C ++中各种继承类型中的运行时多态

📅  最后修改于: 2021-06-01 02:06:32             🧑  作者: Mango

C++允许用户将使用虚拟函数的运行时多态性的概念用于任何类型的Inheritance。

以下是在所有类型的继承中如何实现运行时多态的方法:

  1. 单一继承:
    // C++ program to demonstrate Run Time
    // Polymorphism in Single Inheritance
      
    #include 
    using namespace std;
      
    // Base Class
    class Base {
      
    public:
        // Virtual function
        virtual void funct1()
        {
            cout << "Base::funct1() is called\n";
        }
      
        // Virtual function
        virtual void funct2(int x)
        {
            cout << "Base's Val of x:"
                 << x << endl;
        }
      
        // Non-Virtual Function
        void funct3()
        {
            cout << "Base is the Parent class!"
                 << endl;
        }
    };
      
    // Derived Class or Sub Class
    class Derived : public Base {
    private:
        // Virtual Functions
        // can also be Private!
        void funct1()
        {
            cout << "Derived::funct1() is called\n";
        }
        void funct2(int x)
        {
            cout << "Derived Class's Val of x:"
                 << x << endl;
        }
        void funct3()
        {
            cout << "It's the Derived class's"
                 << " funct3() called!" << endl;
        }
    };
      
    int main()
    {
      
        // Run-Time Polymorphism
        // in Single Inheritance
        Base* bptr = new Derived();
      
        // virtual function
        bptr->funct1();
      
        // virtual function
        bptr->funct2(12);
      
        // Non-virtual function
        bptr->funct3();
      
        return 0;
    }
    
    输出:
    Derived::funct1() is called
    Derived Class's Val of x:12
    Base is the Parent class!
    
  2. 多重继承:
    #include 
    using namespace std;
      
    // Parent to Derived class
    class Base1 {
    public:
        // Non-Virtual function
        void funct1()
        {
            cout << "Base1::funct1() is called\n";
        }
      
        // Virtual function
        virtual void funct2(int x)
        {
            cout << "Base1's Val of x:"
                 << x << endl;
        }
      
        // Non-Virtual Function
        void funct3()
        {
            cout << "Base1 is the Parent class!"
                 << endl;
        }
    };
      
    // Second Parent to Derived class
    class Base2 {
    public:
        void funct1()
        {
            cout << "Base2::funct1() is called\n";
        }
        void funct2(int x)
        {
            cout << "Base2's Val of x:" << x << endl;
        }
      
        // Only Virtual Function
        // in Base2 Parent class
        virtual void funct3()
        {
            cout << "Base2 is Also a Parent class!"
                 << endl;
        }
    };
      
    // Derived Class of Base1 and Base2
    class Derived : public Base1, public Base2 {
    private:
        void funct1()
        {
            cout << "Derived::funct1() is called\n";
        }
        void funct2(int x)
        {
            cout << "Derived Class's Val of x:"
                 << x << endl;
        }
        void funct3()
        {
            cout << "Derived::funct3() is called "
                 << "and not Base2::funct3() due"
                 << " to RTP" << endl;
        }
    };
      
    int main()
    {
        Derived d;
      
        // Run-Time Polymorphism
        // in Multiple Inheritance
        Base1* b1ptr = &d;
      
        // Compile-Time Binding,
        // Hence Base1::funct1() will be called!
        b1ptr->funct1();
      
        // virtual function of Base1
        // RunTime PolyMorphism
        b1ptr->funct2(10);
      
        // Now Parent Class Base2
        // is also pointed to object 'd'
        // of Derived (to demonstrate RTP)
        Base2* b2ptr = &d;
      
        // virtual function of Base2
        // RunTime PolyMorphism
        b2ptr->funct3();
      
        return 0;
    }
    
    输出:
    Base1::funct1() is called
    Derived Class's Val of x:10
    Derived::funct3() is called and not Base2::funct3() due to RTP
    

    注意:此处Base1指针和Base2指针都可能指向相同的派生类对象d,但实际上,由于使用了不同的基类指针,编译器在运行时选择了不同的虚函数。

  3. 多级继承:
    // C++ Program to illustrate Run-Time
    // Polymorphism in multi-level inheritance
      
    #include 
    using namespace std;
      
    // Parent Class
    class Base1 {
    public:
        // Virtual function
        virtual void funct1()
        {
            cout << "Base1::funct1() is called\n";
        }
      
        // Virtual function
        virtual void funct2(int x)
        {
            cout << "Base1's Val of x:"
                 << x << endl;
        }
      
        // Non-Virtual Function
        void funct3()
        {
            cout << "Base1 is the Parent class!"
                 << endl;
        }
    };
      
    // Derived Class of Base1
    // but Parent to Base3
    class Base2 : public Base1 {
      
        // Virtual Functions can be Private!
    private:
        void funct1()
        {
            cout << "Base2::funct1() is called\n";
        }
        void funct2(int x)
        {
            cout << "Base2's Val of x:"
                 << x << endl;
        }
        void funct3()
        {
            cout << "Base2 is the first "
                 << "Derived class!" << endl;
        }
    };
      
    // Derived Class of Base2
    // but Parent to Derived
    class Base3 : public Base2 {
    private:
        void funct1()
        {
            cout << "Base3::funct1() is called\n";
        }
        void funct2(int x)
        {
            cout << "Base3's Val of x:"
                 << x << endl;
        }
        void funct3()
        {
            cout << "Class Base3 is second "
                 << "Derived class!" << endl;
        }
    };
      
    // 3 Levels of Multi-Level Inheritance
    // and final Child Class
    class Derived : public Base3 {
    private:
        void funct1()
        {
            cout << "Derived::funct1() is called\n";
        }
        void funct2(int x)
        {
            cout << "Derived Class's Val of x:"
                 << x << endl;
        }
        void funct3()
        {
            cout << "Class Derived is Final"
                 << " Child class!" << endl;
        }
    };
      
    int main()
    {
      
        // Run-Time Polymorphism
        // in multi-level Inheritance
        Base1* b1ptr = new Derived;
      
        b1ptr->funct1();
        b1ptr->funct2(30);
      
        // Compile-Time Binding
        b1ptr->funct3();
      
        return 0;
    }
    
    输出:
    Derived::funct1() is called
    Derived Class's Val of x:30
    Base1 is the Parent class!
    

    说明:在上面的示例中,Derived类是继承自Base3的最终子类,该子类继承自Base2,后者又再次继承自Base1(父类为Base2)。但是,即使您尝试在Base1类中使用虚拟函数并将其指针指向派生类(这是Base1的曾祖父),也可以看到运行时多态性。因此,即使在这里,运行时多态也可以根据标准规则进行工作。

  4. 层次继承:
    // C++ Program to illustrate Run-Time
    // Polymorphism in Hierarchical inheritance
      
    #include 
    using namespace std;
      
    class Base1 {
    public:
        // Virtual function of Parent Class
        virtual void funct1()
        {
            cout << "Base1::funct1() is called\n";
        }
        virtual void funct2(int x)
        {
            cout << "Base1's Val of x:" << x << endl;
        }
      
        // Non-Virtual Function
        void funct3()
        {
            cout << "Base1 is the Parent class!"
                 << endl;
        }
    };
      
    class Base2 : public Base1 {
    private:
        void funct1()
        {
            cout << "Base2::funct1() is called\n";
        }
        void funct2(int x)
        {
            cout << "Base2's Val of x:"
                 << x << endl;
        }
        void funct3()
        {
            cout << "Base2 is the first"
                 << " Derived class!" << endl;
        }
    };
      
    class Base3 : public Base1 {
    private:
        void funct1()
        {
            cout << "Base3::funct1() is called\n";
        }
        void funct2(int x)
        {
            cout << "Base3's Val of x:"
                 << x << endl;
        }
        void funct3()
        {
            cout << "Class Base3 is second"
                 << " Derived class!" << endl;
        }
    };
      
    // Grand-Child_1 of Base1 class
    class Derived1 : public Base3 {
    private:
        void funct1()
        {
            cout << "Derived1::funct1() is called\n";
        }
        void funct2(int x)
        {
            cout << "Derived1 Class's Val of x:"
                 << x << endl;
        }
        void funct3()
        {
            cout << "Class Derived1 is Good!!"
                 << endl;
        }
    };
      
    // Grand-Child_2 of Base1 class
    class Derived2 : public Base3 {
    private:
        void funct1()
        {
            cout << "Derived2::funct1()"
                 << " is called\n";
        }
        void funct2(int x)
        {
            cout << "Derived2 Class's Val "
                 << "of x:" << x << endl;
        }
        void funct3()
        {
            cout << "Class Derived2 is Good!!"
                 << endl;
        }
    };
      
    // Run-Time Polymorphism
    // in Hierarchical Inheritance
    int main()
    {
      
        // Base1 class's(Parent class's)
        // pointer points to Derived1 class
        Base1* b1ptr = new Derived1();
      
        // Run-Time Polymorphism
        b1ptr->funct1();
        Derived2 d2;
      
        // Now the Base1 class pointer
        // points to d2 object(Derived2 class)
        b1ptr = &d2;
      
        // Run-Time Polymorphism
        b1ptr->funct2(30);
      
        // Compile-Time Binding
        b1ptr->funct3();
      
        return 0;
    }
    
    输出:
    Derived1::funct1() is called
    Derived2 Class's Val of x:30
    Base1 is the Parent class!
    

    说明:在这里,父级是Base1,其子级是Derived1类和Derived2类。即使在这种情况下,由于虚拟功能(“ VPTR”和“ VTABLE”),当将Base1类指针指向Derived1对象或Derived2对象时,我们也可以在此处应用运行时多态。

要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”