C + + Constructor _ destructor

Source: Internet
Author: User

What is a destructor

If the constructor is the first cry of the object to the world, then the destructor is the final word before the object dies.

Destructors are automatically called when the object is destroyed, and the task is to return the resources of the system.

Characteristics:

1. If there is no custom destructor, the system automatically generates

2. Destructors are automatically called when objects are destroyed

3. Destructors have no return value, no parameters, and cannot be overloaded

Defining formats

  ~ Class name ()

For example:

class student{public:    Student () {cout<<"Student"< <Endl;}     ~student () {cout<<"~student"<<Endl;} Destructors are not allowed to add any arguments to private:    string  m_strname;}
Necessity of defining destructors
class student{public:    newchar[+];}     ~student () {m_pname = new char[20];} Private :     Char *m_pname;}

When we define m_pname this variable, we do not use a string array, but a pointer of type char, and in the definition of the function we point to the memory in a heap. When this space is used, if it is not released, it will cause a memory leak. The best time to release this memory is before the object is destroyed.

code example
#include <iostream>#include<stdlib.h>#include<string>#include"Teacher.h"using namespacestd;/*************************************************************************teacher class custom destructor ordinary way instantiation object, Whether the destructor is automatically called when the object is destroyed by copying the constructor to instantiate the object and whether the destructor data member is automatically called when the object is destroyed: Name Age member function: wrapper function for data member ************************************* ************************************/classteacher{ Public:    //declaring constructors, parameters given default valuesTeacher (stringName ="CJJ",intAge = A); //declaring copy ConstructorsTeacher (ConstTeacher &tea); //declaring destructors~Teacher (); //declare member functions and list all the member functions    voidSetName (string_name); stringGetName (); voidSetage (int_age); intgetage ();Private:    stringM_strname; intM_iage; };//define constructors, use initialization lists, initialize parameters of constructorsTeacher::teacher (stringNameintAge ): M_strname (name), M_iage (age) {cout<<"Teacher (String name,int age)"<<Endl;}//Defining copy ConstructorsTeacher::teacher (ConstTeacher &tea) {cout<<"teacher::teacher (const Teacher &tea)"<<Endl;}//defining DestructorsTeacher::~teacher () {cout<<"proving that the destructor was called"<<Endl;}//out-of-class definitions, function bodies that write out member functionsvoidTeacher::setname (string_name) {M_strname=_name;}stringTeacher::getname () {returnM_strname;}voidTeacher::setage (int_age) {M_iage=_age;}intTeacher::getage () {returnm_iage;}intMainvoid) {Teacher T1; //instantiating an object from the stackTeacher *p =NewTeacher ();//instantiating an object from the heap    Deletep;    Teacher T2 (T1); System ("Pause"); return 0;}

C + + Constructor _ destructor

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.