CompuServe Messages

C,C++ Books

    31-Mar-94 07:02:03
Sb: #40291-C,C++ Books
Fm: david benn 100033,605
To: Patrick Walsh 74252,2750
Patrick, The rest of my message was truncated by the system it seems. Here's the rest… In this case, SHAPE may have a single method (member function): area(). CIRCLE and SQUARE would inherit this (and in a real shape class, other data members and member functions too) and then ADD EXTRA data members and functions. You could derive a hundred other shape classes from SHAPE and they could all be compared to one another so long as they implemented an area function. The ideas of one class inheriting the properties of another and delaying the decision about which method to employ until run-time are very powerful. Another powerful feature in C++ is polymorphism which while not strictly OO, is great when combined with OOP. The example I just gave is a kind of polymorphism since one function can handle more than one class type implicitly. Here's a clearer example of polymorphism: suppose you want to add two strings together. In BASIC, strings are a first-class data type so you can add strings together easily. The "+" operator is said to be overloaded since you can add numbers AND strings together, ie. "+" is a polymorphic operator. In C++ it is possible to create a string class such that EVEN THOUGH STRINGS ARE NOT A FIRST-CLASS DATA TYPE IN C++, they can become so. You can overload "+" and "=" so that the following is possible: stringA = stringB + stringC You can also overload "*" to multiply matrices, etc. Jim mentioned the use of cin and cout (iostreams), which is another example of polymorphism. There are other features in C++ such as templates which allows you to create a generic stack class for example. You can later declare a stack of integers, reals, etc from a SINGLE class template. This is a kind of macro expansion facility. If you (or anyone else) would like some C++ code examples for anything mentioned above (or anything else in C++), please feel free to ask. Also, I think it's important to mention that unlike C, there is still no official C++ standard yet. The first ANSI/ISO draft standard is due to be released for public comment within the next couple of months. However, the standard C library will be included (with minor mods). I/O streams is also a part of the standard. There are differences between compilers at present which the standard will hopefully resolve down the track. Regards, David Benn (author of ACE)