Operator Overloading for C ++ Primer Plus learning notes

Source: Internet
Author: User

Operator Overloading of C ++ Primer Plus learning notes 1. Recommended selection of member functions and friend functions: Let's take a look at two examples: member function Overloading

# Include
 
  
Using namespace std; class Complex {public: Complex (double r = 0, double I = 0) {re = r; im = I ;} complex operator + (const Complex & obj); Complex operator! (); Void Display (); private: double re; double im ;}; Complex: operator + (const Complex & obj) {Complex temp; temp. re = re + obj. re; temp. im = im + obj. im; return temp;} Complex: operator! () {Complex temp; temp. re = re; temp. im =-im; return temp;} void Complex: Display () {cout <
  
   
0) cout <"+" <
   
    
Friend function overload
    
# Include
     
      
Using namespace std; class Complex {public: Complex (double r = 0, double I = 0) {re = r; im = I ;} friend Complex operator + (const Complex & obj1, const Complex & obj2); Complex operator! (); Void Display (); private: double re; double im ;}; Complex operator + (const Complex & obj1, const Complex & obj2) {Complex temp; temp. re = obj1.re + obj2.re; temp. im = obj1.im + obj2.im; return temp;} Complex: operator! () {Complex temp; temp. re = re; temp. im =-im; return temp;} void Complex: Display () {cout <
      
       
0) cout <"+" <
       
        
For member function overloading, because the display call of obj3 = 27 + obj1 is obj3 = 27. operator + (obj1), but 27 is not of the Complex type. Therefore, the compiler cannot call the operator + () overload operator of Complex to complete addition operations, because the call to obj3 = 27 + obj1 has a default constructor Complex (double r = 0, double I = 0), the system can automatically convert 27 to Complex (27) the member function can only be called for an actual object, and youyuan has no such restriction. Therefore, if an operator needs to modify the state of a class object, it should be a member function instead of a friend. On the contrary, if the operator's operands (especially the first operand) want to have implicit type conversion, this operator must be reloaded using a friend function instead of a member function. 2. Reload the prefix and suffix of ++ member functions.
        
