KEMBAR78
Object Oriented Programming - Constructors & Destructors | PPTX
Q3M1 – OOP C# Dudy Fathan Ali S.Kom
Constructors & Destructors
Q3M1
Dudy Fathan Ali, S.Kom (DFA)
2015
CEP - CCIT
Fakultas Teknik Universitas Indonesia
Constructors
Q3M1 – OOP C# Dudy Fathan Ali S.Kom
Class UsingConstructor
{
public UsingConstructor()
{
Console.WriteLine(“Constructor
Invoked!”)
}
public void Display()
{
Console.WriteLine(“Display.”);
}
}
Output :
Class Program :
UsingConstructor uc =
new UsingConstructor();
Constructor Invoked!
A constructor is a special type of method that is invoked when you create a new
instance of a class. A constructor is used to initialize the members of the class. The
name of a constructor is the same as the name of the class that contains it.
~ OOP C# NIIT Courseware MMSv2
Constructors
Q3M1 – OOP C# Dudy Fathan Ali S.Kom
A constructor is a special type of method that is invoked when you create a new
instance of a class. A constructor is used to initialize the members of the class. The
name of a constructor is the same as the name of the class that contains it.
~ OOP C# NIIT Courseware MMSv2
Class UsingConstructor
{
public UsingConstructor()
{
Console.WriteLine(“Constructor
Invoked!”)
}
public void Display()
{
Console.WriteLine(“Display.”);
}
}
Output :
Class Program :
UsingConstructor uc =
new UsingConstructor();
uc.Display();
Constructor Invoked!
Display.
The Need of Constructors
Q3M1 – OOP C# Dudy Fathan Ali S.Kom
Class Calculator
{
int num1, num2, total;
public Calculator()
{
num1 = 10;
num2 = 20;
}
public void addNumber()
{
total = num1 + num2;
}
public void Display()
{
Console.WriteLine(“The total
is : ” + total);
}
}
Output :
Class Program :
Calculator objCal = new
Calculator();
objCal.addNumber();
objCal.Display();
The total is : 30
Constructors with Parameters
Q3M1 – OOP C# Dudy Fathan Ali S.Kom
Class ConstructParam
{
int num;
public ConstructParam(int x)
{
num = x;
}
public void Display()
{
Console.WriteLine(“The number
is : ” + num);
}
}
Output :
Class Program :
ConstructParam cp = new
ConstructParam(20);
cp.Display();
The number is : 20
A constructor can be modified to accept the user supplied values at runtime.
~ OOP C# NIIT Courseware MMSv2
Destructors
Q3M1 – OOP C# Dudy Fathan Ali S.Kom
Class UsingDestructors
{
public UsingDestructors()
{
Console.WriteLine(“Constructor
Invoked.”);
}
~UsingDestructors()
{
Console.WriteLine(“Destructor
Invoked.”);
Console.ReadLine();
}
}
Output :
Class Program :
UsingDestructors ud =
new UsingDestructors();
Constructor Invoked.
Destructor Invoked.
Destructors are special methods that are used to release the instance of a class
from memory. The purpose of the destructor is to perform the required
memory cleanup action.
~ OOP C# NIIT Courseware MMSv2
Destructors
Q3M1 – OOP C# Dudy Fathan Ali S.Kom
Class UsingDestructors
{
public UsingDestructors()
{
Console.WriteLine(“Constructor
Invoked.”);
}
~UsingDestructors()
{
Console.WriteLine(“Destructor
Invoked.”);
Console.ReadLine();
}
}
Output :
Class Program :
UsingDestructors ud =
new UsingDestructors();
Constructor Invoked.
Destructor Invoked.
The programmer has no control on when to call the destructor. The .NET
Framework automatically runs the destructor to destroy objects in the
memory.
~ OOP C# NIIT Courseware MMSv2
Tilde Symbol
Q3M1 – OOP C# Dudy Fathan Ali S.Kom
Thank You!
Dudy Fathan Ali S.Kom
dudy.fathan@eng.ui.ac.id

