C # self-study Road 07

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.