1
class SuperClass
{
public:

    SuperClass(int foo)
    {
        // do something with foo
    }
};

class SubClass : public SuperClass
{
public:

    SubClass(int foo, int bar)
    : SuperClass(foo)    
    {
        // do something with bar
    }
};

Does Subclass inherit the constructor of the superclass? What does its inherit and why i need to specify in sub class constructor the : SuperClass(foo)?

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
  • Please be careful with terminology: [constructor inheritance](http://stackoverflow.com/questions/9979194/what-is-constructor-inheritance) is not what you mean. – rubenvb Feb 05 '14 at 10:01
  • 1
    Constructors are not inherited: See http://stackoverflow.com/questions/347358/inheriting-constructors –  Feb 05 '14 at 10:01

5 Answers5

2

No SubClass does not inherit the constructor. E.g you cannot just call one of the constructors of SuperClass like you could with functions which are inherited.

But SubClass is a SuperClass defined by your inheritance. So in order to be a SuperClass it must not only create itself but also create the SuperClass.
This done using a constructor. And since you do not define a default constructor(one with no arguments) the compiler cannot know which constructor to call. This is the reason why you need to explicitly write which constructor the SubClass should use to create it's SuperClass.

mkaes
  • 13,781
  • 10
  • 52
  • 72
2

Constructors are not inherited, but they must be called.

If your base class has a default constructor then the derived class will automatically call this for you if you don't specify an alternative. The reason you have to specify a call to the base class constructor is because that class specifies that it must be initialized with an integer, and as a derived class it's your responsibility to ensure that the base class is initialized correctly.

Sean
  • 60,939
  • 11
  • 97
  • 136
0

No, the subclass does not "inherit" the base class constructor. The syntax is indeed similar, but its meaning is very different. What is going on is that the constructor of the subclass is initializing its internal copy of a superclass object before anything else. The reason you have to specify this manually, is because the base class may have different constructors which you might want to call with specific argumens for a certain subclass constructor.

rubenvb
  • 74,642
  • 33
  • 187
  • 332
0

No constructor is not inherited,you need to use :SuperClass(foo) because you are trying to pass an argument to base class constructor.even if you dont specify compiler makes sure that base class default constructor(zero argument constructor)is called when ever derived class objects craeted. But in your case you have not defined zero argument constructor.make sure you define it before removing ':SuperClass(foo)' !!!

prazor9
  • 137
  • 8
-1

Does Subclass inherit the constructor of the superclass?

yes, it does

What does its inherit and why i need to specify in sub class constructor the : SuperClass(foo)?

because your SuperClass does not provide default constructor (with arguments) and compiler wouldn't know how to construct the SuperClass object, you have to tell him that foo is foo

  • can you explain your last answer ? what do you mean by the compiler wouldnt know? – user3270704 Feb 05 '14 at 10:07
  • simply, you have a constructor in SuperClass which takes one argument, and while constructing SubClass object you have to call it, but it's not obvious what value should be passed to the `foo` parameter in SuperClass constructor If you had a default constructor (and no other) then it would be obvious for compiler to use it and how to call – Kamil Mikolajczyk Feb 05 '14 at 10:12