Copy Constructor

Posted on Updated on

Image result for Copy ConstructorCopy Constructor

A parameter constructor that has the object of the same class is termed as copy constructor. C# does not have a copy constructor for instances but it can be created. It is used to initialize the instance variables with the values of the existing objects i.e. the object of the class can be passed as an argument to the object of the same class as a parameter.
Purpose: The main objective of the copy constructor is to initialize the new instance with the values of the existing instance.

When to use: To audit the class
To retain the old values of the class
Example
Class Car{

//Instance Constructor
public Car(string nameofcar, intcarno, double carprice)
{
_NameofCar = nameofcar;
_CarNo = carno;
_CarPrice = carprice;
}

//Copy Constructor
public Car(Car objCar)
{
_NameofCar = objCar._NameofCar;
_CarNo = objCar._CarNo;
_CarPrice = objCar._CarPrice;
}
}

static void Main(string[] args)
{
Console.WriteLine(“Print Car Details From First Instance Object \n”);
Car objCar = new Car(“BMW Sedan”, 12345, 454555.45);
Console.WriteLine(“Name of a Car –> {0}”,objCar._NameofCar);
Console.WriteLine(“Car Plate Number –> {0}”, objCar._CarNo);
Console.WriteLine(“Price of a Car –> {0}”, objCar._CarPrice);
Console.WriteLine(“————————————- \n”);
Console.WriteLine(“Print Car Details From Copy Constructor \n”);
Car objCar2 = new Car(objCar);//Copy Data Stored in Objcar
Console.WriteLine(“Name of a Car –> {0}”, objCar2._NameofCar);
Console.WriteLine(“Car Plate Number –> {0}”, objCar2._CarNo);
Console.WriteLine(“Price of a Car –> {0}”, objCar2._CarPrice);
}

Complete Program
class Person
{
// Copy constructor.
public Person(Person previousPerson)
{
Name = previousPerson.Name;
Age = previousPerson.Age;
}

//// Alternate copy constructor calls the instance constructor.
//public Person(Person previousPerson)
// : this(previousPerson.Name, previousPerson.Age)
//{
//}

// Instance constructor.
public Person(string name, int age)
{
Name = name;
Age = age;
}

publicint Age { get; set; }

publicstring Name { get; set; }

publicstring Details()
{
return Name + ” is ” + Age.ToString();
}
}

classTestPerson
{
staticvoid Main()
{
// Create a Person object by using the instance constructor.
Person person1 = newPerson(“George”, 40);

// Create another Person object, copying person1.
Person person2 = newPerson(person1);

// Change each person’s age.
person1.Age = 39;
person2.Age = 41;

// Change person2’s name.
person2.Name = “Charles”;

// Show details to verify that the name and age fields are distinct.
Console.WriteLine(person1.Details());
Console.WriteLine(person2.Details());

// Keep the console window open in debug mode.
Console.WriteLine(“Press any key to exit.”);
Console.ReadKey();
}
}
// Output:
// George is 39
// Charles is 41

*****************************************************************************************************

For more details and queries please feel free to email, visit or call us. Wishing you the very best for all your future endeavors.
Helpline: 9814666333, 8699444666
Email: info@technocampus.co.in

Please fill the form and we shall contact you soon.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s