In general, what you are looking for is the virtual . In short, virtual announces its intention to override this method. Please note that such a method may still have an implementation - virtual just makes it overrideable. To declare an "abstract method", you can say to declare an intention, please provide an implementation in a derived class with = 0 , as shown below. Such methods are called pure virtual in C ++.
However, there are some warnings you should keep an eye out for. As indicated in the comment below, you called method() from the SuperClass constructor. Unfortunately, this is not possible in C ++ due to the order in which the objects are built.
In C ++, a constructor of a derived class immediately calls its superclass constructor before distributing its elements or executing the constructor body. Thus, the members of the base class are formed first, and the members of the derived class are built last. Calling a virtual method from the base class will not work as you would expect in Java, since the derived class has not yet been created, and thus the virtual methods have not yet been redirected to derived implementations. Hope this makes sense.
However, calling the method() of the SuperClass object after creation will work as you expect: it will call a virtual function that prints out.
class SuperClass { public: SuperClass() {
Subclass.h
class SubClass : public SuperClass { public: SubClass() : SuperClass() {
source share