I. ++ Operator Overloading
Front ++ operator overload
The method of member functions is overloaded. The prototype is:
Function Type & operator ++ ();
You can use the following methods to overload functions:
Friend function type & operator ++ (class type &);
Post ++ operator overload
The method of member functions is overloaded. The prototype is:
Function Type Operator ++ (INT );
You can use the following methods to overload functions:
Friend function type operator ++ (class type &, INT );
C ++ code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
|
# Ifndef _ integer_h _ # DEFINE _ integer_h _Class integer { Public: INTEGER (int n ); ~ INTEGER (); Integer & operator ++ (); // Friend integer & operator ++ (integer & I );
Integer operator ++ (int n ); // Friend integer operator ++ (integer & I, int N );
Void display () const; PRIVATE: Int N _; }; # Endif // _ integer_h _ |
C ++ code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
|
# Include "integer. H" # Include <iostream> Using namespace STD;Integer: INTEGER (int n): N _ (n) { } Integer ::~ INTEGER () { } Integer & INTEGER: Operator ++ () { // Cout <"integer & INTEGER: Operator ++ ()" <Endl; + + N _; Return * this; } // Integer & operator ++ (integer & I) //{ /// Cout <"integer & operator ++ (integer & I)" <Endl; // ++ I. N _; // Return I; //}
Integer INTEGER: Operator ++ (int n) { // Cout <"integer & INTEGER: Operator ++ ()" <Endl; // N _ ++; Integer TMP (N _); N _ ++; Return TMP; } // Integer operator ++ (integer & I, int N) //{ // Integer TMP (I. N _); // I. N _ ++; // Return TMP; //}
Void INTEGER: Display () const { Cout <n _ <Endl; } |
C ++ code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
|
# Include "integer. H" # Include <iostream> Using namespace STD;Int main (void) { Integer N (100 ); N. Display (); Integer n2 = ++ N; N. Display (); N2.display (); Integer N3 = n ++; N. Display (); N3.display (); Return 0; } |
Note that an int parameter is added to the front ++ and back ++, but it does not work. When you set breakpoint debugging, you can find that the default value is 0.
In addition, the member functions cannot coexist with the member functions because they are not clear when the ++ operator is called.
Ii. Value assignment operator overload ,! Operator overload
C ++ code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
|
# Ifndef _ string_h _ # DEFINE _ string_h _Class string { Public: Explicit string (const char * STR = ""); String (const string & other ); String & operator = (const string & other ); String & operator = (const char * Str ); Bool Operator! () Const; ~ String (void ); Void display () const; PRIVATE: Char * allocandcpy (const char * Str ); Char * STR _; }; # Endif // _ string_h _
|
C ++ code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
|
# Pragma warning (Disable: 4996) # Include "string. H" # Include <string. h> # Include <iostream> Using namespace STD;String: string (const char * Str) { STR _ = allocandcpy (STR ); } String: string (const string & other) { STR _ = allocandcpy (other. Str _); } String & string: Operator = (const string & other) { If (this = & other) Return * this; Delete [] STR _; STR _ = allocandcpy (other. Str _); Return * this; } String & string: Operator = (const char * Str) { Delete [] STR _; STR _ = allocandcpy (STR ); Return * this; } Bool string: Operator! () Const { Return strlen (STR _)! = 0; } String ::~ String () { Delete [] STR _; } Char * string: allocandcpy (const char * Str) { Int Len = strlen (STR) + 1; Char * newstr = new char [Len]; Memset (newstr, 0, Len ); Strcpy (newstr, STR ); Return newstr; } Void string: Display () const { Cout <STR _ <Endl; } |
C ++ code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
|
# Include "string. H" # Include <iostream> Using namespace STD;Int main (void) { String S1 ("ABC "); String S2 (S1 ); String S3; S3 = S1; S3.display (); S3 = "XXXX "; S3.display (); String S4; Bool notempty; Notempty =! S4; Cout <notempty <Endl; S4 = "aaaa "; Notempty =! S4; Cout <notempty <Endl; Return 0; } |
Note that we declare the constructor as explicit, so S3 = "XXXX"; cannot be implicitly converted. You can reload a string & operator = (const char * Str ); function .! The operator is true when the string is not null.
Refer:
C ++ primer version 4
Valid tive C ++ 3rd
C ++ programming specifications