Why must I re-declare a virtual function from an inherited class?

前端 未结 4 1116
长发绾君心
长发绾君心 2021-02-14 02:24

I\'m working on a simple C++ program and am having a difficult time understanding a compiler error I was getting. The issue was caused by me attempting to create a derived class

4条回答
  •  挽巷
    挽巷 (楼主)
    2021-02-14 03:01

    you should override all the base class pure virtual functions to be able to instantiate derived class.

    • you cannot define a base class member function from derived class.

    in your example you are trying to define method1 and method2 and method3 which are not members of DerivedClass!! you have to declare them yourself in your derived class. compiler doesn't do it for you.

    so your Derivedclass.h will look like:

    #ifndef DERIVEDCLASS_H
    #define DERIVEDCLASS_H
    
    #include "BaseClass.h"
    
    class DerivedClass: public BaseClass
    {
    
        public:
            DerivedClass(void); 
    
            virtual int method1(void); // not pure function
            virtual int method2(void);
            virtual float method3(void);
    };
    
    #endif // DERIVEDCLASS_H
    

提交回复
热议问题