+ 3
if the base class has a overloaded constructor, can we specify which to be called when creating a derived class from the bass class?
1 Answer
+ 6
Base class constructors are automatically called for you if they have no argument. If you want to call a superclass constructor with an argument, you must use the subclass's constructor initialization list.
For example:
class A {
public:
A(int foo) {
// do something with foo
}
};
class B: public A {
public:
B(int foo, int bar)
: A(foo) // Call the superclass constructor in the subclass' initialization list.
{
// do something with bar
}
};