Hello,
For what purpose we are using constructor and destructor in a Program.....[Constructor Overloading]
We wanna use this mandatory...! Please clarify me....
Loading
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Posted Aug 6, 2013, 4:51 AM
public class Person
{
public string Name {get;set;}
public int Id{get;set;}
//Default constructor or silent constructor
public Person()
{
Console.WriteLine("Default constructor called");
}
public Person(string Name)
{
this.Name = Name;
Console.WriteLine("One arguemented constructor called");
Console.WriteLine("Person name is "+ this.Name);
}
public Person(string Name,int Id)
{
this.Name = Name;
this.Id = Id;
Console.WriteLine("Two arguemented constructor called");
Console.WriteLine("Person name is "+ this.Name + " id " + this.Id);
}
public virtual void draw()
{
Console.WriteLine("Base class draw method called");
}
~Person()
{
Console.WriteLine("Destructor called");
}
}
public class Employee : Person
{
public override void draw()
{
Console.WriteLine("Child class draw method called");
}
}
Person p = new Person();
p.draw();
Employee emp = new Employee();
emp.draw();
The person object will call the draw method from person and Employee object method will call the draw method from employee class.
To override the method in child class, in the base class that method should be marked as virtual then you can override it in the child class.
If the method is not marked as virtual in the base class, still can override it by using "new" keyword instead of override.
public new void draw()
{
Console.WriteLine("Child class draw method called");
}
VulpesPosted Aug 6, 2013, 9:19 AM
public new void draw()
try removing 'new' and then study the compiler output when you rebuild.
Similarly, add this method to the Employee class and look at the compiler output:
public new void draw2()
{
}
VijayPosted Aug 6, 2013, 9:08 AM
Vulpes plz mention some examples what u said
""
'new' to hide an inherited member is never necessary.
However, if you don't use it when it should be used, then the compiler will issue a warning.
Similarly, if you use it when it shouldn't be used, then the compiler will warn you about that as well.
""
VulpesPosted Aug 6, 2013, 8:59 AM
However, if you don't use it when it should be used, then the compiler will issue a warning.
Similarly, if you use it when it shouldn't be used, then the compiler will warn you about that as well.
However, because you can't both hide and override an inherited member at the same time, the combination 'new override' is not allowed and gives a compiler error.
VijayPosted Aug 6, 2013, 8:48 AM
Posted Aug 6, 2013, 7:12 AM
public class Person
{
public string Name {get;set;}
public int Id{get;set;}
//Default constructor or silent constructor
public Person()
{
Console.WriteLine("Default constructor called");
}
public Person(string Name)
{
this.Name = Name;
Console.WriteLine("One arguemented constructor called");
Console.WriteLine("Person name is "+ this.Name);
}
public Person(string Name,int Id)
{
this.Name = Name;
this.Id = Id;
Console.WriteLine("Two arguemented constructor called");
Console.WriteLine("Person name is "+ this.Name + " id " + this.Id);
}
public void draw()
{
Console.WriteLine("Base class draw method called");
}
~Person()
{
Console.WriteLine("Destructor called");
}
}
public class Employee : Person
{
public new void draw()
{
Console.WriteLine("Child class draw method called");
}
}
Person p = new Person();
p.draw();
Employee emp = new Employee();
emp.draw();
The derived class method is preceded with the new keyword, the method is defined as being independent of the method in the base class. The override modifier extends the base class method, and the new modifier hides it.
To know more about on the new and override keyword,
http://msdn.microsoft.com/en-us/library/ms173153.aspx
VijayPosted Aug 6, 2013, 6:50 AM
Is it access modifier ? give samples sir......
public new void draw() { }
VijayPosted Aug 6, 2013, 4:43 AM
Give example for method loading and overriding using class
Posted Aug 6, 2013, 3:55 AM
You can override base class methods in the child class, but not the constructors.
Posted Aug 6, 2013, 3:44 AM
Person person = new Person(); //Which calls default constructor
person = new Person("Murali"); //Which calls single parameterized constructor
person = new Person("Murali",10); //Which calls two parameterized constructor
VijayPosted Aug 6, 2013, 3:40 AM
How can use method overriding and loading using constructor... plz give example
Posted Aug 6, 2013, 2:32 AM
public class Person
{
public string Name {get;set;}
public int Id{get;set;}
//Default constructor or silent constructor
public Person()
{
Console.WriteLine("Default constructor called");
}
public Person(string Name)
{
this.Name = Name;
Console.WriteLine("One arguemented constructor called");
Console.WriteLine("Person name is "+ this.Name);
}
public Person(string Name,int Id)
{
this.Name = Name;
this.Id = Id;
Console.WriteLine("Two arguemented constructor called");
Console.WriteLine("Person name is "+ this.Name + " id " + this.Id);
}
~Person()
{
Console.WriteLine("Destructor called");
}
}
//You can creat object for person in multiple ways,
Person person = new Person(); //Which calls default constructor
person = new Person("Murali"); //Which calls single argumented constructor
person = new Person("Murali",10); //Which calls two argumented constructor
//Destructors are used to dispose an object.To call the destructor, call GC.Collect();
System.GC.Collect();
Iftikar HussainPosted Aug 6, 2013, 1:44 AM
Please refer the below link
In Constructor
http://www.dotnetspider.com/forum/171656-constructor-uses-Purpose.aspx
In Destructor
http://www.c-sharpcorner.com/UploadFile/chandrahundigam/UnderstandingDestructors11192005021208AM/UnderstandingDestructors.aspx
Regards,
Iftikar