crackyourinterview.com


To improves our performance please Like Share Subscribe(Will boost us)

what is constructor and its types with example
Question Posted on 18 Jul 2011Home >> OOPS >> CLASS >> what is constructor and its types with example

Constructors are special methods that are used to initialize an object (instance) of a class. We donot have to define a return type for it because Constructors doesnot return anything. One of the most important thing of Constructors is that it has the same name as class. One important thing is that it must follows the access scope ( for exam private,protected, public, Internal and external).we can also overload Constructor. A class can have any number of constructors. Below is the one of simple example of Constructors
public class getproductpriceclass
{
  public getproductpriceclass(int x)
 {
  Console.WriteLine (x);
 }
}

There are many type Constructors as given Below:
(1)Default Constructor
(2)Parameterized constructor
(3)Private Constructor
(4)Static Constructor
(5)Copy Constructor

(1)Default Constructor:-A default constructor is a constructor in both C++ and C# that has no parameters or where it has parameters they are all defaulted.The compiler will generate its own default constructor for a class provided that no other constructors have been defined by the programmer. The compiler generated default constructor initializes the member variables of the class using their respective default constructors.
Example:-
class defaultcons // Has default parameterless constructor
{
 public int Value { get; set; }
}

class Program
{
 static void Main()
 {
  // Call the default constructor
  defaultcons defval= new defaultcons();
  defval.Value = 1;
  Console.WriteLine(defval != null);
 }
}

(2)Parameterized constructor:-A constructor with at least one parameter is called as parameterized constructor. Advantage of parameterized constructor is you can initialize each instance of the class to different values.
namespace Programname
{
 class Test1
 {
  int A, B;
  //Paremetrized Constructor
  public Test1(int X, int Y)
  {
   A = X;
   B = Y;
  }
  //Method to print
  public void Print()
  {
   Console.WriteLine("A = {0}\tB = {1}", A, B);
  }
 }
 class MainClass
 {
  static void Main()
  {
   Test1 T1 = new Test1(80, 40); //Parameterized Constructor is called
   T1.Print();
   Console.Read();
  }
 }
}
output:-
A = 80 B = 40

(3)Private Constructor:-A private constructor is a special instance constructor. It is commonly used in classes that contain static members only. If a class has one or more private constructors and no public constructors. Private constructor is used to restrict the class from being instantiated when it contains every member as static.
public class Myexample
{
 private Myexample() {}
 public static int counter;
 public static int IncrementCounter()
 {
  return ++counter;
 }
}
class MainClass
{
 static void Main()
 {
  // Myexample myObject = new Myexample(); // Error
  Myexample.counter = 100;
  Myexample.IncrementCounter();
  Console.WriteLine(Myexample.counter);
 }
}


(4)Static Constructor:-A static constructor is used to initialize any static data, or to perform a particular action that needs performed once only. It is called automatically before the first instance is created or any static members are referenced.A static constructor cannot be called directly and doesnot have access modifiers or have parameters. A static constructor is called automatically to initialize the class before the first instance is created or any static members are referenced. The user has no control on when the static constructor is executed in the program. Static constructors are also useful when creating wrapper classes for unmanaged code.
namespace ProgramExample
{
 class Testclass
 {
  public Testclass()
  {
   Console.WriteLine("First instance");
  }
  static Testclass()
  {
   Console.WriteLine("Static");
  }
 }
 class StaticConstructor
 {
  static void Main()
  {
   //Both Static Constructor and instance constructor are invoked for first instance.
   Testclass T1 = new Testclass();
   //Only First instance constructor is invoked.
   Testclass T2 = new Testclass();
   Console.Read();
  }
 }
}
output:-Static
First instance
First instance

(5)Copy constructor:-A parameterized constructor that contains a parameter of same class type is called as copy constructor. Main purpose of copy constructor is to initialize new instance to the values of an existing instance.
namespace Programexample
{
 class Testclass
 {
  int A, B;
  public Testclass(int X, int Y)
  {
   A = X;
   B = Y;
  }
  //Copy Constructor
  public Testclass(Testclass T)
  {
   A = T.A;
   B = T.B;
  }
  public void Print()
  {
   Console.WriteLine("A = {0}\tB = {1}", A, B);
  }
 }
 class CopyConstructor
 {
  static void Main()
  {
   Testclass T2 = new Testclass(80, 90);
   //getting copy constructor
   Testclass T3 = new Testclass(T2);
   T2.Print();
   T3.Print();
   Console.Read();
  }
 }
}
output:-
A = 80  B = 90
A = 80  B = 90
0
0




.


Most Visited Questions:-

Deep Learning Questions Answers
Below are the different Deep Leaning Questions and answer a More...

What is Gulpjs and some multiple choice questions on Gulp
Gulpjs is an open source whihc helps in building Javascript More...

Continuous Integration Questions Answers
Below are the 20 odd questions for CI or Continuous Integra More...

Derived relationships in Association Rule Mining are represented in the form of __________.
Derived relationships in Association Rule Mining are repres More...

Microservices Architecture Questions Answers
Below are the different questions on Microservices Architec More...




Other Important Questions

difference between classes and struct

Function or Method Overload Ways with Example

Types of Inheritance OOPS

Can we create a instance of abstract class object

what is constructor and its types with example






@2014-2022 Crackyourinterview (All rights reserved)
Privacy Policy - Disclaimer - Sitemap