C++ operator overloading

Discussion in 'Programming General' started by blindkilla, Jul 13, 2010.

C++ operator overloading
  1. Unread #1 - Jul 13, 2010 at 9:53 AM
  2. blindkilla
    Joined:
    Jun 22, 2005
    Posts:
    1,896
    Referrals:
    0
    Sythe Gold:
    6
    Discord Unique ID:
    282000633404456960
    Discord Username:
    sogord

    blindkilla Guru
    $25 USD Donor New

    C++ operator overloading

    Operator overloading is very useful in c++.

    I'll show a quick example of overloading an assignment operator.

    Consider the following class:

    Code:
    class user {
    	char *userName;
    	char *email;
    	int dateOfBirth;
    public:
    	user(char *userNamePar = NULL, char *emailPar = NULL, int dateOfBirthPar = -1) : dateOfBirth(dateOfBirthPar) {
    		
    		if (userNamePar != NULL) {
    			username = new char[strlen(userNamePar) + 1];
    			strcpy(userName, userNamePar);
    		} else {
    		    userName = userNamePar;
    		}
    		
    		if (emailPar != NULL) {
    			email = new char[strlen(emailPar) + 1];
    			strcpy(email, emailPar);
    		} else {
    		    email = emailPar;
    		}
    
    	}
    	
    	~user() { 
    		delete[] userName;
    		delete[] email;
    	}
    	
    };
    
    If you wanted to assign the values of one class to another, you would have to write methods that return the values in
    the variables, which would be annoying. You can avoid the need to do that by overloading the assignment operator.

    Here is the class, including the two overloaded operators:

    Code:
    class user {
    	char *userName;
    	char *email;
    	int dateOfBirth;
    public:
    	user(char *userNamePar = NULL, char *emailPar = NULL, int dateOfBirthPar = -1) : dateOfBirth(dateOfBirthPar) {
    		
    		if (userNamePar != NULL) {
    			username = new char[strlen(userNamePar) + 1];
    			strcpy(userName, userNamePar);
    		} else {
    		    userName = userNamePar;
    		}
    		
    		if (emailPar != NULL) {
    			email = new char[strlen(emailPar) + 1];
    			strcpy(email, emailPar);
    		} else {
    		    email = emailPar;
    		}
    
    	}
    	
    	~user() { 
    		delete[] userName;
    		delete[] email;
    	}
    	
    	user& operator=(const user &userObj) {
    	
    			if (this != &userObj) // make sure you aren't assigning an object to its self
    			{
    				/* In case we are assigning an object to one that isn't empty, we need to deallocate the memory for its current variables */
    				if (userName)
    					delete[] userName;
    				if (email)
    					delete[] email;
    			
    				/* Typical validation.  No need to allocate memory if it is a NULL value */
    				if (userObj.userName != NULL) {
    					username = new char[strlen(userObj.userName) + 1];
    					strcpy(userName, userObj.userName);
    				} else {
    					userName = userObj.userName;
    				}
    		
    				if (userObj.email != NULL) {
    					email = new char[strlen(userObj.email) + 1];
    					strcpy(email, userObj.email);
    				} else {
    					email = userObj.email;
    				}
    				
    				dateOfBirth = userObj.dateOfBirth;
    			}
    			
    			return *this; // Return the address of the new object to the one it's being assigned to
    		}
    	
    };
    
    Now the code that will use it:

    Code:
    int main() {
    
    	user user01 = new user("test", "[email protected]");
    	user user02 = new user("Blindkilla", "[email protected]", 1500);
    	
    	user01 = user02;
    
    	return 0;
    }
    
    In that case, the values in object user02, will be in object user01. From there you could have an function that allows you to set new values into an object and
    insert a new user. You can already see how overloading the assingment operator has proven to be useful.

    The first user was a dummy to test a system. So you want to get rid of it and add the legit user to be number 1.
     
  3. Unread #2 - Sep 14, 2010 at 11:57 PM
  4. stocktips02
    Joined:
    Sep 13, 2010
    Posts:
    1
    Referrals:
    0
    Sythe Gold:
    0

    stocktips02 Newcomer

    C++ operator overloading

    hey blindkilla,

    Great !!! operator overloading topic is just very tough in c++. But with your
    e.g I am able to clear my concept abt operator overloading.

    thnx a lots 4 sharing...

    Nifty Tips
    Sure Shot Tips
     
< Need help | omg homework help!!! >

Users viewing this thread
1 guest


 
 
Adblock breaks this site