1. Some concepts of class 1.1 class (1) C Class Class and Structure Differences Are: Class Definitions The default is private, while the structure is public; (2) The structure in C can also have member functions; (3) :: The front may not be class, indicate global data or global function, call non-member functions to use :: Number ;: :: You can have two types of names, one is the name of the name space, One is the name of the class; (4) Class-Class method calls differently, Class Name :: Class Method (5) Class The constructor and destructive function does not return type, which is created and invalid in objects Automatically run; (6) Class is an abstract concept, it is an entity that does not account for a memory, so it cannot be assigned and an initialization of the object in the statement, such as Int i = 3, string s ("test" ) Trinity. 1.2 Creating Method for Object If a class is defined, the class name is TDATE, then it has the following possible TDATE object creation method: (1) TDATE D; call TDATE default constructor; (2) TDATE D () TDATE-free parameter constructor; (3) TDATE D; (4) TDATE D = TDATE; newly created a nameless object, then assign the object to D, call TDATE constructive and assignment function. Application of member objects in the class is useful; (5) TDATE * D = new TDATE (parameter), at this time, visit object member application ->, such as D-> getDate (), it is best to use IF after this declaration ( D == NULL) To determine if the memory allocation is successful; (6) TDATE * D = (TDATE *) Malloc (SIZEOF (TDATE)); this declaration is manually calling its constructor and the destructive function, and also If (d == null) determines whether memory is allocated; (7) TDATE M (D): Create a new specified object with existing specified objects, calling TDATE copy functions; (8) TDATE D = "2004 -03-02 ": If this method, TDATE must have a CHAR * type constructor, which is TDATE (CHAR * MDATE), which indicates that the TDATE object can be created using a string, which uses the type conversion of constructor. Features. Or there is a type of CHAR * constructed as a parameter, such as TDATE (String Date), char * can be converted to String, so it can also be converted to TDATE.
The most common type of constructor is the use of String class, such as string s = "hello"; (9) Auto_Ptr
(13) Object array creation: class test {public: test (int I, int j) {m_i = i; m_j = j;} int m_i; int m_j;}; test t [2] = {Test (1, 2), TEST (3, 4)}; 1.3 The initial list of the initialization list of the class class is after the parameter list of constructor, before the function body, this indicates that the object structure has not been established (the establishment of the object structure is completed by the constructor). . Therefore, the list of initialization has the following special roles: (1) Initialization constant, because constants cannot assign a value, it must assign it to it, and the category declaration does not have assignment statements because the assignment statement will Bring a range of memory allocations. Therefore, it is often necessary to initialize before the object structure has not been established, so it is necessary to place in the initial list; (2) initialization of the reference variable, the reason is the same as the first one; (3) the initialization of the object member, such as: class studentId {public : StudentId (int ID = 0) {value = id;} private: int value;}; class student {public: student (int ID), m_name ("jj"), m_name1 (m_name) {} Private: const string m_name; const string & m_name1;}; "), m_name1 (m_name) {m_id = 1; // uses the type conversion function of the constructor; m_id = studentID (2); // Newly create an nameless object, then assign the value;} private: studentid m_id; const string m_name Const string & m_name1;}; D = 10 is not allowed in the initial list. 1.4 Construction and Sequence C Provisions, each class must have a constructor, if the constructor is not defined when defining the class, C will provide a default constructor, it is a configuration function that does not parameters, not initialization, It is only responsible for creating (constructed) objects. As long as the class defines a constructor (whether it is with a function), then C will no longer provide this constructor, then the most custom parameter-free constructor, otherwise the definition method such as ClassName Obj can no longer use it. And use classname obj (....) To define this method. But if you use classname obj (); such a statement, you will not be wrong when the declaration is defined because it defines a function such as an OBJ, and its return type is ClassName.
Local and static objects, in order to declare the order, and all variables and objects are unified, such as: int main () {int m = 5; if (m == 5) goto abc; int N = 0; abc: cout << n << ',' << m << Endl; return 0;} The above program is in the compiler, if it passes The result is 5, 0. Similarly, the class of members data is also constructed in the class, such as Class Test {public: test (): n (2), m (n 1) {cout << "M: << m << "N: << n << endl;} private: int m; int N;}; After the instance of the Test class is created, the output is not M: 3 N: 2, but m : Unscontinence N: 2, because M declaration is before n, so it is before n, so it is actually run before M (N 1) before M (N 1) in the initialization list. M (n 1), then run n (2), thus N-N 1) is running yet (related to the compiler). Like static objects and static variables, the static objects of the file scope are all constructed before the main function starts. The static object in the block scope, the configuration is constructed when entering the function of the static object first entered the static object, such as Class Test {public: TEST (INT i) {cout << "Class:" << i << endl ;}}; Void f (int N) {static test t (n); cout << "function:" << n << end1;} int main () {f (10); f (20); return 0 The result of the program is: Class: 10; function: 10; function: 20; two calls for the function F in the main function of the program, but the Test class is only constructed. That is, the static object constructor is only constructed once. All global objects are all constructed before the main function starts running (this is the same as global variables), so it is difficult to debug if the constructor of these objects has problems. You can add an output statement in these constructors to locate an error, and then debug it as a local object. Moreover, since the compiler cannot control the connection order of the file, the sequence of constructing of the global object in the multi-file is uncertain, so it is not necessary to access another global object in the global object, so this global object that is accessed may not be constructed. Such as: class study {}; class teacher {public: teacher (student & s) {}}; student S1; // file 1 Global Object Teacher T (S1); // File 2 Global Objects may be constructed Teacher When t (S1) is still not configured. 1.5 Copy Function Assignment Functions C The default copy function of the default copy function is to complete a copy of a member. If the member is a class object, call its copy constructor or the default copy function.
Therefore, it is also necessary to be careful to the use of member objects, be sure to know that the copy function of the member object is allocated, if the dynamic memory is assigned, and these memory is released in the destructor. The automatically created copy function is a shallow copy. It is a bit copy. If a pile of memory is allocated in a class method, then the two pointers point to the same resource, and the same memory will be Double sectors, if such objects are passed to the function as parameters, the function in the actual parameters is not in the actual function. So this is a need for overloading the copy function. It is actually determined whether the overloaded copy function is required, and it can be seen whether the destructor uses delete and free. If you use it, you will use the stack in the class method, you should overload the copy function, and if you overload the copy function Generally also reloaded assignment functions, the heavy-duty assignment function is simple, need to determine if it is self-assignment before calling the copy function, and then releases the own resources, then calls as long as the copy function can be called. The purpose of the heavy-duty copy function is to achieve the same dynamic memory, and then create the same dynamic memory according to the dynamic memory occupied by the source copy object, and then copy the data in the copy object dynamic memory to the newly built dynamic memory. The copy function and assignment should be done by copying the value of the member data of the copy object, and of course this process may involve the allocation of dynamic memory, only this is.
The following are examples of myString class: class myString {public: myString (); myString (const char * str); myString (const myString & obj); ~ myString (void); myString & operator = (const myString & obj); const char * & c_str () // Notes 0: The return value should be const {return m_str;} private: char * m_str;}; mystring :: mystring () {m_str = new char [2]; m_str [0] = 'a'; M_Str [1] = '/ 0';} mystring :: mystring (const char * str) {// Notes 1: M_Str = Str; if (str == null) {// Notes 2: You can use m_str = null; m_str = new char ('/ 0');} else {INT Ilen = Strlen (STR); // Notes 3: Strlen calculation length does not include end computing, so plus one m_str = new char [Ilen 1]; if (m_str! = null) {structpy (m_str, str); // Notes point 4: Make sure there will be string end values m_str [Ilen] = '/ 0';}}} mystring: : MyString (const myString & obj) {// Notes 5: You can access the object's private variable INT ilen = strlen (obj.m_str); m_str = new char [Ilen 1]; if (m_str! = null) {structure ( m_str, obj.m_str); m_str [il] = '/ 0';}} mystring & mys TRING :: Operator = (const mystring & obj) {if (this == & obj) Return * this; delete [] m_str; int = strlen (obj.m_str); m_str = new char [Ilen 1]; if (m_str = Null) {structure (m_str, obj.m_str); m_str [il] = '/ 0';} return * this;} myString :: ~ mystring () {delete [] m_str; // Notes 6} There are several values for this class: (1) Notes 0: const char * & c_str (), the reference to the function c_str () must be limited to constraint, otherwise, using char * p = s outside the object .c_str (); p [0] = 'a';
It is easy to change the value of private variables inside the object. This is not satisfied with the data package requirements. (2) Note 1: M_Str = STR cannot be used, because in the paypoint 6, it is used in the destructive function, that is, indicate that m_str points to a dynamic memory, and the parameter STR may point to not Dynamic memory, then there is an error when destructure. Even if the STR points to a dynamic memory, such an assignment statement uses two pointers to point to a dynamic memory, the memory is released after the designer, and the pointing point of the arguments is very dangerous. (3) Note 2: You can use m_str = null instead of m_str = new char ['/ 0'], because for the definition of m_str, you can use the delete statement, that is, the destructor will not be wrong. (4) Notice 5: You can access the private variable of the object, I don't know why, who knows maybe notify 8280338@tzenet.com. (5) Notice 6: Use Delete in the destructor, so in the definition of the class must ensure that all M_STR assignments must point to dynamic memory. (6) For the default constructor mYString (), there is another form, namely: mystring (const char * str = "hello); // If the value of the parameter is not provided, it will have a default value but Can't use: MyString (const char * str) as the default constructor (7) This can be done without overloading mystring & mystring :: Operator = (const char * str), because using mystring s; s = "hello", Type conversion, and MyString constructor implements mYString (char *), so you can convert a string to a MyString object via the constructor. (8) The biggest difference between the copy function and the assignment function is that the copy function is initialized, that is, the copy function is executed, and the object does not exist when myclass a = b is executed; and the value of the value is executed, such as A = B, A and B The two objects already exist. 1.6 Static members Static members are divided into static data members and static methods. Static members' statements need to be added with static keywords, such as: static int num; static int getnum () {...} Static member does not belong to any specific object of the class, it belongs to the class, so it is class, so the static member is called a class in other languages. Data members, and class methods, indicating that static members can call directly through class, without having to create a class object, and then call it through object calls.
Because of these particularities of static members, the static data member cannot be initialized in the constructor, and the initialization of the general static data member works in the definition department of the class. The following is an example of the definition of static members: Class TestStatic {PUBLIC : Static int getNumber () {return m_int;} private: static int m_int;}; int interface () {cout << TestStatic :: getnumber () → rn 0;} In this example, Static declaration static variables and methods are used, and the static data members are initialized via Int TestStatic :: m_int = 1, and TestStatic :: GetNumber () is used to call the static method. There is also a particularly need to note that non-static methods and data members cannot be accessed in a static method, and the reason is very simple. Static members have existed before class instantiation, which can be called, at which time non-static methods and data members Haven't created yet, so it could not be called. If you want to call non-active members in a static method, you need to create instances of this class first, such as Class TestStatic {public: testStatic () {m_INT1 = 10;} static int getNumber () {testStatic T; t.m_int = t. M_INT1; RETURN M_INT;} int GETNUMBER1 () {RETURN M_INT;} private: static int m_int; int m_int1;}; int testStatic :: m_int = 1; static member function and non-static member function depending on the statics of the function function The THIS pointer, instead of the static member function has a THIS pointer to the current object, compile the compiler as the first parameter of the object, as the int GetNumber1 () function in the above class will automatically add an address parameter of an object. , Becomes int GetNumber1 (TestStatic *). And for static member functions, there is no such conversion. 1.7 The main role of friend's friend is to improve efficiency and convenient programming, but also destroyed the package. A class friend member can be a normal function, another class of member functions, or another class.
Friends' statements may be in any part of the class such as Public, Private et al, the following is an example of friendly definition and use: class test; // Notes 1class test1 {public: int Gettest (Test & T);}; class test { PUBLIC: TEST () {m_value = 1;} Friend Int Test1 :: Gettest (Test & T); // Notes 2Private: Friend Int Gettest (Test & T); // Notes 3 INT M_VALUE;}; int Gettest & t) {RETURN T.M_Value;} int Test1 :: Gettest (Test & T) {Return T.m_Value;} int main () {test1 t; cout << t.gettest (Test ()) << endl; cout <