Study Notes for the next week (2-3)

Source: Internet
Author: User

Day 1: The constructor is a member function.

After processing an object, you can use the destructor to clear it in a custom manner. Destructor are special member functions used to clear data.
Remember: constructor and destructor are the member functions used to create, initialize, and clear object data. They can be used to overload constructors so that a class has more than one constructor, because sometimes you need to use one of these methods to create different objects respectively. You cannot reload the destructor.

The constructor has the same name as the member function and class. For example, a class is named aClass, And the constructor is aClass (). The constructor does not return values, and its return type cannot be defined, and void does not. This is also used by destructor. When writing an overloaded function, only the parameter table is different. By comparing the number or type of parameters, you can distinguish two overloaded functions.

File: // packageClass headers containing three Constructors
Class Children {
Char name [25];
Int age;
Float Weight;
Public:
Void prData (void );
Char * getname (void );
Int getage (void );
Float getWeight (void );
Children (void); // The Declaration of the three constructors;
Children (int, float );
Children (char *, int, float );
~ Children (); // destructor
};

The Destructor, As a member function of the class, also have the same name as the class. The difference is that there is one more ~ Symbol. Destructor of the class named aClass ~ AClass (). A class can have only one destructor. If the Destructor is a member function, there is no parameter, no return value, and no reload is allowed.
If the constructor allocates memory space for data members, the destructor releases the memory. As long as the variable is within its scope, its value is valid.

VC ++ calls the constructor when defining a variable, and calls the Destructor when the variable is out of scope. The timing is automatic.
Constructor and destructor should be common member functions.
The constructor prototype can contain default parameters. However, it cannot be used with null parameters without parameters.

When defining an array of objects, write constructor and destructor. When creating an array, VC ++ uses constructor to construct each element in the array of objects. When an array is cleared out of scope, the destructor releases all objects.
When creating an object that contains pointer members, the assignment operator's function and the copy constructor should be re-disaster-tolerant.



A copy constructor is a special type of constructor. Its parameters are references to an object variable. When initializing a new object based on an object or passing the returned object through reference, you need to call the copy constructor.

//////////////////////////////////////// //////////////////////
File: // wordString character string makes up for the shortcomings of VC ++ without strings
File: // copyrightRbg and 2000
File: // Filename: StrClass. cpp
//////////////////////////////////////// //////////////////////
# Include
# Include
Class String {
Char * st;
Public:
String & operator = (const String &);
Friend String operator + (const String &, const String &);
Friend ostream & operator <(ostream &, const String &);
String ();
String (const char *);
String (const String &);
~ String ();
};
String & String: operator = (const String & s)
{
If (this = & s)
{Return * this ;}
Delete [] st;
St = new char [strlen (s. st) + 1];
Strcpy (st, s. st );
Return * this;
}
String: String ()
{
St =;
}
String: String (const char * s)
{
St = new char [strlen (s) + 1];
Strcpy (st, s );
}
String: String (const String & s)
{
St = new char [strlen (s. st) + 1];
Strcpy (st, s. st );
}
String ::~ String ()
{
Delete [] st;
}
String operator + (const String & source, const String & tar)
{
If (! Strlen (source. st ))
Return tar;
Else if (! Strlen (source. st ))
Return source;
String temp;
Temp. st = new char [strlen (source. st) + strlen (tar. st) + 1];
Strcpy (temp. st, source. st );
Strcat (temp. st, tar. st );
Return temp;
}
Ostream & operator <(ostream & out, const String & s)
{
Out <return out;
}
//////////////////////////////////////// ////////////////////////////////
Main (){
String myname;
String youname ("zhangsan ");
String hisname = youname;
Myname = "renbugang ";
Cout <"My name is:" <cout <"You name is:" <cout <"His name is:" Cout <"His name chanage:" <String ourname;
Ourname = myname + youname + hisname;
Cout <"Our name is:" <return 0;
}

14th days: the keyword static ensures that local variables are not destroyed at the end of the block.

Static can be placed before int, float, and other data type identifiers, or before custom data type names. Static is also called extended survival. The default dynamic lifetime of a local variable corresponds to the static lifetime of a global variable.

Global variables are static and cannot be defined as dynamic. The global variable starts from its definition and ends with the source code. If the keyword static is added before the global variable, it indicates that the global variable has the file scope.
Similar to global variables, the keyword "static" before a function can restrict the visibility of the function, making it unavailable for other files connected to the file. If other files use the same name, no conflict will occur.
If you want another source file to call non-static functions in the file after the connection is complete, you must describe the prototype in all the files that call this function and name the keyword extren.

Non-static global variables and functions have external connectivity, that is to say, the code outside the current source file can also use these global variables and functions that do not use static definitions. static global variables and functions have internal connectivity, only the current file can be used.
The execution program returns to the block to keep its variable value, and uses static to define its local variable.
VC ++ retains only one set of member functions for each class, and each object has a function pointer to the member functions of its class.
Static data members are usually used to record the number of created objects. For example, each time a constructor calls the constructor, it adds a static member and deletes the corresponding static member in the destructor.

Use the scope OPERATOR: Access static data members and member functions. Static members are not connected to any objects. Therefore, they must call static functions or access static members through a class.
Use static member functions to access private static data members.
Initialize the static data member before main () and add the data type before the static data member.
Use of static data member record class usage, such as sum or average.

Concepts:

Compile: Convert the source code (. cpp) to the target code (. obj ).
Target code: the internal binary representation of the source file. It is a program format that machines can understand, and the source code is a program format that programmers can understand.
Connection: connect all the associated target codes to a single Executable File (.exe ).
Executable File: the State after the program is compiled and connected.
The program should be divided into several files, especially the Class header and the class implementation part, which can ensure that the program using the class is clear and easy to read.

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.