Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

C# C# Objects Inheritance Inheritance

FEI LI
FEI LI
828 Points

it says I didn't initialize 4 to numSides using base constructor

I don't understand

Polygon.cs
namespace Treehouse.CodeChallenges
{
    class Polygon
    {
        public readonly int NumSides;

        public Polygon(int numSides)
        {
            NumSides = numSides;

        }
    }
    class Square : Polygon
    {
        public readonly int SideLength;
        public Square (int sideLength): base(numSides);
        {
         numSides=4;
        }

    }


}

2 Answers

Balazs Peak
Balazs Peak
46,160 Points

You mixed this up a little bit ;)

The child class constructor should have the numSides in the parameter list. Think of it like this: you have two parameters, numSides and sideLength. You want to use both to construct the object, right? Well, you give the numSides to the base constructor, and it will do the job with it. In the body of the child class constructor function, you will handle the sideLength only.

Also, please note that 4 should be given to it when the function is called, not declared. You have to call this function somewhere, probably in the other page of the project ;)

The child class constructor should look something like this:

    public Square (int sideLength, int numSides) : base(numSides);  //you pass the numSides to parent constructor!
    {
          // here, you should probably do something like this:
                   this.sideLength = sideLength;
    }
FEI LI
FEI LI
828 Points

class Square : Polygon { public readonly int SideLength; public Square (int sideLength): base(4) { SideLength=sideLength; }

}

It worked!. Square is the Polygon subclass, like you said, I don't need to worry about the numSides, cause I use the base constructor to pass it to the parent class.

Thank you very much!

Balazs Peak
Balazs Peak
46,160 Points

You are most welcome! Feel free to have me on facebook! Maybe later we can work together on projects or do something else!

facebook.com/puklibalazs

FEI LI
FEI LI
828 Points

Great! I am very new on this though, has a long way ahead....... lol

Balazs Peak
Balazs Peak
46,160 Points

it's ok, it'll be fun :D most of the time at least 8D