Call an abstract method in a super class and implement it in a subclass in C ++?

In Java, you can write an abstract superclass with unrealized abstract methods and non-abstract methods that call abstract methods. Then the subclass implements abstract methods. When you then instantiate the subclass, the superclass uses the implementations in the subclass. How to do it in C ++?

Here is what I mean, but in Java:

SuperClass.java

public abstract class SuperClass { public SuperClass() { method(); } private void method() { unimplementedMethod(); } protected abstract void unimplementedMethod(); } 

Subclass.java

 public class SubClass extends SuperClass { public SubClass() { super(); } @Override protected void unimplementedMethod() { System.out.println("print"); } public static void main(String[] args) { new SubClass(); } } 

It would be great if you showed me how to do this in C ++. :)

+4
source share
5 answers

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() { // cannot call virtual functions from base constructor. } virtual ~SuperClass() { } // destructor. as Kerrek mentions, // classes that will be inherited from, // should always have virtual destructors. // This allows the destructors of derived classes // to be called when the base is destroyed. private: void method() { unimplementedMethod(); } protected: virtual void unimplementedMethod() = 0; // makes method pure virtual, // to be implemented in subclass } 

Subclass.h

 class SubClass : public SuperClass { public: SubClass() : SuperClass() { // how the superclass constructor is called. } // no need for "override" keyword, if the methd has the same name, it will // automatically override that method from the superclass protected: void unimplementedMethod() { std::cout << "print" << std::endl; } } 
+4
source

In C ++, you should never call virtual functions in the constructor, so it does not work as literally. It is best to use a separate member function

 class SuperClass { public: void action() { method(); } // not in the constructor, please virtual ~SuperClass() { } // always a virtual destructor when polymorphic protected: void method() { unimplementedMethod(); } private: virtual void unimplementedMethod() = 0; }; class SubClass : public SuperClass { private: virtual void unimplementedMethod() { std::cout << "print" << std::endl; } // no need to spell out the next couple of functions, but for your entertainment only public: SubClass() : SuperClass() { } virtual ~SubClass() { } }; 

Now for the call:

 int main() { SuperClass * p = new SubClass; // construct first... p->action(); // ... then invoke, after construction is complete delete p; // thank god for that virtual destructor! } 

The base constructor works before creating the derived class, so you cannot call any derived functions in the base constructor, and in particular, you cannot call any purely virtual functions.

Note that you have private and protected wrong way: the non-virtual access function must be protected , so it can be used in the entire hierarchy of classes, but the virtual implementation function must be private , since you only need to see it with the accessor function in same classroom. In short: secure non-virtual and private virtual.

(The usage example is a little far-fetched, since you usually don't use new or raw C ++ pointers.)

+3
source

In C ++, they are called pure virtual functions / methods.

Basically you press "= 0" at the end of the method:

 virtual doSomething() = 0; // pure virtual 

Search around SO for β€œC ++ pure virtual” and you will find many answers.

+1
source

You need to use virtual methods. The implementation works as follows:

 /* here MyBaseClass.h */ class MyBaseClass { public: MyBaseClass(void); ~MyBaseClass(void); void MyMethod(); protected: virtual void MyUnimplementedMethod() = 0; }; /* here MyIneritedClass.h */ class MyInheritedClass : public MyBaseClass { public: MyInheritedClass(void); ~MyInheritedClass(void); protected: virtual void MyUnimplementedMethod(); }; /* here the implementation of the method in the base class */ void MyBaseClass::MyMethod() { MyUnimplementedMethod(); } /* and here the implementation of the abstract method in the derived */ void MyInheritedClass::MyUnimplementedMethod() { _tprintf(L"Hello, world"); } 
+1
source

You declare the method as virtual :

snippet:

 class Parent{ public: virtual int methodA() {return methodB();}; // calls the abstract method virtual int methodB() = 0; // "=0" means pure virtual, not implemented in the base } class Child : public Parent{ public: virtual int methodB() { /* implementation */} } 

virtual means that the child can override the implementation, and the parent must then call the overridden implementation. Adding "= 0" to the declaration of the virtual method makes it pure virtual, i.e. The base does not have its own implementation and relies on the implementation of the child. Such a class cannot be created (i.e. an Abstract class).

0
source

Source: https://habr.com/ru/post/1396293/


All Articles