sas

ASP-Active Server Pages
Computer Fundamentals
What is computer?
Software
Hardware
Operating System
Programming Language
C and C++
Java
C#
Python
Visual Basic
Jscript.NET
Web Development
HTML
ASP
PHP
JavaScript
VBScript
.NET
.NET Micrsoft
ASP.NET
.NET Mobile
Forum
VB/VB.NET
ASP/ASP.NET
VBScript
JavaScript

Topic: C++-Language


Operator Overloading
Operator overloading is the ability to tell the compiler how to perform a certain operation when its corresponding operator is used on one or more variables. For example, the compiler acts differently with regards to the subtraction operator “-“ depending on how the operator is being used. When it is placed on the left of a numeric value such as -48, the compiler considers the number a negative value. When used between two integral values, such as 80-712, the compiler applies the subtraction operation. When used between an integer and a double-precision number, such as 558-9.27, the compiler subtracts the left number from the right number; the operation produces a double-precision number. When the - symbol is doubled and placed on one side of a variable, such as --Variable or Variable--, the value of the variable needs to be decremented; in other words, the value 1 shall be subtracted from it. All of these operations work because the subtraction operator “-” has been reconfigured in various classes to act appropriately.

C++ incorporates the option to use language standard operators between classes in addition to between fundamental types. For example:

int a, b, c;
a = b + c;

is perfectly valid, since the different variables of the addition are all fundamental types. Nevertheless, is not so obvious that we can perform the following operation (in fact it is incorrect):
struct { char product [50]; float price; } a, b, c;
a = b + c;
The assignation of a class (or struct) to another one of the same type is allowed (default copy constructor). What would produce an error would be the addition operation, that in principle is not valid between non-fundamental types.
But thanks to the C++ ability to overload operators, we can get to do that. Objects derived from composed types such as the previous one can accept operators which would not be accepted otherwise, and we can even modify the effect of operators that they already admit. Here is a list of all the operators that can be overloaded:

+ - * / = < > += -= *= /= << >>
<<= >>= == != <= >= ++ -- % & ^ ! |
~ &= ^= |= && || %= [] () new delete

To overload an operator we only need to write a class member function whose name is operator followed by the operator sign that we want to overload, following this prototype:
type operator sign (parameters);
Here you have an example that includes the operator +. We are going to sum the bidimensional vectors a(3,1) and b(1,2). The addition of two bidimensional vectors is an operation as simple as adding the two x coordinates to obtain the resulting x coordinate and adding the two y coordinates to obtain the resulting y. In this case the result will be
(3+1,1+2) = (4,3).

// vectors: overloading operators example
#include <iostream.h>

class CVector {
public:
int x,y;
CVector () {};
CVector (int,int);
CVector operator + (CVector);
};

CVector::CVector (int a, int b) {
x = a;
y = b;
}

CVector CVector::operator+ (CVector param) {
CVector temp;
temp.x = x + param.x;
temp.y = y + param.y;
return (temp);
}

int main () {
CVector a (3,1);
CVector b (1,2);
CVector c;
c = a + b;
cout << c.x << "," << c.y;
return 0;
}

 



































Prev

 







 


 

Error in my_thread_global_end(): 1 threads didn't exit