You are here: Articles > Programming > Programming

 See more articles about "Programming "

C++: Operator Overloading (even a little more complex)

 

How to Overload the operators: +=, -=, *=, /=, %=

Very similar to overloading the +,-,* operators within a class, as they all take a single parameter as such:



As with all the other Person will be the custom class.

Perhaps these operations combine the friends of 2 people to the friends of one of the persons.



+= Operator:



Person& operator+=(const Person& p1){

     this->friends = this->friends + p1.friends;

     return *this;

}







-= Operator:



Person& operator-=(const Person& p1){

     this->friends = this->friends - p1.friends;

     return *this;

}







*= Operator:



Person& operator*=(const Person& p1){

     this->friends = this->friends * p1.friends;

     return *this;

}







/= Operator:



Person& operator/=(const Person& p1){

     this->friends = this->friends / p1.friends;

     return *this;

}







%= Operator:



Person& operator%=(const Person& p1){

     this->friends = this->friends % p1.friends;

     return *this;

}







*As you can see they are all very similar, add in most cases must be defined within the case they are defining operations for. Not that i can see presidence for ever doing anything else.



*Also note that although there is no recipe on pointers, p1 uses the . to refer to it's category friends, as it is passed by reference (NOTE the &) while the implied Person object designated by this must use the -> pointer to access it's version. The difference being, that p1 represents the Person object and can access directly, while this merely points to a person object (eg knows where the object is).



Questions/Comments: william_a_wilson@hotmail.com

-William. (marvin_gohan)

 

Also see ...

Assembly: The Registers and Segments (MASM/TASM)
H3The name and type of the registers and segments usable by a 32 bit processor (Easily converted by a naming convention to 16 bit and even 64 bit)/H3PSince there are no other recipes on Assembly Language, it would be irrational to start anywhere but the basics of hardware in assembly programmi

Assembly: Flags
H3The EFlags register, what they, what they do and when they are set./H3PThe flags are located in a single 32 bit register (EFlags), each a single bit, thus holding a value of 1 true, or 0 false. br / br /I will list the flags by name, location in the register and use: br /*location will

Assembly: Code Template
H3The general template for designing an Assembly code program/H3Pdiv class="code" br /TITLE    br /COMMENT br /        br / br /.MODEL SMALL br /.STACK 100H br /.DATA br / br /.CODE br /.486 br /INCLUDE io.mac br /main    PR

Assembly: If (MASM/TASM)
H3How to implement the in assembly, the effect of the if statement, which is available to other languages./H3PBeing that Assembly is a low level language, as opposed to the high level most programmers are used to (eg: Java, C, C++, Perl, etc), it doesn't have all the same features that these l

Assembly: mov statment (MASM/TASM)
H3How to use mov/H3Pmov is a useful tool in assembly language, it can move constants, or dynamics such as offsets. br / br /span style="font weight: bold"mov/span takes 2 parameters, the first is the reciever and the second is the operand. br / br /span style="font weight: bold"E