Inheritance and Constructors

closed account (DGTDE3v7)
Hello everyone,

I am trying to complete a small C++ project but I have some questions and problems. This project is on inheritance and constructors.

So I have those files :

I already have completed the different classes but I don't understand how to complete the constructors. I tried to use Member initializer list but it doesn't work.
Plus I have a problem because in the definition of the Employer class, I have a error ('unknown Employee') because I am using Employee to define Employer features.

I hope that you can help me. I just started to learn C++
Last edited on
The constructor is usually a means to set things up for an object upon instantiation.

So if I instantiate a Person object like the follow:
Person person00("Bob");

It would make sense there now exists a person named Bob, and since this is a user-defined constructor that took on a string, your constructor for Person would look like:
Person::Person(std::string name) {
  this->name = name;

I use the keyword this to refer to the string called "name" belonging to the current object. Try to avoid using same name variables, as it will confuse you.
I tried to use Member initializer list but it doesn't work.

There is no "it doesn't work" in programming.
There is code and exact errors that it does produce.

What errors does this generate?
Employee::Employee( std::string name, int salary )
 : Person( name ), salary( salary )
You need to post your code.

From what you've said it sounds like you trying to do circular definitions. i.e. Definition of A uses B. Definition of B uses A.

The way around that is to use a forward declaration.
class B;  // forward declaration.  Tells compiler B is a class.
class A
{  B  b;  // Can't do this.  Compiler doesn't know the layout of B yet.
    B * b;  //  This is legal
class B
{  A a;  //  This is legal since A has been defined.

Topic archived. No new replies allowed.