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

Initialize Polygon from base with 4

I am currently very confused of what i am being asked for.

Im following the examples in the videos but it doesn't really go into bases, or how to initialize them. Or at least im not understanding it.

I think what i have is correct so far. but when i run the code, it says, "Bummer, did you create a class within a class? Square should be before or after the class Polygon."

I think i created the Square class correctly, so I'm confused why i would get this error.

If someone could assist that would be great.

Thanks

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 Polygon(int numSides) : base Square(SideLength)
        {
            SideLength = sideLength;
        }
    }



}

1 Answer

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there, Cliffton Caro ! It looks like you're off to a terrific start. But there's a few things going on here. First, the name of the constructor should be the same as the name of the class. So instead of public Polygon(int numSides) you should be using public Square(int sideLength). And it should take the length of the sides. Not the number of sides it should have. There's no scenario where a square has three sides or 8 sides. If it does, it's no longer a square :smiley:

It's the Polygon or "base class" that takes the number of sides. And a square should always send in 4 sides. If this class were Triangle it should send 3 into the Polygon class. A triangle is a polygon with three sides. A square is a polygon with four sides. You'll want to send that into base directly.

 class Square : Polygon  // Square inherits from Polygon. It is a type of polygon.
    {
        public readonly int SideLength;

        public Square(int sideLength) : base(4)  // pass in 4 because a square is always 4 sides
        {
            SideLength = sideLength;  // set the public field to whatever was passed in to determine how long the sides are
        }
    }

A square is a kind/type of polygon. So we set that up. The constructor of the square takes the side length. For example, at the moment I create a new square, I give it dimensions by including the side length. A square is defined as having four sides of equal length. Creating a Square(10) should give me a polygon with four sides each having a side length of 10. The unit of measurement is ambiguous here, but it has some length of 10 of some unit.

Hope this helps! :sparkles:

Thank you for the response! This helped me me understand what the heck was going on.