0

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;
    }
}

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

5 Comments

@Ela: but my line class hast 2 point objects , start and end , and both of them have y and x how can i pass them to start and end in Line ctor with :base(...)
@rNuǝɹɐ that's what I meant with it doesn't make sense, because usually a Line does not have X and Y, if you really want to have the properties from Point inherited, I cannot tell you what logic should be behind it
@Ela: the line dont have X and Y it has two Points Start Point and End Point and Every point has X and Y in diagram so i wrote point class so i can use it in line class
@rNuǝɹɐ ok if Line should not have X and Y, why do you inherit from Point? Inherit means Line will get the properties of Point... class Line : Point is the inheritance...
@Ela well that was logical answer , you'r right there is no need for inheritance , thanks

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.