Condor, all good questions...let me take a moment to answer them.
Quote:I understand the concept of overloading a function - to allow a single function name to be used several times but take different numbers or types of parameters and the compiler makes sense of which actual function to use in a particular situation, determined by it's parameters. Am I correct in thinking that this is polymorphism? |
No. This is not polymorphism. Polymorphism isnt when a function behaves differently based on the parameters, its when a function behaves differently based upon the object it belongs to. We'll cover it in more detail in chapter 14. Until we've covered the concept of inheritance, I cant even begin to explain it, as inheritance is fundamental to the concept of polymorphism.
Quote:I've read the section about overloading operators and I don't have a clue why this is important - but don't want to deal with that just now as there's something more fundamental that I need to clarify. |
Actually, its kind of ironic that you say this. The problem that you're having with the i++ in the rest of the this paragraph is exactly why operator overloading is important. The example in 10.8 wont make any sense unless operator overloading makes sense.
Quote:In each of 10.7 and 10.8, the object called "i" is incremented and it's value printed to the screen but I didn't think that an object could actually hold any data. |
In 10.7 the code calls i.increment(). This is just a method of the class, and as you can see on line 13, all the increment() method does is increment Counter::itsVal. On line 26 and 28 of 10.7 i.GetItsVal() is called to return the value stored in itsVal, a member variable of class Counter. So you can see now that the 'i' isnt being incremented, the value 'itsVal' is being incremented.
In 10.8 they duplicate the code for 10.7 exactly, with one small addition. On line 15 they add the operator++ method. This is operator overloading. By adding this method to the class, it tells the compiler that when someone uses the unary increment (++) operator on this class, to call that method. So on line 29 of 10.8 the Increment method is once again called. On line 31, instead of calling Increment the writer just used the '++' operator, which indirectly called the operator++ method. You'll notice that on line 14-15 both Increment and operator++ do the same thing.
In a nutshell, operator overloading just allows class writers to determine what happens to their object when used in conjunction with various operators. For example....think of a vector math class. What happens when you add two vectors together? What happens when you subtract two vectors? etc...rather than having to write:
Vector vec1(1.0, 1.0, 1.0);Vector vec2(1.0, 1.0, 1.0);Vector vec3 = vec1.Add( vec2 );
Operator overloading allows me to write:
Vector vec1(1.0, 1.0, 1.0);Vector vec2(1.0, 1.0, 1.0);Vector vec3 = vec1 + vec2;
You'll notice that now I'm able to use the '+' operator just as I would mathematically. And the '+' operator for this class knows that when you add two vectors together, you're actually adding the components of the vector together and creating a new vector. C++ doesnt know that by default.
Hope this helps. Let us know if you've got more questions or if this didnt fully answer your question.
Cheers!