Default Constructors - Programmers Heaven
It looks like you're new here. If you want to get involved, click one of these buttons!
C and C++
Delphi and Kylix
DirectX Game dev
Newbie Game Programmers
Database & SQL
Sound & Music
FreeLance Software City
Join the Team
Comments on this site
New programming languages
Off topic board
Mobile & Wireless
Operating Systems & Platforms
Embedded / RTOS
Windows CE & Pocket PC
Quality & Testing
Active Server Pages
HTML & WEB-Design
WEB-Services / SOAP
Sorry I am new in C#. I like to reason behind this empty constructors
they have in the beginning of the program.
public class Circle2 : Point2
// default constructor
Share on Facebook
: 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:
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
public Circle2(int Var1, int Var2, char Char1) : base (Var1, Var2)
Char1 = "a";
where Var1 e Var2 are variables declared in a parent class and initalized in a constructor of this parent class looking like this:
public ParentClass(int Var1, int Var2)
Var1 = 10;
Var2 = 12;
Share on Facebook
: 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(string s)
Console.WriteLine("Howdy from MyClassBase");
public class MyClass : MyClassBase
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");
The inheriting class must overload the constructor and call its parent's constructor explicilty.
Share on Facebook
Programmers Heaven articles
Programmers Heaven files
Programmers Heaven uploaded content
Programmers Heaven C Sharp ebook
Operated by CommunityHeaven LLC
- All rights reserved.