We shall illustrate the concept of object orientation from a simple example borrowed from mathematics. Suppose we would like to implement something like a polynomial. In the beginning it is usually a good idea to think of what we would like to be able to do with our new object. Therefore, we start with an application of a (not yet existing) type Polynomial. Consider the following piece of code:
1 // use the polynomial 2 int main() // this is the entry of the main program (called by the operating system) 3 { 4 Polynomial p(4); // create a polynomial of 4th degree 5 p[0] = 1; // 1*x^0 6 p[4] = 2; // 2*x^4 7 8 cout << "p = " << p << endl; // print it 9 return 0; 10 }We note: The input is freely formatted and everything behind a "//" is ignored as a comment. What does this program do?
Line by line walk through:
The output of this piece of code shall be:
p = 2*x^4 + 1