Next: The implementation
Up: A short C++/OO tutorial:
Previous: A short C++/OO tutorial:
Toggle Background
In the above piece of code we assumed that there is a type Polynomial
(exactly like there is an intrinsic type "int" or "double").
However, currently the compiler (and we ourselves without knowledge of
mathematics) do not know
what a Polynomial is and how it may be used.
Therefore we must tell the compiler there is a new type
Polynomial and what can be done with it:
Polynomial2.C
1 #include <iostream> // use the iostream library (for input and output)
2 #include <vector> // use the STL type vector (a container type)
3
4 using namespace std; // add the namespace "std" to standard scope resolution
5
6 class Polynomial : public vector<double> { // declare a new type "Polynomial" which inheritas from a vector of doubles
7 public: // the following is publicly visible
8 // constructor
9 Polynomial(int n); // gets an integer as degree
10 }; // the class declaration of "Polynomial" ends here
11
12 // define output operator
13 ostream & operator << (ostream & o, const Polynomial & p);
Line by line walk through:
- Line 1:
- Include (import) the iostream library for input/output.
The C++ grammar contains no reference to input/output operations.
Everything is highly configurable and flexible. iostream
provides a standard set of routines for input/output.
- Line 2:
- Include (import) the Standard Template Library (STL)
type vector<double>. This is
a dynamic array of doubles.
- Line 4:
- We shall use the "std"-namespace as a default. You can
think of this as the "PATH" variable from your shell. Namespaces avoid
name clashes in large projects.
- Line 6:
- A new class (type) Polynomial is declared. It in inherits
from "vector<double>". For now we shall think of inheritance as:
Polynomial can do everything a "vector<double>" does. It is
a "vector<double>". Inheritance simplifies code reusage.
For example the element access of lines
5 and 6 in the main programs is inherited from "vector<double>".
- Line 7:
- The following is accessible from the outside of the class.
An often question is: "Why should I restrict myself and disallow
access of certain class members?" The answer is easy: If you cannot access
the forbidden elements you cannot use them. Does this make sense? Yes.
You should hide implementation details.
Consider the following example: You design some great object and some
implementation of it. Later you come up with a better implementation.
If there is no reference from your other code to this implementation
you replace the original implementation with the new one. If the
interface did not change you are finished. One may generalize this
idea to so called "abstract classes" but this is beyond the scope of
this simple to tutorial.
You should remember to separate: What from How that is
interface from implementation.
- Line 9:
- Declaration of a constructor (a constructor has the same
name as the class itself). It "creates" (constructs) a new Polynomial.
This declaration is related to line 4 in the main program above.
- Line 10:
- End of class declaration.
- Line 13:
- Declaration of an output operator for Polynomials.
This declaration is related to line 8 in the main program above.
Now the compiler knows what a Polynomial can do.
What we have provided so far is the "declarative stuff" which actually does
nothing. However, this is the key point to an object oriented language because it
enables us to create really new things. In a way the declarations extend
the language and enable the compiler to understand what we would like to
do.
You might ask: Why do I have to "construct" objects?
Remember: We would like our own created type to be as integrated into the language
as any intrinsic type (as int or double). Therefore, we
have to tell the compiler how to handle them. To "handle them"
includes constructing and destructing them. There are so called
"default" constructors and destructors. These are declared and defined
implicitly and take no arguments. However, in our example we decided
that there is no "default" polynomial. Therefore we declared an explicit
constructor taking exactly one argument that is the degree.
In real applications you will often find also an explicit "destructor".
Next: The implementation
Up: A short C++/OO tutorial:
Previous: A short C++/OO tutorial:
Michael Hanrath
2006-05-02