0

Hi I'm still learning and I have an error that I need some guidance with. I have a base class that I have a constructor in as folllows

class foo {  // Base Class

private:

    int century, year;      // define the members
    string firstName, lastName;

public:

foo(int i1, int i2, string s1, string s2){ // Define your constructor with default values
    year = i2;
    century = i1;
    firstName = s1;
    lastName = s2;
};

if I then create a derived class do I need to repeat the above constructor with any additional variables I tried just using the following -

class newFoo: public foo{  // Derived Class

private:

    int houseNumber;

public:

newFoo(int hn){             // Constructor
    houseNumber = hn;
};

I get the following error - error no matching function call to foo:foo()

Any guidance would be greatly appreciated.

Many Thanks

Brian

I tried just adding the new variables and a blank constructor. I don't know if I need to use the original constructor in fill in the derived class.

463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
BrianDawe
  • 23
  • 6
  • 1
    frankly, you are struggling with basics, you need a better book to introduce you to C++ https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list. This is also a good site, but I suppose difficult to use when you dont know what to search for yet: https://en.cppreference.com/w/cpp/language/constructor – 463035818_is_not_an_ai May 04 '23 at 12:21
  • the derived class does not need to define the base class constructor but it must call it to construct the base class subobject – 463035818_is_not_an_ai May 04 '23 at 12:22
  • 3
    The comment "Define your constructor with default values" does not agree with your constructor, which has no default values. – molbdnilo May 04 '23 at 12:24

2 Answers2

2

When you create a newFoo the foo that it inherits from must be created to. Like this

class newFoo: public foo{  // Derived Class

private:

    int houseNumber;

public:

newFoo(int hn) : foo(...) {             // Constructor
    houseNumber = hn;
};

You fill in the ... with whatever values you think are appropriate.

'I tried just adding the new variables and a blank constructor.' At the moment you don't have a blank constructor, if you think you need one then add it.

'I don't know if I need to use the original constructor in fill in the derived class.' This is possible as well.

john
  • 85,011
  • 4
  • 57
  • 81
2

Once you have declared a constructor which accepts some parameters in your base class, then all derived classes will also have to declare constructors so that they can delegate to the base class constructor.

This can be done in many different ways.

The most common scenario is as follows:

In addition to the extra parameters that it needs, the derived constructor will usually accept all of the parameters expected by the base constructor, so that it can pass those parameters to it. For example:

newFoo::newFoo( int i1, int i2, string s1, string s2, int hn )
    :foo( i1, i2, s1, s2 )
{
    houseNumber = hn;
}

Of course, this does not necessarily have to be done like that if the derived constructor can somehow compute or otherwise provide those parameters, as in the following example:

newFoo::newFoo( int hn )
    :foo( 42, 5, "27B-6", "covfefe" )
{
    houseNumber = hn;
}
Mike Nakis
  • 56,297
  • 11
  • 110
  • 142