Object Oriented Programming - Constructors & Destructors

  • 1.
    Q3M1 – OOPC# Dudy Fathan Ali S.Kom Constructors & Destructors Q3M1 Dudy Fathan Ali, S.Kom (DFA) 2015 CEP - CCIT Fakultas Teknik Universitas Indonesia
  • 2.
    Constructors Q3M1 – OOPC# Dudy Fathan Ali S.Kom Class UsingConstructor { public UsingConstructor() { Console.WriteLine(“Constructor Invoked!”) } public void Display() { Console.WriteLine(“Display.”); } } Output : Class Program : UsingConstructor uc = new UsingConstructor(); Constructor Invoked! A constructor is a special type of method that is invoked when you create a new instance of a class. A constructor is used to initialize the members of the class. The name of a constructor is the same as the name of the class that contains it. ~ OOP C# NIIT Courseware MMSv2
  • 3.
    Constructors Q3M1 – OOPC# Dudy Fathan Ali S.Kom A constructor is a special type of method that is invoked when you create a new instance of a class. A constructor is used to initialize the members of the class. The name of a constructor is the same as the name of the class that contains it. ~ OOP C# NIIT Courseware MMSv2 Class UsingConstructor { public UsingConstructor() { Console.WriteLine(“Constructor Invoked!”) } public void Display() { Console.WriteLine(“Display.”); } } Output : Class Program : UsingConstructor uc = new UsingConstructor(); uc.Display(); Constructor Invoked! Display.
  • 4.
    The Need ofConstructors Q3M1 – OOP C# Dudy Fathan Ali S.Kom Class Calculator { int num1, num2, total; public Calculator() { num1 = 10; num2 = 20; } public void addNumber() { total = num1 + num2; } public void Display() { Console.WriteLine(“The total is : ” + total); } } Output : Class Program : Calculator objCal = new Calculator(); objCal.addNumber(); objCal.Display(); The total is : 30
  • 5.
    Constructors with Parameters Q3M1– OOP C# Dudy Fathan Ali S.Kom Class ConstructParam { int num; public ConstructParam(int x) { num = x; } public void Display() { Console.WriteLine(“The number is : ” + num); } } Output : Class Program : ConstructParam cp = new ConstructParam(20); cp.Display(); The number is : 20 A constructor can be modified to accept the user supplied values at runtime. ~ OOP C# NIIT Courseware MMSv2
  • 6.
    Destructors Q3M1 – OOPC# Dudy Fathan Ali S.Kom Class UsingDestructors { public UsingDestructors() { Console.WriteLine(“Constructor Invoked.”); } ~UsingDestructors() { Console.WriteLine(“Destructor Invoked.”); Console.ReadLine(); } } Output : Class Program : UsingDestructors ud = new UsingDestructors(); Constructor Invoked. Destructor Invoked. Destructors are special methods that are used to release the instance of a class from memory. The purpose of the destructor is to perform the required memory cleanup action. ~ OOP C# NIIT Courseware MMSv2
  • 7.
    Destructors Q3M1 – OOPC# Dudy Fathan Ali S.Kom Class UsingDestructors { public UsingDestructors() { Console.WriteLine(“Constructor Invoked.”); } ~UsingDestructors() { Console.WriteLine(“Destructor Invoked.”); Console.ReadLine(); } } Output : Class Program : UsingDestructors ud = new UsingDestructors(); Constructor Invoked. Destructor Invoked. The programmer has no control on when to call the destructor. The .NET Framework automatically runs the destructor to destroy objects in the memory. ~ OOP C# NIIT Courseware MMSv2 Tilde Symbol
  • 8.
    Q3M1 – OOPC# Dudy Fathan Ali S.Kom Thank You! Dudy Fathan Ali S.Kom dudy.fathan@eng.ui.ac.id