derived class is abstract class when pure virtual function in base class in c++ #cppprogramming
Understanding Abstract Classes and Function Signatures in C++
The Issue with Abstract Class B
- The problem arises because class B is an abstract class, preventing object creation. This is due to the
showfunction being marked as pure virtual and not implemented in the derived class.
- It's important to note that even if a function appears similar, its signature must match exactly for it to be considered valid. In this case, the signatures differ.
Compiling with Correct Function Signature
- The initial error was identified as class B being abstract due to mismatched function signatures. Specifically, one version of
showtakes an integer while another does not.
- To resolve this issue, modifying the function signature in the derived class to accept an integer (e.g., passing 10 as an argument) allows successful compilation.
Key Takeaways on Function Signatures
- Always ensure that both the name and parameters of a function match exactly when overriding in derived classes; return type differences do not affect signature validity.
- Understanding these nuances is crucial for effective programming in C++, particularly when dealing with inheritance and polymorphism.