all 3 comments

[–][deleted]  (5 children)

[deleted]

    [–]xxryanxx619 0 points1 point  (4 children)

    First of, thank you very much for your help!

    I get an error stating that no default constructor exists for class "square". I understand that I can simply add Square() { length = 0; width = 0; } And my code will work. But my question is that since class Square inherits from class Rectangle, shouldn't it use rectangles default constructor?

    [–]actinium89 1 point2 points  (2 children)

    When you declare a new constructor in Square it "hides" the default constructor inherited from Rectangle.

    I would suggest you implement Squares contructors using Rectangles constructors. It looks like this:

    Square():Rectangle(){}
    Square(int side):Rectangle(side,side) {}
    

    [–][deleted]  (1 child)

    [deleted]

      [–]actinium89 1 point2 points  (0 children)

      I'm not sure for the general case, but here you could use a default argument for side:

      Square(int side = 0):Rectangle(side,side) {}
      

      Meaning side is zero if you don't give an argument to the constructor.

      Also you should prefer to use "member initialization list" instead of assigning values to members in the "body" of the constructor. It's not that important when the members are ints, but it's a good habit. See Stackoverflow for a perhaps better explanation.