Talk about the initialization of C ++ (1)

zhaozj2021-02-16  56

C is profound, this is a lot of people who know C . The more deeper, the more I think she will give you a good training - let you become a real programmer. I want to start from her initialization, trying to show their corners, I hope to help you improve the interest in C and programming - then, I will discover, open up, when you integrate your wisdom into it, The line is not only a program, but it has life.

Regarding the initialization related part of C, such as pointers, such as the difference between the global variable and the local variable default initialization, such as the default initialization of static variables, jump. We started from the class. Initialization is a very important job, because your class (exactly the object, the program) execution process is a series of state transformations, while the initiality is incorrect, it is impossible to reach the correct solution. Initialization in object-oriented C is done by constructor, in other scenarios, it may be called a constructor. This is everyone understand. However, it is not necessary, maybe you still haven't clear, such as how to design the default initialization, which member variables are only the only initial form, the combination and inheritance of initialization, shallow copy issues, unnamed objects, special needs initialization ( Example objects must be unique), etc.) I will debug the program on the VC7.0, and I am debugging one, talk about a problem, trying to explain it clearly. I am willing to help you.

This time, I said the initialization process and the initialization of static members. Regardless of the programmer, the initialization of the object-oriented C is necessary! ! You have written a class, there is no write constructor, but the system will "secret" give you a system default constructor. It will work when instantiative objects - I want to know, once you define the construct Functions, the system will not provide the default constructor. The problem is, we should define your own constructor. Otherwise, the system is mostly unable to reach the correct initial state! Define a good constructor, should be a multi-version constructor to make a security check work. We will give an example below that the example of C creators is changed. Need a class, date date, which has a member variables Day, Month, Year, perform some relevant operations. How to perform initialization? We may see the following code:

// ... class date {Int D, M, Y; PUBLIC: DATE (INT DD = 0, INT mm = 0, int yy = 0) {d = DD; m = mm; y = yy;}

// ...}; // ...

Such a program does not have a syntax error, but it works, but it is not working properly. What will the statement below? Date OneDay (-2, 10, 2002); Simple inspection, as part of the code below. It is also nothing. If the following statement is still unable to force:

Date Oneday (29, 2, 1981);

// ... class date {Int D, M, Y; PUBLIC: DATE (INT DD = 0, INT mm = 0, int yy = 0) {IF (DD> = 0 && yy> = 0 && mm> = 0 && m <= 31 ) {// ??? D = DD; m = mm; y = yy;} // else ???} // ...}; // ...

What's more, we may need to initialize with string, initialize with a char * pointer: string s = "29/2/1981"; char * p = "29/2/1981";

How should I do it? I think you need to examine your initialization! ! !

Let's take a look at a design example: //////> Date (C ) // Used to initialize the work, write on 22/5/2003 ///

//date.h#pragma overce # include using namespace std; enum month {jan = 1, Feb, Mar, Apr, May, Jun, JUL, AUG, SEP, OCT, NOV, DEC}; Class Date { // Abnormal class (default constructor, because we just throw an exception, even no logo) Class Bad_date {}; int D, y; month m; // static member variable static date default_date; public: Date (int DD = 0 , MONTH MM = Month (0), int yy = 0);

Date (string s) {/ * 省 省 去 * /} Date (char * p) {/ * 省 省 去 内容 * /} ~ Date (void); static void set_default (int D, month m, int y); int Day (void) consth (void) const; int year (void) const;

INT Leepyear (int y) {/ * 省 省 去 * / return 0;} // ... void test (void);

Let's take a look at the implementation section:

//date.cpp#include "Date.h" #using

// Static member's initialization Date Date :: default_date (4, Feb, 1981);

Date :: ~ Date (void) {} ​​// Example of exact initialization Date :: Date (int DD, MONTH MM, INT YY) {// (1) IF (DD == 0) DD = default_date.day (); // test d = default_date.day () IF (mm == 0) mm = default_date.month (); // test m = default_date.month () if (yy == 0) yy = default_date.year (); // test y = default_date.year () int max;

Switch (mm) {Case Feb: max = 28 Leepyear (YY); Break; Case Apr: Case Jun: Case Sep: Case Nov: Max = 30; Break; Case Jan: Case Mar: Case: Case Jul: Case AUG: Case Oct: Case Dec: max = 31; Break; default: throw bad_date ();} if (DD <1 || max

Void Date: Set_Default (int D, month m, int y) {date :: default_date = DATE (D, M, Y);

INT DATE:: day (void) const {return d;}

Month Date :: Month (void) const {return m;}

INT DATE: YEAR (VOID) const {return y;} // Test function void date :: test (void) {std :: cout << "/ n this is a test using class date. / n" << " The date is (days / month / year): "<< D <<" <<< m << "/" << Y << std :: endl; std :: cout << "/ n Thank you! / N / N ";

Here, there are several need to pay attention to: (1) Version Date of Constructor (int DD = 0, Month mm = month (0), int yy = 0); Date (String s) {/ * 省 省 内容* /} Date (char * p) {/ * 省 省 内容 * /} (2) Static member provides the default value // static member variable static date default_date; // and interface static void set_default (int D, Month M, INT Y); (3) Abnormal management // abnormal class (default constructor, because we just throw an exception, even no logo) Class Bad_date {}; (4) Good algorithms in constructor These are our initialization work Have a good guarantee! Test with the following file program, available: //fmain.cpp#include "date.h"

Void main () {date one.test ();} / * results:

This is a test using class date. The date is (day / month / year): 4/2/1981

Thank you!

Press any key to continche * /

转载请注明原文地址:https://www.9cbs.com/read-25447.html

New Post(0)