C ++ learning diary (2) ---- initial string type and encapsulation, diary string
Which data is frequently used but complicated to operate?
Summary:
Int; double; float; char; bool these types are frequently used, but not complex. However, for strings (char arrays), which are frequently used but complex, can only be referenced by a series of functions. How can this problem be overcome?
String type: string!
For example
After setting the header file, you can directly define a name, hobby, and output it directly, which is very convenient!
The following describes how to initialize a string:
The convenience of string is not only reflected in initialization, but also in a complete set of humanized services. As follows:
Code example:
# Include <iostream> # include <stdlib. h ># include <string> using namespace std;/* run this program using the console pauser or add your own getch, system ("pause ") or input loop */int main (void) {string name; cout <"Please input your name"; getline (cin, name); if (name. empty () {cout <"input is null .. "<endl; system (" pause "); return 0;} if (name =" imooc ") {cout <" you are a adminstractor "<endl ;} cout <"hello" + name <endl; cout <"your name length:" <name. size () <endl; // "+" cannot be used for string and non-string connection. Use "cout <" your name first letter is: "<name [0] <endl; system (" pause "); return 0 ;}
If you enter any name, for example:
If you enter the Administrator name (imooc ):
Data encapsulation
Basic Object-oriented thinking:
Object-centric, specifically, it is to express the logic of the program by "who is doing what", to reflect the code level, that is, to put all programs and data, in other words, all the actions of an object in a program are completed by calling its own functions.
How can we encapsulate data members through functions? Example
Taking the age data as an example, we define two member functions, "setAge", which is used to set the age value. The other is "getAge", which is used to read the age value.
Although this operation is troublesome, it improves the security and integrity of the Code and is worth it!
Advantages of Data encapsulation:
In this example, if "stu. age" is assigned a value of 1000, it is obviously a problem. However, if the data is encapsulated, the problem can be solved.
For "setAge" to calibrate the function, define a value range, and solve the above problem!
For some data members, we only want these Members to be read by the outside, instead of being rewritten by the outside. How can this problem be solved? For example:
We only want to use such a member to get the number of wheels instead of setting them.
The following is an example of encapsulated code.
# Include <iostream> # include <string> using namespace std;/* Data encapsulation defines a Student class, which includes the following requirements: 1. name: name2. gender3. credits (read-only): score4. study: study */class Student {public: void setName (string _ name) {m_strName = _ name;} string getName () {return m_strName;} void setGender (string _ gender) {m_strGender = _ gender;} string getGender () {return m_strGender;} int getScore () {return m_iScore ;} void initScore () {m_iScore = 0;} void study (int _ score) {m_iScore + = _ score; // m_iScore = m_iScore + _ score;} private: string m_strName; string m_strGender; int m_iScore;}; int main (void) {Student stu; stu. initScore (); stu. setName ("zhangsan"); stu. setGender ("male"); stu. study (5); stu. study (3); cout <stu. getName () <"" <stu. getGender () <"<stu. getScore () <endl; system ("pause"); return 0 ;}