Deriving an abstract class from concrete class

后端 未结 7 817
囚心锁ツ
囚心锁ツ 2021-02-05 16:05

Let\'s say we have a concrete class Apple. (Apple objects can be instantiated.) Now, someone comes and derives an abstract class Peach from Apple. It\'

7条回答
  •  北海茫月
    2021-02-05 16:38

    a bit unusual, but if you had some other subclass of the base class and the subclasses of the abstract class had enough common stuff to justify the existance of the abstract class like:

    class Concrete
    {
    public:
        virtual void eat() {}
    };
    class Sub::public Concrete { // some concrete subclass
        virtual void eat() {}
    };
    class Abstract:public Concrete // abstract subclass
    {
    public:
        virtual void eat()=0;
        // and some stuff common to Sub1 and Sub2
    };
    class Sub1:public Abstract {
        void eat() {}
    };
    class Sub2:public Abstract {
        void eat() {}
    };
    int main() {
        Concrete *sub1=new Sub1(),*sub2=new Sub2();
        sub1->eat();
        sub2->eat();
        return 0;
    }
    

提交回复
热议问题