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 trialIMran Karim
1,118 Pointsi dont understand this question
what's wrong with this code? I dont understand how to initialize the base class.
namespace Treehouse.CodeChallenges
{
class Polygon
{
public readonly int NumSides;
public Polygon(int numSides)
{
NumSides = numSides;
}
}
class Square : Polygon
{
public readonly int SideLength;
public Square:base (int sideLength)
{
SideLength = sideLength;
}
}
void main()
{
Polygon(4);
}
}
3 Answers
Steven Parker
231,184 PointsYou have the reference to base on the wrong side of the parameter.
Plus you need to pass the value 4 for the base constructor to use to initialize the number of sides:
public Square(int sideLength) : base(4)
You also have a stray main method definition that's not part of this challenge.
Allan Clark
10,810 PointsSyntax is a little off. The base() function allows you to specify the parameters for the constructor of the base class. Since a Square will always have 4 sides so you don't want the user of the Square class to have to explicitly code that every time. Try this:
class Square : Polygon
{
public readonly int SideLength;
public Square (int sideLength) : base(4)
{
SideLength = sideLength;
}
}
IMran Karim
1,118 Pointsthank you sir