0

I just want to check in order to inherit a constructor from the parent class is this the correct way to do it by using object composition?

and I'm getting this errors and they are all referring to my constructors.

    C:\Users\user\AppData\Local\Temp\cc0CYtVR.o:SquareImp.cpp:(.text+0x16): undefine
d reference to `vtable for Square'
C:\Users\user\AppData\Local\Temp\cc0CYtVR.o:SquareImp.cpp:(.text+0x79): undefine
d reference to `vtable for Square'
C:\Users\user\AppData\Local\Temp\cc0CYtVR.o:SquareImp.cpp:(.text$_ZN6SquareD1Ev[
Square::~Square()]+0xb): undefined reference to `vtable for Square'
collect2: ld returned 1 exit status

Sorry this is my new script as I've made try rewriting them

ShapeTwoD.h

class ShapeTwoD
{
    protected:
        string name, warpSpace;
        bool containsWarpSpace;
        int xCord, yCord, length, breath;
    public:
        //constructor
        ShapeTwoD();
        ShapeTwoD(string, bool);
};

ShapeTwoD.cpp

ShapeTwoD::ShapeTwoD()
{
    string name = "";
    bool containsWarpSpace = true;
}

ShapeTwoD::ShapeTwoD(string ShapeName, bool warpspace)
{
    name = ShapeName;
    containsWarpSpace = true;
}

square.h

class Square:public ShapeTwoD
{
    private:
        int xVal,yVal;
        int newlength, newbreath;
        double area;

    public:
        Square();
        Square(string, bool, int, int);
};

square.cpp

Square::Square()
{
    xVal = 0;
    yVal = 0;
}

  Square::Square(string ShapeName, bool warpspace, int xval, int yval):ShapeTwoD(ShapeName, warpspace), xVal(xval), yVal(yval)
    {
    xVal = xval;
    YVal = yval;
    cout << "test test" << endl;
}

int main()
{
    Square square;
    cout << "hello world" << endl;
}
9
  • What have you tried? For example, you can print statements in the constructor and determine in what order they're built. Commented Nov 2, 2013 at 6:04
  • Looks good. There is a site in the StackExchange network for code review, it may be better suited there. Commented Nov 2, 2013 at 6:05
  • OK, it seems you've edited your question. How do you declared those classes? Commented Nov 2, 2013 at 6:10
  • Ahh, but it then isn't a question about specifics of construction order. It is a question of my code won't compile. Commented Nov 2, 2013 at 6:12
  • @MM I've already updated the question but I'm still getting the same compilation error.. why is this so? Commented Nov 2, 2013 at 6:25

1 Answer 1

2

Yes, and also put xVal and yVal in the initializer list too:

Square::Square(string ShapeName, bool square, int xval, int yval):
      ShapeTwoD(ShapeName, square), xVal(xval), yVal(yval)
{
}

And construct base class for Square() too:

Square::Square() : ShapeTwoD(..., ...)
{
}
Sign up to request clarification or add additional context in comments.

2 Comments

sorry why do I have to include "xVal(xval), yVal(yval)"?
You don't have to, your way works, but this way is standard and has good advantages specially for initializing objects.

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.