Default Constructors - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Default Constructors

razermanrazerman Posts: 2Member
Sorry I am new in C#. I like to reason behind this empty constructors
they have in the beginning of the program.
Like so:

public class Circle2 : Point2

// default constructor
public Circle2()
{
}

Comments

  • ing_gigioing_gigio Posts: 31Member
    : Sorry I am new in C#. I like to reason behind this empty constructors
    : they have in the beginning of the program.
    : Like so:
    :
    : public class Circle2 : Point2
    :
    : // default constructor
    : public Circle2()
    : {
    : }
    :
    Ur question is not so clear...but I'll try to answer as well ;)

    Once u instantiate a class, the first thing the compiler do is executing the constructor method.. By default it's defined a "default constructor" which is chosen every time no other constructor is defined or when u pass no arguments in the class instantiation.
    What it does is to assign the following values to the variables of the class:

    int = 0;
    chat = null character;
    bool = false;
    reference = null;

    When u inherit (like in ur code where Circle2 derives from Point2), the derived class acquires all of the methods of the father and this implies constructors too. But if in the father u did not implemented any constructor (and therefor there is only the default one), well... this can't be inherited. What u have to do is to explicitely define a default constructor for the derived class.
    This is precisely what u do with:
    [code]
    public Circle2{}
    [/code]
    This is an empty method which is called everytime a new Circle2 object should be instantiated using a constructor with no passed-parameters. What happens is that a constructor for Circle2 is found (so the compilers doesn't notify an error) and executed. As long as it's empty it will not do anything... so the variables in Circle2 will still have to be initialized. In order to accomplish such a goal, the compiler will call the default constructor from the parent class.

    this uses the same mechanisms u use when u write something like
    [code]
    public Circle2(int Var1, int Var2, char Char1) : base (Var1, Var2)
    {
    Char1 = "a";
    }
    [/code]

    where Var1 e Var2 are variables declared in a parent class and initalized in a constructor of this parent class looking like this:

    [code]
    public ParentClass(int Var1, int Var2)
    {
    Var1 = 10;
    Var2 = 12;
    }
    [/code]
  • BaldusariusBaldusarius Posts: 162Member
    : When u inherit (like in ur code where Circle2 derives from Point2),
    : the derived class acquires all of the methods of the father and this
    : implies constructors too.


    This is not correct. Constructors are *not* inherited, which is the reason they cannot be declared virtual. The following will not compile:[code]
    public class MyClassBase
    {
    public MyClassBase(){}

    public MyClassBase(string s)
    {
    Console.WriteLine("Howdy from MyClassBase");
    }
    }

    public class MyClass : MyClassBase
    {
    public MyClass(){}
    }

    public Class Test
    {
    public static void Main()
    {
    // compilation will fail here with this message:
    // No overload for method 'MyClass' takes '1' arguments
    // even though MyClass's parent class implements this constructor
    MyClass myClass = new MyClass("testing");
    }
    }[/code]

    The inheriting class must overload the constructor and call its parent's constructor explicilty.
Sign In or Register to comment.