HomeComputer programmingC++: Main Operator Overloading

C++: Main Operator Overloading

This tech recipe covers how to Overload the common operators: =, +, -, *.


Overloading an operator allows for custom operations on your own objects.
*For example, if you want to add to person objects (i.e., marriage), then you overload the + operator and define what is to be done when you add the objects.

*For all my examples, I will be using Person as the class which is my personally defined object.

+ Operator:
The + operator takes two operands if it is declared outside of the Person class:

//+Operator Overloader
Person& operator+(const Person& p1, const Person& p2){
//code goes goes in here
}

The + operator takes one operand if it is declared inside the Person class:

//+Operator Overloader
Person& operator+(const Person& p2){
//code goes goes in here
}

*The object p1 can be accesed as a pointer as well with the keyword this.

– Operator:
The – operator takes two operands if it is declared outside of the Person class:

//-Operator Overloader
Person& operator-(const Person& p1, const Person& p2){
//code goes goes in here
}

The – operator takes one operand if it is declared inside the Person class:

//-Operator Overloader
Person& operator-(const Person& p2){
//code goes goes in here
}

*The object p1 can be accessed as a pointer as well with the keyword this.

* Operator:
The * operator takes two operands if it is declared outside of the Person class:

//*Operator Overloader
Person& operator*(const Person& p1, const Person& p2){
//code goes goes in here
}

The * operator takes one operand if it is declared inside the Person class:

//*Operator Overloader
Person& operator*(const Person& p2){
//code goes goes in here
}

*The object p1 can be accesed as a pointer as well with the keyword this.

You may wish to write two or more of any of these overloaders, depending on the nature of your program, to include adding, subtracting and multiplying by integers, Strings or other objects.
(e.g., I personally wrote a Polynomial class as an assignment in university to apply scalars to Polynomials as well as multiply polynomials requiring two * operator overloaders.)

= Operator:

Personl operator=(const Person& eqR){ //R for right-hand side of = sign
//code would be similar to:
Person *p = new Person(); //requires a default constructor
//copy all fields from eqR to p here
//(using p-> to assign left side, and this-> for right side)
return *p; //incase you wish to assign an x = y = z
}

<< Operator:

ostream& operator<<(ostream& out, const Polynomial& p){ //your cout<< statements will be out<< inside this function return out; //must return the ostream object to continue cout through the rest of your program }

*Note the use of out to replace cout here. Thus your code looks similar.
The ostream object must be returned, or you risk losing the use of cout<< for the rest of your program.

Questions/Comments: [email protected]
-William. § (marvin_gohan)

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

LATEST REVIEWS

Recent Comments

error: Content is protected !!