将基类绑定到派生类有什么好处,而不是简单地创建派生类指针?

请注意,在此示例中,我创建了派生类指针(Laser * pLaser = new Laser),而不是像(Boat * pLaser = new Laser)这样的基类指针。相反,创建基类指针有优势吗?谢谢!

   #include <iostream>
    using namespace std;

    class Boat      
    {
        protected:
         int length;

        public:
         int getLength() { return length; }
         virtual void Model() = 0;
    };

    class Sailboat : public Boat        
    {
        protected:
         int mast;

        public:
         int getMast() { return mast; }
         virtual void Boom() = 0;
    };

    class Laser : public Sailboat       
    {
        public:
         Laser() { mast = 19; length = 35; }    
         ~Laser();                              

         void Model() { cout << "Laser Classic" << endl; }  
         void Boom() { cout << "Boom: 14 ft" << endl; }
    };


    int main()
    {
        Laser* pLaser = new Laser;      

        pLaser -> Model();
        cout << "Length: " << pLaser -> getLength() << "ft" << endl;
        cout << "Height: " << pLaser -> getMast() << "ft" << endl;
        pLaser -> Boom();

        return 0;
    }
评论