DeadlyDagger
DeadlyDagger

Reputation: 609

Inheritance constructor error

so i have my child class Line that inherits form my parent class Point and i don't use my base class's constructor in my child class but i get this error :

'Shape.Point' does not contain a constructor that takes 0 arguments

this is my parent class:

public class Point
{

   public Point(int x, int y)
   {
       X = x;
       Y = y;
   }
   public int X { get; set; }
   public int Y { get; set; }
}

this is my child class:

public class Line : Point
{
   public Point Start { get; set; }
   public Point End { get; set; }
   public double Lenght { get; set; }

    public Line(Point start , Point end)
    {
        Start = start;
        End = end; ;
    }


    public double Calculate_Lenght()
    {
        Lenght = System.Math.Sqrt((End.X - Start.X) ^ 2 + (End.Y - End.X) ^ 2);
        return Lenght;
    }
}

Upvotes: 0

Views: 58

Answers (1)

MichaC
MichaC

Reputation: 13381

You extend with Point which has a ctor of

public Point(int x, int y)
{
   X = x;
   Y = y;
}

In Line you'll have to call this constructor or implement it, too

Either

public Line(int x, int y)
{
}

Or

public Line(Point start, Point end)
   : base (...)
{
}

While this seems to make no sense, I guess you really don't want to extend Line with Point maybe.

Or, if Point has some common functionality you want to inherit, you can give Point a parameter-less constructor.

Upvotes: 1

Related Questions