0

I am new to C# and struggling through a tutorial printout. My intention is to inherit properties from the class Rectangle. However I am getting an error I cant resolve. any tips appreciated thank you Error C57036 There is no argument given that corresponds to the required formal parameter 'a' of 'Rectangle.Rectangle(double, double)'

{

{
    //private static void Main(string[] args)
    {
        Rectangle myRectangle = new Rectangle(23.5, 8.6);
        Console.WriteLine(myRectangle.getArea().ToString());
        Console.WriteLine(myRectangle.getParimeter().ToString());
        Square mySquare = new Square(15);
        Console.WriteLine(mySquare.getArea().ToString());
        Console.WriteLine(mySquare.getParimeter().ToString());
        Console.ReadLine();
    }
}

class Rectangle
{
    private double length; 
    private double width; 

    public Rectangle(double a, double b)
    {
        length = a;
        width = b;
    }

    public double getArea()
    {
        return this.length * this.width;
    }
    public double getParimeter()
    {
        return 2 * (this.length + this.width);
    }
}
 class Square : Rectangle
{ 
    public Square(double a)        
    {
        length = a;
    }
}

}

1 Answers1

3

Rectangle has no public constructor without arguments, so you need to declare your constructor and call suitable base constructor with arguments:

class Square : Rectangle
{ 
    public Square(double a) : base(a, a)     
    {

    }
}

This also solves the problem of accessing private property in your code.
This should solve compilation error.

However, in your case it looks like you can go right into the most popular Liskov Substitution Principle violation case if you make your class mutable.
In short, in case of mutability you cannot inherit Square from Rectangle even if it sounds logical in real world. It is not possible to implement changing width or height of rectangle so that it behaves correctly in all cases.

Read more about it here:

Is deriving square from rectangle a violation of Liskov's Substitution Principle?

P.S. Please, follow C# naming guidelines. Methods should have an UpperCamelCase name like GetArea or GetPeremiter. When you see a method with name starting with Get, then you probably want it to become a property:

class Rectangle
{
    private double _length; 
    private double _width; 

    public Rectangle(double a, double b)
    {
        _length = a;
        _width = b;
    }

    public double Area => this.length * this.width;

    public double Perimeter => 2 * (this.length + this.width);
}    
Yeldar Kurmangaliyev
  • 33,467
  • 12
  • 59
  • 101
  • Note that the OP's types are immutable (even if they don't use `readonly`), and so _don't_ violate LSP, at least not according to the argument espoused in the post you reference. – Peter Duniho Jul 27 '17 at 03:46
  • thank you Yeldar you resolved the errors but i don't understand. I am following course work which I guess was written incorrectly. I will save your post for reference when I learn more. – bronxsystem Jul 27 '17 at 04:09