# Include
         
          
Using namespace std; class Complex {public: Complex (double r = 0, double I = 0) {re = r; im = I ;} friend Complex operator + (const Complex & obj1, const Complex & obj2); Complex & operator ++ (); // prefix Complex operator ++ (int ); // suffix Complex operator! (); Void Display (); private: double re; double im ;}; Complex operator + (const Complex & obj1, const Complex & obj2) {Complex temp; temp. re = obj1.re + obj2.re; temp. im = obj1.im + obj2.im; return temp;} Complex & Complex: operator ++ () {re ++; im ++; return * this;} Complex :: operator ++ (int) // int Is the placeholder {Complex temp (* this); // copy the constructor re ++; im ++; return temp ;} Complex :: operator! () {Complex temp; temp. re = re; temp. im =-im; return temp;} void Complex: Display () {cout <
          
           
0) cout <"+" <
           
            
# Include
            
             
Using namespace std; class Complex {public: Complex (double r = 0, double I = 0) {re = r; im = I ;} friend Complex operator + (const Complex & obj1, const Complex & obj2); friend Complex & operator ++ (Complex & obj ); // Prefix: friend Complex operator ++ (Complex & obj, int); // Suffix: Complex operator! (); Void Display (); private: double re; double im ;}; Complex operator + (const Complex & obj1, const Complex & obj2) {Complex temp; temp. re = obj1.re + obj2.re; temp. im = obj1.im + obj2.im; return temp;} Complex & operator ++ (Complex & obj) {obj. re ++; obj. im ++; return obj;} Complex operator ++ (Complex & obj, int) // int Is the placeholder {Complex temp (obj); // copy the constructor obj. re ++; obj. im ++; return temp;} Complex: operator! () {Complex temp; temp. re = re; temp. im =-im; return temp;} void Complex: Display () {cout <
             
              
0) cout <"+" <
              
                # Include
               
                 # Include
                
                  # Include
                 
                   Using namespace std; class String {public: String () {msize = 0; mstr = NULL;} String (char * str); String (const String & str ); string operator + (const String & str); String & operator = (const String & str); void Display (); private: char * mstr; int msize ;}; String :: string (char * str) {if (str = NULL) {msize = 0; mstr = NULL;} else {msize = strlen (str ); mstr = new char [msize + 1]; assert (mstr); strcpy (mstr, str) ;}string :: String (const String & str) {if (str. msize = 0) {msize = 0; mstr = NULL;} else {msize = str. msize; mstr = new char [msize + 1]; assert (mstr); strcpy (mstr, str. mstr) ;}} String: operator + (const String & str) {String temp; temp. msize = msize + str. msize-1; temp. mstr = new char [temp. msize]; assert (temp. mstr); strcpy (temp. mstr, mstr); strcpy (& (temp. mstr [msize]), str. mstr); return temp;} String & String: operator = (const String & str) {if (this = & str) return * this; if (msize> 0) delete [] mstr; msize = str. msize; mstr = new char [msize]; assert (mstr); strcpy (mstr, str. mstr); return * this;} void String: Display () {cout <
                  
                    If you do not define a class overload assignment operator, the compiler generates a default assignment operator. The value assignment operator copies the source object to the target object in a domain-by-domain manner. The following is a summary of the functions automatically generated by the compiler: 1, constructor; 2, destructor; 3, copy constructor; 4. assign value operator; the difference between copy constructor and assign value operator is that copy constructor creates a new object, the value assignment operator is to change the value of an existing object. It is important to note that when a class contains data members of the pointer type, it is best to reload the value assignment operator and the copy constructor, otherwise, pointer suspension may occur (the dynamically applied space cannot be accessed and cannot be released); 4. Reload the [] operator.
                   
# Include
                    
                     
# Include
                     
                      
# Include
                      
                       
Using namespace std; class String {public: String () {msize = 0; mstr = NULL;} String (char * str); String (const String & str ); string operator + (const String & str); String & operator = (const String & str); char & operator [] (int index ); // readable and writable char operator [] (int index) const; // read-only void Display (); private: char * mstr; int msize ;}; String :: string (char * str) {if (str = NULL) {msize = 0; mstr = NULL;} else {msize = strlen (str ); mstr = new char [msize + 1]; assert (mstr); strcpy (mstr, str) ;}string :: String (const String & str) {if (str. msize = 0) {msize = 0; mstr = NULL;} else {msize = str. msize; mstr = new char [msize + 1]; assert (mstr); strcpy (mstr, str. mstr) ;}} String: operator + (const String & str) {String temp; temp. msize = msize + str. msize-1; temp. mstr = new char [temp. msize]; assert (temp. mstr); strcpy (temp. mstr, mstr); strcpy (& (temp. mstr [msize]), str. mstr); return temp;} String & String: operator = (const String & str) {if (this = & str) return * this; if (msize> 0) delete [] mstr; msize = str. msize; mstr = new char [msize]; assert (mstr); strcpy (mstr, str. mstr); return * this;} char & String: operator [] (int index) // readable writable {return mstr [index-1];} char String :: operator [] (int index) const // read-only, but can only be called by regular objects {return mstr [index-1];} void String: Display () {cout <
                       
                        
When the [] operator is overloaded, I have written two versions. One is readable and writable, and the other is read-only. Note: The operators that can only be reloaded as class members include four =, [], and (). ,->; 5. Input/Output Overloading
                        
# Include
                         
                          
Using namespace std; class Complex {public: Complex () {re = 0; im = 0;} Complex (double r, double I) {re = r; im = I ;} complex operator + (const Complex & obj); Complex operator! (); Friend ostream & operator <(ostream & OS, const Complex & c); friend istream & operator> (istream & is, Complex & c); private: double re; double im;}; Complex: operator + (const Complex & obj) {Complex temp; temp. re = re + obj. re; temp. im = im + obj. im; return temp;} Complex: operator! () {Complex temp; temp. re =-re; temp. im =-im; return temp;} ostream & operator <(ostream & OS, const Complex & c) {OS <
                          
                           
0) OS <"+" <
                           
                            
> (Istream & is, Complex & c) {is> c. re> c. im; return is;} int main (int argc, char * argv []) {Complex obj1 (1, 2), obj2 (3, 4); Complex obj3 = obj1 +! Obj2; cout <
                            
                             
> Obj3; cout <
                             
                              
The input and output operators can only be overloaded in the form of user meta functions, and should be implicitly called as cout <
                              
                                >, Note that the second parameter must be an object reference;
                               

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.