There are many programming languages in the world, and almost as many programming styles (sometimes called paradigms). Three styles that have appeared in this book are procedural, functional, and object-oriented. Although C++ is usually thought of as an object-oriented language, it is possible to write C++ programs in any style. The style I have demonstrated in this book is pretty much procedural. Existing C++ programs and C++ system libraries are written in a mixture of all three styles, but they tend to be more object-oriented than the programs in this book.
It's not easy to define what object-oriented programming is, but here are some of its characteristics:
Recently object-oriented programming has become quite popular, and there are people who claim that it is superior to other styles in various ways. I hope that by exposing you to a variety of styles I have given you the tools you need to understand and evaluate these claims.
There are two types of functions in C++, called nonmember functions and member functions. So far, every function we have written has been a nonmember function. Member functions are declared inside class defintions. Any function declared outside of a class is a nonmember function.
Although we have not written any member functions, we have invoked some. Whenever you invoke a function "on" an object, it's a member function. Also, the functions we invoked on strings in Chapter 7 were member functions.
Anything that can be written as a nonmember function can also be written as a member function, and vice versa. Sometimes it is just more natural to use one or the other. For reasons that will be clear soon, member functions are often shorter than the corresponding nonmember functions.
When you invoke a function on an object, that object becomes the current object. Inside the function, you can refer to the instance variables of the current object by name, without having to specify the name of the object.
You can also refer to the current object through the keyword this. We have already seen this in an assignment operator in Section 11.2. However, the this keyword is implicit most of the time, so you will rarely find any need for it.
Continuing the example from the previous chapter, we will consider a class definition for complex numbers. Complex numbers are useful for many branches of mathematics and engineering, and many computations are performed using complex arithmetic. A complex number is the sum of a real part and an imaginary part, and is usually written in the form x + yi, where x is the real part, y is the imaginary part, and i represents the square root of –1. Thus, i · i = -1.
The following is a class definition for a new object type called Complex:
class ComplexThere should be nothing surprising here. The instance variables are two doubles that contain the real and imaginary parts. The two constructors are the usual kind: one takes no parameters and assigns default values to the instance variables, the other takes parameters that are identical to the instance variables.
In main, or anywhere else we want to create Complex objects, we have the option of creating the object and then setting the instance variables, or doing both at the same time:
Complex x;Let's look at some of the operations we might want to perform on complex numbers. The absolute value of a complex number is defined to be √(x2 + y2). Our abs function will be a pure function that computes the absolute value of a Complex object. Written as a nonmember function, it looks like this:
// nonmember functionThis version of abs calculates the absolute value of c, the Complex object it receives as a parameter. The next version of abs is a member function; it calculates the absolute value of the current object (the object the function was invoked on). Thus, it does not receive any parameters:
class ComplexI removed the unnecessary parameter to indicate that this is a member function. Inside the function, I can refer to the instance variables real and imag by name without having to specify an object. C++ knows implicitly that I am referring to the instance variables of the current object. If I wanted to make it explicit, I could have used the keyword this:
class ComplexBut that would be longer and not really any clearer. To invoke this function, we invoke it on an object, for example
Complex y (3.0, 4.0);Another operation we might want to perform on complex numbers is addition. You can add complex numbers by adding the real parts and adding the imaginary parts. Written as a nonmember function, that looks like:
Complex Add (Complex& a, Complex& b) {To invoke this function, we would pass both operands as arguments:
Complex sum = Add (x, y);Written as a member function, it would take only one argument, which it would add to the current object:
Complex Add (Complex& b) {Again, we can refer to the instance variables of the current object implicitly, but to refer to the instance variables of b we have to name b explicitly using dot notation. To invoke this function, you invoke it on one of the operands and pass the other as an argument.
Complex sum = x.Add (y);From these examples you can see that the current object (this) can take the place of one of the parameters. For this reason, the current object is sometimes called an implicit parameter.
As yet another example, we'll look at conjugate, which is a modifier function that transforms a Complex number into its complex conjugate. The complex conjugate of x + yi is x - yi.
As a nonmember function, this looks like:
void conjugate (Complex& c) {As a member function, it looks like
void conjugate () {By now you should be getting the sense that converting a function from one kind to another is a mechanical process. With a little practice, you will be able to do it without giving it much thought, which is good because you should not be constrained to writing one kind of function or the other. You should be equally familiar with both so that you can choose whichever one seems most appropriate for the operation you are writing.
For example, I think that Add should be written as a nonmember function because it is a symmetric operation of two operands, and it makes sense for both operands to appear as parameters. It just seems odd to invoke the function on one of the operands and pass the other as an argument. (Actually, in the next section you'll learn of a method called operator overloading which eliminates the need for explicitly calling functions like Add.)
On the other hand, simple operations that apply to a single object can be written most concisely as member functions (even if they take some additional arguments).
There are two operators that are common to many object types: << and =. << converts the object to some reasonable string representation so it can be outputted, and = is used to copy objects.
When you output an object using cout, C++ checks to see whether you have provided a << definition for that object. If it can't find one, it will refuse to compile and give an error such as
complex.cpp:11: no match for `_IO_ostream_withassign & << Complex &'Here is what << might look like for the Complex class:
ostream& operator << (ostream& os, Complex& num) {(This definition should not be within the Complex class, since it is not a member function of Complex objects. However, as for all nonmember functions, for this to work, the real and imag fields would have to be made public or corresponding accessor functions would need to be defined.) Whenever you pass an object to an output stream such as cout, C++ invokes the << operator on that object and outputs the result. In this case, the output is 1 + 2i.
The return type for << is ostream&, which is the datatype of a cout object. By returning the os object (which, like ostream, is just an abbreviation of output stream), you can string together multiple << commands such as
cout << "Your two numbers are " << num1 << " and " << num2 << endl;To illustrate why that's a good thing, consider what you would be forced to do if you didn't return the ostream object:
cout << "Your two numbers are ";Because the first example allows stringing << statements together, all the display code fits easily on one line. The output from both statements is the same, displaying "Your two numbers are 3 + 2i and 1 + 5i".
This version of << does not look good if the imaginary part is negative. As an exercise, fix it.
Unlike the << operator, which refuses to output classes that haven't defined their own definition of that function, every class comes with its own =, or assignment, operator. This default operator simply copies every data member from one class instance to the other by using the = operator on each member variable.
When you create a new object type, you can provide your own definition of = by including a member function called operator =. For the Complex class, this looks like:
const Complex& operator = (const Complex& b) {By convention, = is always a member function. It returns the current object. (Remember this from Section 11.2?) This is similar to how << returns the ostream object, because it allows you to string together several = statements:
Complex a, b, c;In the above example, c is copied to b, and then b is copied to a. This is because = associates to the right, and so "a = b = c" is an abbreviation for "a = (b = c)". The result is that all three variables contain the data originally stored in c. While not used as often as stringing together << statements, this is still a useful feature of C++.
The purpose of the const in the return type is to prevent assignments such as:
(a = b) = c;This is a tricky statement, because you may think it should just assign c to a and b like the earlier example. However, in this case the parentheses actually mean that the result of the statement a = b is being assigned a new value, which would actually assign it to a and bypass b altogether. By making the return type const, we prevent this from happening.
As you might expect, it is legal and common to invoke one member function from another. For example, to normalize a complex number, you divide through (both parts) by the absolute value. It may not be obvious why this is useful, but it is.
Let's write the function normalize as a member function, and let's make it a modifier.
void normalize () {The first line finds the absolute value of the current object by invoking abs on the current object. In this case I named the current object explicitly, but I could have left it out. If you invoke one member function within another, C++ assumes that you are invoking it on the current object.
As an exercise, rewrite normalize as a pure function. Then rewrite it as a nonmember function.
If you have both member functions and nonmember functions in the same class definition, it is easy to get confused. A common way to organize a class definition is to put all the constructors at the beginning, followed by all the member functions and then all the nonmember functions.
You can have a member function and a nonmember function with the same name, as long as they do not have the same number and types of parameters. As with other kinds of overloading, C++ decides which version to invoke by looking at the arguments you provide.
Since there is no current object in a nonmember function, it is an error to use the keyword this. If you try, you might get an error message like: "Undefined variable: this." Also, you cannot refer to instance variables without using dot notation and providing an object name. If you try, you might get "Can't make a static reference to nonstatic variable..." This is not one of the better error messages, since it uses some non-standard language. For example, by "nonstatic variable" it means "instance variable." But once you know what it means, you know what it means.
The language feature that is most often associated with object-oriented programming is inheritance. Inheritance is the ability to define a new class that is a modified version of a previously-defined class (including built-in classes).
The primary advantage of this feature is that you can add new functions or instance variables to an existing class without modifying the existing class. This is particularly useful for built-in classes, since you can't modify them even if you want to.
The reason inheritance is called "inheritance" is that the new class inherits all the instance variables and functions of the existing class. Extending this metaphor, the existing class is sometimes called the parent class and the new class is called the subclass.
An an example of inheritance, we are going to take a message class and create a subclass of error messages. That is, we are going to create a new class called ErrorMessage that will have all the instance variables and functions of a Message, plus an additional member variable, errorCode, which will be displayed when the object is outputted.
The Message class definition looks like this:
class MessageAnd that's all there is in the whole class definition. A Message has two protected member variables: the source of the message and the text of the message. (Protected variables will be explained in a moment.) The constructor initializes these member variables from the two strings passed as arguments.
You probably noticed that there is a const floating in free-space after the getMessage function declaration. When variables are declared const (such as in the Message constructor), it indicates that the function can't modify their values. However, a const after a function declaration means that the function itself is const! Only member functions can use this feature, because what it means is that the function can't modify any member variables of its class. Think of it as if *this is marked const.
The virtual indicator at the beginning of getMessage is a very important feature of inheritance. When a function is marked virtual, it allows that function to be redefined in subclasses. We will use this feature to change the behavior of getMessage in the ErrorMessage class.
Now here is an example of an ErrorMessage class which extends the functionality of a basic Message:
class ErrorMessage : public MessageThe class declaration indicates that ErrorMessage inherits from Message. A colon followed by the keyword public is used to identify the parent class.
The ErrorMessage class has one additional member variable for an error code, which is added to the string returned from the getMessage function. It would serve to notify a user of the error code associated with whatever message they received. The constructor of ErrorMessage initializes errorCode after it initializes both the original member variables, source and message using the Message constructor of the parent class. This is the meaning of ": Message (src, msg)" at the end of first line of the ErrorMessage constructor. That is, the ErrorMessage constructor first calls Message (src, msg) to initialize the variables of the parent class and then executes errorCode = ec to initialize the additional member variable.
An important thing to note is that the getMessage function has been redefined in ErrorMessage. Now the returned string includes the error code of the message, as well as source and message strings from the parent class. This is why source and message were declared protected variables; this makes them visible to subclasses of Message but not in other parts of the program. That is, they are public to classes descended from Message, but otherwise they are like private variables.
Suppose we want to overload the << operator to call getMessage in order to display messages.
ostream& operator << (ostream& os, const Message& msg) {This function will take any Message object and display it by calling its getMessage function. Since the ErrorMessage class is inherited from the Message class, what that means is that every ErrorMessage object is also a Message object! This allows you to use displayMessage like this:
ErrorMessage error ("1234", "Hard drive", "Out of space");The code first creates an ErrorMessage object with three strings for the source, message, and error code. Then the error message is passed to <<. Inside <<, the Message object's getMessage function is called in order to get a string representation of the object for output. The resulting output is:
ERROR 1234: Hard drive: Out of spaceEven though the function thinks the object is a Message, and has probably never even heard of the ErrorMessage class, it is still calling a function defined in ErrorMessage. This is all because of the virtual keyword used in the getMessage declaration. All functions that are ever going to be redefined in subclasses must be declared virtual. Otherwise, << would not realize the object is an ErrorMessage and would go ahead and call the getMessage defined in Message instead.
As an excercise, remove the virtuals and recompile the program. See if you can predict the output before running it.
Inheritance is a powerful feature. Some programs that would be complicated without inheritance can be written concisely and simply with it. Also, inheritance can facilitate code reuse, since you can customize the behavior of build-in classes without having to modify them.
On the other hand, inheritance can make programs difficult to read, since it is sometimes not clear, when a function is invoked, where to find the definition. For example, in a GUI environment you could call the Redraw function on a Scrollbar object, yet that particular function was defined in WindowObject, the parent of the parent of the parent of the parent of Scrollbar.
Also, many of the things that can be done using inheritance can be done almost as elegantly (or more so) without it.
Revised 2009-11-04.