Complete teaching example - C++, inheritance, polymorphism
Archive - Originally posted on "The Horse's Mouth" - 2010-01-15 09:07:59 - Graham EllisOn yesterday's C++ course, I provided a final example which illustrated polymorphism, and showed how even the simple example was best written, split, ito no less that seven source files.
1. The main C++ source code - for my example, a simple demo test harness
See http://www.wellho.net/resources/ex.php4?item=c051/further.cpp
2. The C++ source code of the first class that my example used
See http://www.wellho.net/resources/ex.php4?item=c051/starredhotel.cpp
3. The C++ source code of the second class that my example used
See http://www.wellho.net/resources/ex.php4?item=c051/selfhotel.cpp
4. The C++ source code of the base class from which my two sample classes were derived.
See http://www.wellho.net/resources/ex.php4?item=c051/g_hotel.cpp
Because I need to include function / method prototypes into various files so that the comiler can check that calls are valid in isolation from the separate compiles, I have also provided separate header files. In theory it is possible to include these within the four source files above directly, but that is a TERRIBLE practice and you should load them through #includes ...
5. The header file defining the methods in the first used class
See http://www.wellho.net/resources/ex.php4?item=c051/starredhotel.h
6. The header file defining the methods in the second used class
See http://www.wellho.net/resources/ex.php4?item=c051/selfhotel.h
7. The header file defining the methods in the base class
See http://www.wellho.net/resources/ex.php4?item=c051/g_hotel.h
In essence, C++ is mostly about adding object orientation to C (thus meking it a completely different kettle of fish!), so I have loaded each of the course files above onto our web site so that you can see the complete example under the category "C++ in general". They illustrate a very large range of features - inheritance, overloading, polymorphism, virtual functions, etc ... all of which I'll explain to you on our C++ courses - in fact, this set of examples was written on the C++ course which ended yesterday, where I showed the delegate how we design and write object oriented programs in C++ from scratch.
We offer three public courses
Learning to program in C++ - 5 days, for delegates who have never programmed before in any programming language, or have done so VERY little.
See http://www.wellho.net/course/cffull.html
C and C++ programming - 4 days, for delegates who have programmed before, but not in C (on which C++ is based)
See http://www.wellho.net/course/cafull.html
C++ Programming - 2 days, for experienced C programmers who need to add the extras of C++ (and learn which parts of C they can gently retire!)
See http://www.wellho.net/course/cpfull.html
Update .... there's another full example too on our website - it's the one that's actually in the course notes and it comprises the following:
accom4.cpp - the main program
hotel.cpp - the base class for "hotel"
melkshamhotel.cpp - a derived class
regionhotel.cpp - another derived class
hotel.inc - header file for the base class
melkshamhotel.inc - header file for the first derived class
regionhotel.inc - header file for the second derived class
makefile - the build instruction file for Linux / Unix that ties the whole thing together.
One of the great things about good OO design is that you can reuse your low level code - your classes and objects - across a number of applications. Here are some further applications using all (or some) of those classes:
accom2.cpp, accomchooser.cpp and accom3.cpp