C study notes (3) - Construction and sector
Chen Genfa
Today is a bit depressed, drink some wine, now the head is already not dizzy, write some text, enrich yourself.
Still come back to recall the content yesterday. An example is enough to recall the content yesterday.
Class x {int A; public: x (int sa) // Note, the function name and class name here are uniform {a = sa;} int GETX () {Return A;}}; Class Y: public x {/ / Class y male fertile class x int C; public: showy (int y) {y = C;}}; constant. But everyone doesn't know if I have a note, and its function name and class name are not the same. Ha ha ~~, this is a constructor.
Why do you want to use constructs? Everyone knows that in the computer, the storage space allocated by different data types is different. Class is a user-defined type, its structure or complex, or simple, so when a class is declared, the compiler To allocate storage space for classes, and make the necessary initialization work for classes. Readers may now ask, what is the end of these work? This is the protagonist we want to say.
The constructor is affiliated with a specific class, and can be provided by the user, or the system can be automatically generated, the corresponding destructor, and the storage space is retracted by the destructor when a class is revoked.
The constructor is a special nature, summarized as follows:
One. The constructor name and class name must be the same.
two. The constructor can have any type of parameters, but there is no return type. If there is a statement below: illegal:
Class x {int A; public: x (int SA) // Constructor {a = sa; returnon a;} // illegal, constructor does not have return value int GETX () {Return A;}}. The constructor is automatically called when the object is defined. It is not called to be explicitly called, and the object is called simultaneously. The general format is:
Class name object name (parameter table); // class name and constructor name are consistent
So what kind of function is the destructor? It is the anti-operation function of the constructor, and it also has some of its own characteristics:
One. The destructor name is also the same as the functionality name, but the front must be added (~) declaration.
two. The destructor does not have parameters, and it cannot be returned, and he cannot be overloaded. He has uniqueness in the class.
three. When the object is revoked, the system automatically calls the destructor. example:
Class string_data {
Char * STR;
PUBLIC:
String_data (char * s)
{
Str = new char [strlen (s) 1]; // use new to allocate storage space
Strcopy (STR, S);
}
~ String_Data () // Features of the Pattern Function
{Delete Str;} // Release the allocated storage space with Delete
Void get_info (char *); void Sent_info (char *);
}
All in all, the constructor is used to allocate storage space for objects, and initialize the objects. The destructor is a class internal function that releases the storage space when the object is undo.
-------------------------------------
Struggle for a 100% programmer!
Personal description: The son of the farmer, or the farmer! I have lived in mediocrity before the age of 200. I don't want to be legendary after the age of 200.
Poor is the main song, inferior coat, cave, not sold cool, but reality. One year of rice <= 365 * 2.1.5 yuan a meal accounts for 80%. 2.5 yuan a meal accounted for 19.9%, even the meal occasionally (more than 6 yuan), is a classmate party, or when living subsidies are issued!
Smoking, bag smoke when depressed! (City people have not seen it), 3.5 yuan a pound, when the smoke is hidden, the room is surprising, take into account the brothers of the people, do not dare to smoke.
Occasionally, don't ask for your drill, just seek! Every drink, brothers pay for money!
Hello write, or code, or mood text, or oil poems, impromptu book, never collect (code exception).
Since the smaller county, the ear is ok, and it is infected. The brothers have difficult, always a horse, plus the country children to work hard, so few will lose.
Self-recognition, 50 yuan for the first time, for my university; 200 yuan to visit Xiamen, for livelihoods; 20 km, in order to save 2 yuan bus.
The main activities of entertainment are Internet, do not like to play games, do not like to play MM, often post. Netizens are very small, they are invited.
I like three sentences, I have given:
1. God will not live up to anyone, how much you pay, you will get! (Self-comfort)
--Herry (MS MVP)
2. The first, love second (self-encourage)
A Dang
3. As long as the process is correct, the ending will be good (self-warning)
Chen Zhong
QQ: 78607770
Email: 01ORDER@21cn.com
2003-12-24 Christmas Eve