7. Constructors and destructors
In C + +, a constructor is the initialization of an instance of a class, an object (opening up memory space), and the constructor is destroying the object (Reclaim space).
Namespace ConsoleApplication7
{
Class Program
{
static void Main (string[] args)
{
Student s = new Student ();
Student1 S1 = new Student1 (12);
Student2 s2 = new Student2 ("Guojun");
Console.WriteLine ("Name: {0}, Age: {1}", s2.name,s2.age);
Console.WriteLine ("Name: {0}", s1.age);
Console.WriteLine ("Name: {0}, Age: {1}", s.name,s.age);
}
}
public class Student
{
public int age;
public string name;
Public Student () {}
Public Student (int a)
{
This.age = A;
}
Public Student (int A, string n)
{
This.age = A;
THIS.name = n;
}
~student ()
{
Console.WriteLine ("Student's destructor is called");
}
}
public class Student1:student
{
Public Student1 () {}
Public Student1 (int a)
{
This.age = A;
}
Public Student1 (int A, string n)
{
This.age = A;
THIS.name = n;
}
~student1 ()
{
Console.WriteLine ("Student1 's destructor is called");
}
}
public class Student2:student
{
Public Student2 () {}
Public Student2 (int a)
{
This.age = A;
}
Public Student2 (int A, string n)
{
This.age = A;
THIS.name = n;
}
~student2 ()
{
Console.WriteLine ("Student2 's destructor is called");
}
}
}
Note the time: 1. Constructors and destructors cannot be inherited, and constructors can be overloaded.
2. The destructor call order is the opposite of the constructor function.
This article from "Very blog" blog, reproduced please contact the author!
C # self-study Road 07