1

I have an assignment that requires two classes to be derived from a base class. I am having issues getting the derived classes to call the base class constructor and successfully set the inherited variables. I recreated the issue with a dummy program for simplicity since the assignment is much longer.

#include <iostream>

class ParentClass {
    public:
            ParentClass(int theField1, int junk);
            ParentClass() {}
            virtual void printField();
            virtual void setField(int nf);

    protected:

            int field1;
};

class ChildClass : public ParentClass {
    public:
            ChildClass(int theField1);
            void printField();
            void setField(int nf);
};

ParentClass::ParentClass(int theField1, int junk) {
    field1 = theField1;
}

ChildClass::ChildClass(int theField1) {
    ParentClass::ParentClass(theField1, 3);
}

void ParentClass::printField() {
    std::cout << "The field = " << field1 << std::endl;
}

void ChildClass::printField() {
    ParentClass::printField();
    std::cout << "Some other stuff." << std::endl;
}

void ParentClass::setField(int nf) {
    field1 = nf;
}

void ChildClass::setField(int nf) {
    ParentClass::setField(nf);
}

int main() {
    ChildClass* myChild = new ChildClass(777);
    ChildClass child2(888);
    myChild->printField();
    child2.printField();

    myChild->setField(10);

    myChild->printField();

    child2.setField(20);
    child2.printField();

    return 0;
}

Running this gives me the following output:

The field = 0
Some other stuff.
The field = 4197296
Some other stuff.
The field = 10
Some other stuff.
The field = 20
Some other stuff.

Why do the first two attempts not work? Calling the constructor should be initializing the variables to the value passed as a parameter, but they are not actually set until I specifically call a mutator function. I tried a third class which used the parent mutator function in its constructor rather than the parent constructor:

class StepChild : public ParentClass {
    public:
            StepChild(int nf);
};

StepChild::StepChild(int nf) {
    ParentClass::setField(nf);
}

The object as defined in main:

    StepChild* step = new StepChild(30);
    step->printField();

The output:

The field = 30

Where am I going wrong that attempting to use the parent constructor is not properly initializing these variables?

I also tried changing the parent class to be not virtual, and it worked as well, so it doesn't appear to be an issue with the parent class.

3
  • How you are trying to accomplish this ParentClass::setField(nf);?? Commented Mar 18, 2014 at 4:45
  • I'm not sure what your question is, but that was just a test to see if I could construct a derived class using the setField function. All it does is set the variable field1 equal to the parameter. Commented Mar 18, 2014 at 4:51
  • Find an example of how to initialize a base class in a derived class. This pops up in all inheritance tutorials and chapters of books. Commented Mar 18, 2014 at 4:51

3 Answers 3

2

Use initialiser lists:

ParentClass::ParentClass(int theField1, int junk)
  : field1(theField1)
{ }

ChildClass::ChildClass(int theField1)
  : ParentClass(theField1, 3)
{ }

The following - from your code - creates a temporary ParentClass object and throws it away - that has no affect on the ChildClass object under construction:

 ParentClass::ParentClass(theField1, 3);   // temporary
Sign up to request clarification or add additional context in comments.

Comments

1

If you make the parameters match, you can also do it the c++11 way by putting

using ParentClass::ParentClass( int, int );

in your ChildClass class definition. It is the same as invoking the parent constructor from the ChildClass constructor initialiser list, but a little less esoteric.

Comments

0

Not sure but I find something wrong in the way you are calling base class constructor.

try this way to call base class constructor and see if the problem is solved.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.