next up previous
Next: Making it useful (i) Up: A short C++/OO tutorial: Previous: The declarative stuff

The implementation

Toggle Background


So far we have written a lot of declarative stuff but the program will actually still do nothing. If you put the two parts together into one file (the Polynomial declaration before the main program) and try to compile it (e.g. g++ Polynomial.C1) it will do perfectly well. However, linking will fail because there is no definition of the constructor and the output operator. What is missing is the implementation of these subroutines (so called "method"s). Or in other words: After telling the compiler what can be done we have to tell how this is done. These are in principle two completely different things. "In principle" only because you may do it at once. However, usually one should split the two things. Why? Because the how (the implementation) should be (and is usually) of no concern. The code of the implementation just messes up your declaration. If some one else (or you later) wants to use your type Polynomial he will be interested in what it provides and not how it achieves that. This is very important and even nothing new. Think of driving a car: There is a "common interface" everyone knows: A throttle, a break, a wheel, a gear box, etc. You do not have to know how the engine works ("implementation"). If everyone had to know how the engine works there would be probably much less drivers...

The separation of interface and implementation makes large problems understandable and manageable because one has to deal only with interfaces not implementations (which tend to be lengthy and contain too many details). Of course there is a price you have to pay: You must introduce the actual implementation with some declarative stuff in order to inform the compiler what this implementation belongs to.

The large amount of declarative stuff in a C++ program (which "does nothing") is often criticized by people who are used to use a language like "FORTRAN". They call C++ "pathetic". If you start with a project you may see no benefit from the declarative stuff but later with a growing package will learn to appreciate it. Declarations are essential for object oriented programming.

Now let us take a look at the whole program: Polynomial3.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 //-------------------------------------------------------------
 7 // declaration:
 8 class Polynomial : public vector<double> {  // declare a new type "Polynomial" which inheritas from a vector of doubles
 9 public: // the following is publicly visible
10     // constructor
11     Polynomial(int n);  // gets an integer as degree
12 };  // the class declaration of "Polynomial" ends here
13 
14 // define output operator
15 ostream & operator << (ostream & o, const Polynomial & p);
16 
17 //-------------------------------------------------------------
18 // implementation:
19 Polynomial::Polynomial(int n) :
20     vector<double>(n+1, 0)  // allocate space for the coefficients and initialize them to 0
21 {}  // nothing else to do
22 
23 
24 ostream & operator << (ostream & o, const Polynomial & p)
25 {
26     for ( int i=p.size()-1 ; i>=0 ; i-- )
27     {
28         if ( p[i]!=0 )
29         {
30             if ( i!=p.size()-1 )
31                 o << " + ";
32             o << p[i];
33             if ( i>0 )
34                 o << "*x^" << i;
35         }
36     }
37     return o;
38 }
39 
40 
41 //-------------------------------------------------------------
42 // use the polynomial
43 int main()  // this is the entry of the main program (called by the operating system)
44 {
45 Polynomial  p(4);   // create a polynomial of 4th degree
46     p[0] = 1;   // 1*x^0
47     p[4] = 2;   // 2*x^4
48 
49     cout << "p = " << p << endl;    // print it
50     return 0;
51 }
Download Full Source

This program may be compiled and linked (e.g. g++ Polynomials.C) and run. It will produce the output:

p = 2*x^4 + 1



Footnotes

... Polynomial.C1
We assume that you have installed the GNU C++ compiler on your system.

next up previous
Next: Making it useful (i) Up: A short C++/OO tutorial: Previous: The declarative stuff
Michael Hanrath 2006-05-02