iNoah iNoah -4 years ago 141
C++ Question

Calling the Constructor for the Abstract Base class in C++

I know that the if I have an abstract class then I cannot create an object of abstract class type. But Suppose "Base" is a base class and "Derived" is a derived class.
In base class I have one member variable name.

Base.h
Base(string name = "");

Base.cpp
Base(string theName){name = theName);


Isn't this creating an object ??

In the Derived class I have member variable age.
Now in the derived class default constructor

Derived.h
Derived(string name = "", int theAge = 0);

Derived.cpp
Derived(string theName, int theAge):Base(theName) { age = theAge }


Isn't that also calling the default constructor for the Base ??
So, am I allowed to call a parametrized constructor but not the default one ?

One more thing, if I have another function in the Base class other than the pure function, how can I call that function if I am not allowed to create an object for the Base class ?

Thanks in advance!

Answer Source

I know that the if I have an abstract class then I cannot create an object of abstract class type.

With one exception: An object of an abstract class can be created as a subobject of a derived-class object.

In fact, it must be created in that situation, because that's just how inheritance works in C++. An object of a derived class contains an object of the base class (or generally, objects of the base classes, because let us not forget that C++ also supports multiple inheritance).

The standard says this very clearly in ยง10.4/1:

An abstract class is a class that can be used only as a base class of some other class; no objects of an abstract class can be created except as subobjects of a class derived from it.

There you have it: instances of abstract classes may exist within this restriction. Therefore,

Base(string theName){name = theName);

Isn't this creating an object ??

It is a constructor like any other; it is used to create an object. The fact that the class is abstract doesn't matter, given the rule above.

Derived(string theName, int theAge):Base(theName) { age = theAge }

Isn't that also calling the default constructor for the Base ??

Yes, it is.

So, am I allowed to call a parametrized constructor but not the default one ?

A constructor in which all arguments are defaulted is a default constructor.

One more thing, if I have another function in the Base class other than the pure function, how can I call that function if I am not allowed to create an object for the Base class ?

If it's a public function, then anyone who has a derived-class object can call it (this includes the derived class itself). Additionally, anyone who accesses the derived-class object via a pointer or reference to the base class can call it. And of course, the base class itself can call it, too.

If it's a protected function, then the derived class and potential further derived classes can call it.

If it's private, then the base class itself can call it.

As we have established above, you do technically create an object for the base class, as a subobject inside of the derived-class object. So principally, the situation is not special just because the base class is abstract.


The thing to keep in mind here is that the technicalities of the language are defined so that all basic object-oriented features just work as expected. You don't have to worry about these things in your daily programming business.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download