Tuesday 6 March 2012

Constructor Overloading


//Multiple Constructor
using System;

class Person
{
 // Declare Private Data
 int age;
 string name;
 static int Count=0;

 //Default Constructor
 static Person()
 {
//name="";
//age=0;
Count++;
 }

 //Parameter Constructor with one argument
 public Person(string s)
 {
name=s;
age=0;
 }

 //Parameter Constructor with one argument
 public Person(int n)
 {
name="";
age=n;
 }

 //Parameter Constructor with two argument
 public Person(int n,string s)
 {
name=s;
age=n;
 }

 //Public method to Get/Print Values of Private Data
public void GetData()
{
Console.WriteLine("Name: {0} , Age: {1} \n",name,age);
}
}

class Program
{
static void Main()
{
//Person obj1=new Person();
Person obj2=new Person(26);
Person obj3=new Person("Ankur");
Person obj4=new Person(26,"Ankur Bhatnagar");

//obj1.GetData();
obj2.GetData();
obj3.GetData();
obj4.GetData();
}
}

No comments:

Post a Comment