CPP Classes and constructor

Can we avoid the constructor of the base class being called by the derived class?

#include <iostream>
using namespace std;
class BaseClass {
      cout<<"Parent Class\n";
class DerivedClass: private BaseClass{
   DerivedClass() {
      cout<<"Child Class";
int main() {
   DerivedClass obj = DerivedClass();
   return 0;

The output is
Parent Class
Child Class

But I don’t want “Parent Class” to be chosen.

If you need to override the parent constructor, that is a sign that you have designed your class hierarchy incorrectly. You cannot not initialize the base class in C++.

See also the discussion here: https://stackoverflow.com/questions/3156597/override-or-remove-an-inherited-constructor

1 Like