Computer Science Canada [WIP] C++ OOP Whirlwind |
Author: | wtd [ Thu Oct 26, 2006 5:24 pm ] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Post subject: | [WIP] C++ OOP Whirlwind | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Let's create a class!
This class does precisely nothing useful. However, what it does do is create a new type. We can declare a variable of this type.
We can dynamically allocate memory for that type.
We can delete it.
We can write a function which takes such a value as a parameter.
We can use that type in overloading existing functions.
Member functions!
So now we can just create a Foo object and call that method, right?
Not so fast. By default, everything in a class is private. We'll have to explicitly make that member fucntion public.
Now the code we'd written previously works. Or, at least it will if we actually define the member function we declared. Otherwise, you'll get a linker error when the linker goes looking for the member function that isn't actually defined.
Now, let's try calling that again.
Oops! The variable "f" hasn't yet been initialized.
Dodged a bullet there. No one likes dealing with segmentation faults. Only one problem remaining.
Now, we have a constant Foo object. Really, this makes little sense, as at this point all Foo objects are basically constant. However, this is a big C++ quirk, and one worth understanding. To make bar work in this case, we'd have to qualify it as being ok to call on a const object.
State
So far all Foo objects have been boring because they're all exactly the same. To differentiate them, each object needs to hold some internal state that can be different from other Foo objects. Now, in the above code sample, each Foo object will now have a member variable called "_bar." There's just one problem. Since everything in a class is private unless otherwise specified, there's no way to set the value of that variable. Construction
Constructors provide a way to initialize the state of an object. We may now write the following.
And the output will be as follows.
We may have more than one constructor, following C++ function overloading rules. Though constructors are not actually functions.
Here we've created a default constructor. Such a constructor will be called in either of the following situations.
It's important to note that we might also accomplish the above effect using default parameter values.
However, we cannot achieve the following effect using default values.
This is known as a copy constructor. It literally copies the state of another object into the new object. Being explicit
Previously our constructors have not been qualified as explicit. Without this, we could have had code of the sort that follows.
With the explicit qualifier, we now require one of the following.
But this also makes it impossible to write the following.
Instead we would have to:
Instead, perhaps we should :
Assignment
Previously we showed off assignment-style construction by leaving the copy constructor non-explicit. This only applies to construction. Once the object is constructed, other requirements come into play. Then we need to overload the assignment operator, as has been done above. Since this changes the state of the object, it is naturally not qualified by "const." Another operator
It's quite common, and highly useful to overload various operator for objects. A few of the more common ones are the equality and the less than operator. This permits various functions in the standard library to determine proper ordering of collections of objects. |