Copy Constructor in c#.net and class diagram with example program code
Copy Constructor in c#.net and class diagram with example program code : Here dotnetportal9.blogspot.in blog gives you info about Copy Constructor in c#.net and class diagram and also we will give beatiful example program to understand Copy Constructor easyly
Copy Constructor in c#.net :-
Copy Constructor in with example program c#.net :
using System;
Copy Constructor in c#.net :-
- Copy Constructor is used to copy the data of existing object in to newly created object
- Example to Copy Constructor.
Class Diagram:-
Employee4
|
Int Empid
String Ename
String Eadderse
Int Eage
|
Public employee4()
Public employee4(Employee4 obj1)
Public void DisplayEmpData()
|
Copy Constructor in with example program c#.net :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace CAConstructors
{
class employee4
{
int Empid, Eage;
string Ename, EAddress;
public employee4()
{
Console.WriteLine("Enter Employee Details:-");
this.Empid=Convert.ToInt32(Console.ReadLine());
this.Ename=Console.ReadLine();
this.EAddress=Console.ReadLine();
this.Eage=Convert.ToInt32(Console.ReadLine());
}
public employee4(employee4 obj1)
{
this.Empid=obj1.Empid;
this.Ename=obj1.Ename;
this.EAddress=obj1.EAddress;
this.Eage=obj1.Eage;
}
public void DisplayEmpData()
{
Console.WriteLine("Employee id is :-" + Empid);
Console.WriteLine("Employee Name is :-" + Ename);
Console.WriteLine("Employee Address is :-" + EAddress);
Console.WriteLine("Employee age is :-" + Eage);
}
}
class CopyConstructor
{
static void Main(string[] args)
{
employee4 obj1=new employee4();
employee4 obj2=new employee4(obj1);
obj1.DisplayEmpData();
obj2.DisplayEmpData();
Console.ReadLine();
}
}
}
Output:-
- Without using copy constructor we can also the data of existing objects in to newly created object by writing a statement like
Empoyee4 obj2=obj1;
- when we use this statement by default all data fields data of obj1 will be copied into obj2.
- But if we want to restrict copying of some data fields data it is not possible for this purpose copy constrictor is suitable.
- Realtime:-
Copy Constructor is used in real time in following switchvations:-
- If an object contains data which is stored by getting from database (or) from some remote place over the network for newly created object if we want the same data it is preferable to use copy constructor instead of using getting the data from data base (or) remote place over the network.
0 comments:
Post a Comment