Deep parsing of constructors and destructors in C + + _c language

Source: Internet
Author: User

constructor function:
Executes automatically when a class instantiates an object, initializing the data in the class. Constructors can be from load, there can be multiple, but only one default constructor.

destructor:
A function that takes some action before undoing the memory occupied by the object. Destructors cannot be overloaded, there can be only one.

The order in which constructors and destructors are called:
The reconstructed structure is first constructed, then the first is reconstructed. It is equivalent to a stack, advanced later out.

Copy Code code as follows:

#include <iostream>
#include <string>
using namespace Std;
Class student{
Public
Student (string,string,string);
~student ();
void Show ();
Private
String num;
String name;
string sex;
};
Student::student (string nu,string na,string s) {
Num=nu;
Name=na;
Sex=s;
cout<<name<< "is builded!" <<endl;
}
void Student::show () {
cout<<num<< "\ t" <<name<< "T" <<sex<<endl;
}
Student::~student () {
cout<<name<< "is destoried!" <<endl;
}
int main () {
Student S1 ("001", "Thousand Hands", "male");
S1.show ();
Student S2 ("007", "Outline Hand", "female");
S2.show ();
cout<< "Nihao" <<endl;
cout<<endl;
cout<< "Nihao" <<endl;
return 0;
}



The thousand hands are constructed first, and the results are reconstructed after the hands of the structure, the results of the first to break the outline of the hand.

Characteristics:
An object defined globally and a static (static) local object defined in a function that is called only when the main function ends or when the Exit function ends the program.

If the object is defined in a function, its constructor is called when the object is created, and the destructor is called first when the function call ends and the object is freed.

Copy Code code as follows:

#include <iostream>
#include <string>
using namespace Std;
Class student{
Public
Student (string,string);
~student ();
void Show ();
String num;
String name;
};
Student::student (String nu,string na) {
Num=nu;
Name=na;
cout<<name<< "is builded!" <<endl<<endl;
}
void Student::show () {
cout<<num<< "T" <<name<<endl<<endl;
}
Student::~student () {
cout<<name<< "is destoried!" <<endl<<endl;
}
void Fun () {
cout<< "============ Call fun function ============" <<endl<<endl;
Student S2 ("002", "automatic local variable");//define automatic local objects
S2.show ();
Static Student S3 ("003", "static local variable");
S3.show ();
cout<< "===========fun function call End ==============" <<endl<<endl;
}
int main () {
Student S1 ("001", "global variable");
S1.show ();
Fun ();
cout<< "\nthis is some content before the end\n";//This is a section after the function is called before the end of the main function
cout<<endl;
return 0;
}

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.