01.C + + type conversion (understand)
Static conversion:
be used for Class hierarchy Base class (parent class) and Derived class Conversion of pointers or references between (subclasses)
Used for conversion between basic data types, such as converting int to char and char to int. The security of this transformation should also be guaranteed by developers
Dynamic conversion:
l ynamic_cast is mainly used for uplink conversion and downlink conversion between class levels;
l when performing uplink conversion between class levels, dynamic_cast and static_ The effect of cast is the same;
l during downlink conversion, dynamic_cast has the function of type checking, which is better than static_cast is safer;
Constant conversion
l the constant pointer is converted into a non constant pointer and still points to the original object;
l constant references are converted to non constant references and still point to the original object;
Reinterpret transformation
This is the most insecure conversion mechanism and is most likely to go wrong.
It is mainly used to convert a data type from one type to another. It can convert a pointer to an integer or an integer to a pointer
02. Basic concept of abnormality (understand)
Exception handling is an error in the handler. The so-called error refers to some abnormal events that occur during the operation of the program
03. Abnormal function (understand)
//Defects of exception handling method in C language:
//1. Ambiguous meaning of return value
//2. The return value can only return one piece of information
//3. The return value can be ignored
C + + exceptions can solve the above problems
04. Basic syntax of exceptions (key points)
int func(int a, int b) { if (b == 0) { //2. Throw an exception throw 10;//Throw an exception of type int, } return a / b; } void test() { int a = 10; int b = 0; //1. Put the code blocks with possible exceptions into the try try { func(a, b); } catch (int)//3. Receive an exception of type int { cout << "Receive one int Exception of type" << endl; } }
05. Execution process of exception code (understand)
#define _CRT_SECURE_NO_WARNINGS #include<iostream> using namespace std; int func(int a, int b) { if (b == 0) { //Step 2: throw 10;//Throw an exception of type int, cout << "throw Code after" << endl; } return a / b; } void test() { int a = 10; int b = 0; try { func(a, b);//Step 1: cout << "func Code after" << endl; } catch (int)//Step 3: { cout << "Receive one int Exception of type" << endl; } } int main() { test(); system("pause"); return EXIT_SUCCESS; }
06. Exceptional advantages (key points)
1. The user does not know what the return value means. An exception can throw an object. The object can contain many member functions and can have a lot of information
class Maker { public: void printMaker() { cout << "Divisor cannot be 0" << endl; } }; int func(int a, int b) { if (b == 0) { Maker m; throw m; } return a / b; } void test() { int a = 10; int b = 0; try { func(a, b); } catch (int) { cout << "Receive one int Exception of type" << endl; } catch (Maker maker) { cout << "Receive one Maker Exception of type" << endl; maker.printMaker(); } }
2. The return value can be ignored by the user, but the exception cannot be ignored. If it is ignored, an error will be reported to you
3. The return value can only return one piece of information, but the object has member functions and can contain multiple pieces of information
4. Layer by layer dependency handling exceptions
int func(int a, int b) { if (b == 0) { //Maker m; //throw m;// Throw an exception of type Maker throw 20.22;//Throw an exception of type double } return a / b; } void test() { int a = 10; int b = 0; try { func(a, b); } catch (int) { cout << "Receive one int Exception of type" << endl; } catch (Maker maker) { cout << "Receive one Maker Exception of type" << endl; maker.printMaker(); } catch (double s) { //If you don't want to handle exceptions, you can throw them up to the function calling this function throw; } } int main() { try { test(); } catch (double d) { cout << "Receive one double Exception of type" << endl; } system("pause"); return EXIT_SUCCESS; }
07. Strict type matching of exceptions (key)
int func(int a, int b) { if (b == 0) { //Maker m; //throw m;// Throw an exception of type Maker //throw 20.22;// Throw an exception of type double //throw 'c'; throw 20.0f; } return a / b; } void test() { int a = 10; int b = 0; try { func(a, b); } catch (int) { cout << "Receive one int Exception of type" << endl; } catch (double s) { cout << "Receive one double Exception of type" << endl; } catch (char) { cout << "Receive one char Exception of type" << endl; } catch (...)//Receive other types of exceptions { cout << "Receive an exception of another type" << endl; } }
08. Abnormal interface declaration (understand)
void func() throw(int,char) //Only int or char exceptions are allowed { throw 10; //Throw a double type exception, and the QT program will file } int main() { try { func(); } catch(int) { cout << "int"; } catch(...) { cout << "....."; } return 0; }
09. Stack unwinding (key)
In a function that throws an exception, if the function does not end after the exception is thrown, all the objects applied on the stack will be released, which is called stack unwinding
class Maker { public: Maker() { cout << "Maker Structure of" << endl; } Maker(const Maker &m) { cout << "Maker Copy construction of" << endl; } ~Maker() { cout << "Maker Deconstruction of" << endl; } }; void func() { //In the function that throws an exception, if the exception is thrown but the function does not end, the objects applied on the stack will be released //This is called stack unwinding Maker m; throw m;//This m is a copy of Maker m cout << "func End of function" << endl; } void test() { try { func(); cout << "func()After code" << endl; } catch (Maker) { cout << "Receive one Maker Exception of type" << endl; } }
10. Life cycle of abnormal variables (key and difficult points)
1. Generate three objects
class Maker { public: Maker() { cout << "Maker Structure of" << endl; } Maker(const Maker &m) { cout << "Maker Copy construction of" << endl; } ~Maker() { cout << "Maker Deconstruction of" << endl; } }; //Generate three objects void func1() { Maker m;//The first object is released before the exception is received throw m;//The second object is copied from the first object } void test01() { try { func1(); } catch (Maker m1)//The third object is copied from the second object { cout << "Receive one Maker Exception of type" << endl; //The second and third objects are released at the end of the catch } }
2. Generate two objects
void func2() { //First object throw Maker();//Anonymous object } void test02() { try { func2(); } catch (Maker m1)//Second object { cout << "Receive one Maker Exception of type" << endl; //The first and second objects are released at the end of the catch } }
3. Generate an object (commonly used)
void func3() { throw Maker();//Anonymous object } void test03() { try { func3(); } catch (Maker &m1) { cout << "Receive one Maker Exception of type" << endl; } }
4. Note:
void func4() { //The compiler does not allow addressing anonymous objects in the stack //throw Maker();// Anonymous object //The compiler allows addressing of anonymous objects in the heap throw new Maker(); } void test04() { try { func4(); } catch (Maker *m1) { cout << "Receive one Maker Exception of type" << endl; delete m1; } }
11. Abnormal polymorphism (key)
//Base class of exception class Father { public: virtual void printM() { } }; //1. Succession class SonNULL :public Father { public: virtual void printM()//2. Override the virtual function of the parent class { cout << "Null pointer exception" << endl; } }; class SonOut :public Father { public: virtual void printM() { cout << "Offside overflow" << endl; } }; void func(int a,int b) { if (a == 0) { throw SonNULL(); } if (b == 0) { throw SonOut(); } } void test() { int a = 0; int b = 10; try { func(a,b); } catch (Father &f)//3. The parent class reference points to the child class object { f.printM(); } }
12. Standard exceptions provided by the system (key points)
1. Standard exception class of the system
#define _CRT_SECURE_NO_WARNINGS #include<iostream> using namespace std; #Include < stdecept > / / 2013vs can be dispensed with #include<string> class Maker { public: Maker(int age) { if (age<0 || age>150) { throw out_of_range("Age out of range"); } else { this->age = age; } } public: int age; }; void test() { try { Maker m(200); } catch (out_of_range &ex) { cout << ex.what() << endl; } } int main() { test(); system("pause"); return EXIT_SUCCESS; }
2. Exception class written by yourself
class MyOut_of :public exception { public: MyOut_of(const char *errorinfo) { //const char * convert string this->m_Info = string(errorinfo); } MyOut_of(const string errorinfo) { this->m_Info = errorinfo; } const char * what() const { //Convert string to const char* return this->m_Info.c_str(); } public: string m_Info; }; class Maker { public: Maker(int age) { if (age<0 || age>150) { throw MyOut_of("Their own abnormal class, age is not in the range"); } else { this->age = age; } } public: int age; }; void test() { try { Maker m(200); } catch (MyOut_of &ex) { cout << ex.what() << endl; } }
13. Input flow of system standard (key)
1. The concept of flow and the structure of flow class library (understand)
Standard IO: read and write the standard input and output devices of the system
File IO: input / output read / write to disk
String IO: read and write memory
2. Member function
cin.get() / / only one character can be read at a time
cin. Get (a parameter) / / read a character
cin. Get (two parameters) / / you can read strings
cin.getline() / / take a line and discard the newline character
cin.ignore() / / ignore
cin.peek() / / peeping
cin.putback() / / put it back
3. Cases
//Determine whether the user entered a string or a number void test06() { cout << "Please enter a string or number" << endl; char c=cin.peek(); if (c >= '0'&&c <= '9') { int num; cin >> num; cout << "The number entered is:" << num << endl; } else { char buf[1024] = { 0 }; cin >> buf; cout << "The string entered is:" <<buf << endl; } } //Enter a number from 0 to 10 until it is entered correctly void test07() { int num; while (1) { cin >> num; if (num >= 0 && num <= 10) { cout << "Correct input" << endl; break; } cout << "Re-enter:" << endl; //Reset flag bit cin.clear(); //Empty buffer //cin.sync(); //2015 char buf[1024] = { 0 }; cin.getline(buf, 1024); //Print flag bit cout << cin.fail() << endl; } }
14. System standard output flow (key)
1. Format output through member function
//The formatted output is realized through the stream member function void test03() { int num = 99; cout.width(20);//Set width cout.fill('*');//fill cout.setf(ios::left);//Keep the data on the left cout.unsetf(ios::dec);//Unload decimal cout.setf(ios::hex);//Install hex cout.setf(ios::showbase);//display base cout.unsetf(ios::hex);//Unload hex cout.setf(ios::oct);//Install octal cout << num << endl; }
2. Format the output through the control character
//Format the output through the control character and introduce the header file iomanip void test04() { int num = 99; cout << setw(20);//Set width cout << setfill('~');//fill cout << setiosflags(ios::showbase);//display base cout << setiosflags(ios::left);//Keep the data on the left cout << hex;//Hex cout << oct;//octal number system cout << dec;//decimal system cout << num << endl; }
3. Print the decimal point after the floating point number
void test05() { double d = 20.22; cout << setiosflags(ios::fixed);//Set display float cout << setprecision(10);//Display 10 decimal places cout << d << endl; }
15. File reading and writing (key)
1. Review the document reading and writing of C language
1.Read and write files according to characters fgetc,fputc int fputc(int ch, FILE * stream);//write int fgetc(FILE * stream);//read 2.Read and write files by line fgets,fputs int fputs(const char * str, FILE * stream);//write char * fgets(char * str, int size, FILE * stream);//read 3.According to the format file fprintf,fscanf int fprintf(FILE * stream, const char * format, ...);//write int fscanf(FILE * stream, const char * format, ...);//read 4.Read and write files by block fread,fwrite size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream);//write size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream);//read
2. File reading and writing in C + +
#define _CRT_SECURE_NO_WARNINGS #include<iostream> using namespace std; //1. Import header file #include<fstream> //Output the information in the program to the buffer, and then write to the file (write file) void test01() { //2. Define flow objects ofstream ofs; //3. Open the file and open it by writing. If there is no file, create it ofs.open("test.txt", ios::out | ios::trunc); //4. Judge whether it is opened successfully if (!ofs.is_open()) { cout << "Open failed" << endl; } //5. Write information ofs << "full name:name of a fictitious monkey with supernatural powers" << endl; ofs << "Age:18" << endl; ofs << "height:180cm" << endl; //6. Close the file ofs.close();//Close the file and flush the buffer } //Input the disk information into the buffer and then read it into the program (read file) void test02() { ifstream ifs; ifs.open("test.txt", ios::in); if (ifs.is_open() == false) { cout << "Open failed" << endl; } //The first way is to read the file //Read line by line /*char buf[1024] = { 0 }; while (ifs>>buf) { cout << buf << endl; }*/ //The second way is to read files //char buf[1024] = { 0 }; //while (!ifs.eof()) / / judge whether to read the end of the file //{ // ifs.getline(buf, sizeof(buf)); // cout << buf << endl; //} //The third way is to read the file //Single character reading char c; while ((c=ifs.get())!=EOF) { cout << c; } //Close file ifs.close(); } int main() { test02(); system("pause"); return EXIT_SUCCESS; }
16. Binary file reading and writing (key and difficult points)
1. Binary reading and writing
class Maker { public: Maker(){} Maker(const char*name,int age) { this->age = age; strcpy(this->name, name); } public: char name[64]; int age; }; //Write file void test01() { Maker m1("name of a fictitious monkey with supernatural powers",18); Maker m2("Vegeta", 22); ofstream ofs; ofs.open("test.txt", ios::out | ios::trunc | ios::binary); if (!ofs.is_open()) { cout << "Open failed" << endl; } //write ofs.write((const char *)&m1, sizeof(Maker)); ofs.write((const char *)&m2, sizeof(Maker)); ofs.close(); } //read file void test02() { ifstream ifs; ifs.open("test.txt", ios::in | ios::binary); if (!ifs.is_open()) { cout << "Open failed" << endl; } //read Maker m1; Maker m2; ifs.read((char*)&m1, sizeof(Maker)); ifs.read((char*)&m2, sizeof(Maker)); cout << "Name:" << m1.name << " Age:" << m1.age << endl; cout << "Name:" << m2.name << " Age:" << m2.age << endl; ifs.close(); }
2. There is a problem when the class member variable is string in file reading and writing
Note: when reading and writing files, the member variables in the class should not have string type
class Maker { public: Maker(){} /*Maker(const char*name,int age) { this->age = age; strcpy(this->name, name); }*/ Maker(string name, int age) { this->name = name; this->age = age; } public: string name; //The string class has a member pointer char *, which points to the space where the string is stored //When we store the data of string class in a file and read it again, we can't guarantee that the pointer is valid //If the space opened up by string is more than 16 bytes, it is in the heap area and less than is in the stack area int age; };