Capítulo 2 Marco teórico
2.3 Gestión de proyectos
2.3.2 Modelos de gestión
In this figure, I've used vectors A and B from the previous diagram. To
subtract A from B, I reversed the direction of B. This turns B into B. Now it is easy to lay the two vectors end to end and add them. The result is a new vector, C.
The vector class in Listing 5.2 supports both addition and subtraction. It does so
by providing its own functions for those operators. The prototypes are on lines 1720. In general, a prototype for overloading any operators follows the
pattern that these two use. That is, it first states the return type, which is often the name of the class. Next, it uses the C++ keyword operator to indicate
that an operator is being overloaded for the class. That is followed by the symbol for the operator itself-in this case, the + and - signs for the addition
and subtraction operator. Finally, the prototype presents the parameter list. Most of the operators that you'll overload are binary operators. That means they have two operands. Addition is a perfect example. Here's an integer addition:
3+5
You always add two things together, so you need two operands and an operator. In this case, the 3 and the 5 are the operands and the + is the
operator. If you're adding vectors, it works the same way. Here's a vector addition:
Warning
It is possible for you to overload operators in unusual ways. For example, you can overload the + operator to do subtraction and the
- operator to do addition. This is not a good idea. You should always
overload the operators to do what most people expect.
a + b
The format is just the same as an integer addition. The equation contains two vector operands, a and b, and the + operator.
When you add two integers in a C++ program, you use a notation that is similar to normal integer addition, like the one shown here:
int a = 3, b = 5; int c;
c = a + b;
This simply declares two integer variables, a and b, adds them, and stores the
result in the variable c. This code looks very normal and understandable to
anyone familiar with addition. When we overload the addition operator for any class, such as the vector class, we want our code to look essentially the same
way. Specifically, we'd like to be able to write our code like the following code fragment:
vector a(10,20); vector b(30,40); vector c;
c = a + b;
This is exactly the way that you can write your code when you overload the + operator.
Notice that the vectors a and b are now printed in bold lettering. This is actually the proper way to show vectors. Printing them in bold type indicates that you're adding vectors, not integer or
floating-point variables. Of course, this is just the convention used in books. Happily, we do not have to try and bold the variables in our programs. Compilers don't like that sort of thing.
Here's the code for the + and operators for the vector class:
inline vector vector::operator +( vector &rightOperand) { vector temp; temp.x = x + rightOperand.x; temp.y = y + rightOperand.y; return (temp); }
inline vector vector::operator -( vector &rightOperand) { vector temp; temp.x = x - rightOperand.x; temp.y = y - rightOperand.y; return (temp); }
This code fragment calls these two functions:
vector v1(10,20), v2(20,30), v3; v3 = v1 + v2;
Bear in mind that when your program calls a class's member function, it must use an object. For instance, calling the X() function from the vector class would
require a
vector variable: vector a;
a.X(10);
Here, the variable a is used to call X(). Calling your overloaded operator
functions also requires a class variable. In the previous vector example, the
object that calls the + function is v1. The variable v2 gets passed to the +
function through the parameter list. If you look back at Listing 5.2, you'll see that the name of the parameter for the + operator is rightOperator. Now you know
why.
The addition function contains the statement
temp.x = x + rightOperand.x;
Notice that there are three references to the private data member called x. The
leftmost one is the x for the temp variable. The rightmost x (rightOperand.x) is the x
for the right-hand operator in the addition. The one in the middle is just x. It
doesn't have an object name in front of it. That's because it's the x for the
object that was used to call the + function. The object that's used to call the
addition function is always the left-hand operand. Therefore, in the statement
v3 = v1 + v2;
the variable v1 calls the + function and v2 is copied into the parameter rightOperand.
The function adds the x and y values of v2 to the x and y values of v1, stores
them in the variable temp, and then uses the return statement to send the answer
back. The answer is then stored in v3.
Factoid
Even if your class only provides one version of an operator function, such as a + operator, it is said to be overloading that
operator for the class.