C++: Create abstract class with abstract method and override the method in a subclass

前端 未结 3 1525
囚心锁ツ
囚心锁ツ 2021-01-01 16:18

How to create in C++ an abstract class with some abstract methods that I want to override in a subclass? How should the .h file look? Is there a .cpp

3条回答
  •  灰色年华
    2021-01-01 16:27

    In C++ you use the keyword virtual on your routines, and assign =0; into them. Like so:

    class GameObject {
    public:
        virtual void update()=0;
        virtual void paint(Graphics g)=0; 
    
    }
    

    Having a virtual method with a 0 assigned into it automagically makes your class abstract.

提交回复
热议问题