Hi Guys
NP44 constructor
C# Corner article website address is given below. I wish to know what is the purpose of having public Shape() constructor in the Shape class. Because program is working well without this constructor. Please anyone knows explain.
http://www.c-sharpcorner.com/UploadFile/pcurnow/polymorphcasting06222007131659PM/polymorphcasting.aspx
Thank you
public class Shape
{
protected int m_xpos;
protected int m_ypos;
/*
public Shape()
{
}
*/
public Shape(int x, int y)
{
m_xpos = x;
m_ypos = y;
}
public virtual void Draw()
{
Console.WriteLine("Drawing a SHAPE at {0},{1}", m_xpos, m_ypos);
}
}
Posted Sep 25, 2007, 9:48 AM
Thank you very much for your explanation, Alan
AlanPosted Sep 25, 2007, 8:45 AM
Hi Maha,
There's actually no point in declaring such a constructor because, if you don't supply one, then the system automatically provides a default public, parameterless, 'do nothing' constructor for you.
However, it does no harm to provide such a constructor and the author of that article may have thought he'd do so just to show that no special code was needed to construct a Shape object.
Incidentally, one thing that all constructors do - whether system provided or not - is to call the base class constructor. In this case System.Object's constructor is called because that is the base class of Shape. The only exception to this is System.Object itself which, of course, sits at the top of the class hierarchy and therefore has no base class.