1. compatibility of string classes
(1) The String class maximizes the compatibility of C strings
(2) You can use a string object in the same way that you use C strings , such as
string " ABCEDEFG " ; char c = s[i];
The "Programming Experiment" uses the string class in C mode
#include <iostream>#include<string>using namespacestd;intMain () {strings ="a1b2c3d4e"; intn =0; for(intI=0; I<s.length (); i++) { if(IsDigit (S[i]))//use a String object (that is, an array) in C-strings{n++; }} cout<< n << Endl;//4 return 0;}
2. How objects of the class support subscript access to arrays
(1) Array access is a built-in operator in C/s + +
(2) The primitive meaning of array accessors is array access and pointer operations
a[n]←→* (A + N) ←→* (n + a) ←→n[a]
Review of "instance analysis" pointers and arrays
#include <iostream>#include<string>using namespacestd;intMain () {inta[5] = {0}; for(intI=0; i<5; i++) {A[i]=i; } for(intI=0; i<5; i++) { //pointer-mode access array, less intuitive! //while subscript access is used, you can hide the manipulation of pointerscout << * (A + i) << Endl;//cout<< A[i] << Endl;} cout<<Endl; for(intI=0; i<5; i++) {I[a]= i +Ten;//A[i] = i + ten; } for(intI=0; i<5; i++) {cout<< * (i + a) << Endl;//cout<< A[i] << Endl; } return 0;}
3. Overloaded array access operators:[]
(1) can only be overloaded with member functions of a class
(2) overloaded functions can and can only use one parameter
(3) Multiple overloaded functions can be defined with different parameters
(4) You can hide the operation of the pointer
"Programming Experiment" overloaded array access operators
The perfection of the "Programming Experiment" array class
4. Summary
(1) String class Max program compatible with C string usage
(2) overload of an array accessor allows the object to emulate the behavior of an array
(3) Array accessors can be overloaded only by member functions of the class
(4) Overloaded functions can and can only use one parameter
34th class overloading of array operators