On 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
(written 2010-01-15, updated 2010-10-30)
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
C233 - C and C based languages - OO in C++ - beyond the basics [798] References and Pointers in C++ - (2006-07-10)
[801] Simple polymorphism example - C++ - (2006-07-14)
[831] Comparison of Object Oriented Philosophy - Python, Java, C++, Perl - (2006-08-13)
[925] C++ - just beyond the basics. More you can do - (2006-11-14)
[1217] What are factory and singleton classes? - (2007-06-04)
[1572] C - structs and unions, C++ classes and polymorphism - (2008-03-13)
[1674] What a lot of files! (C++ / Polymorphism demo) - (2008-06-12)
[1819] Calling base class constructors - (2008-10-03)
[2845] Objects and Inheritance in C++ - an easy start - (2010-07-01)
[3056] C++ - a complete example with polymorphism, and how to split it into project files - (2010-11-16)
[3123] C++ objects - some short, single file demonstrations - (2011-01-07)
[3124] C++ - putting the language elements together into a program - (2011-01-08)
[3142] Private and Public - and things between - (2011-01-22)
[3244] C and C++ - preprocess, compile, load, run - what each step is for - (2011-04-12)
[3251] C++ - objects that are based on other objects, saving coding and adding robustness - (2011-04-17)
[3508] Destructor methods in C++ - a primer - (2011-11-05)
[3811] Associated Classes - using objects of one class within another - (2012-07-21)
[3979] Extended and Associated objects - what is the difference - C++ example - (2013-01-18)
[4356] Object factories in C++, Python, PHP and Perl - (2014-12-19)
[4375] Final examples for 2014 - and a look at our 2015 training course options - (2014-12-31)
[4377] Designing a base class and subclasses, and their extension, in C++ - (2015-01-01)
[4559] When do I use the this keyword in C++? - (2015-10-29)
[4560] Variables, Pointers and References - C and C++ - (2015-10-29)
C232 - C and C based languages - Defining and using classes in C++ [1925] Introduction to Object Oriented Programming - (2008-12-06)
[2578] Where are your objects stored in C++? - (2010-01-16)
[2579] Creating, setting up and using objects in C++ - (2010-01-16)
[3250] C++ - how we teach the language and the concepts behind the language - (2011-04-17)
[3716] Learning C++ - a design pattern for your first class - (2012-05-02)
[3721] Naming blocks of code, structures and Object Orientation - efficient coding in manageable chunks - (2012-05-06)
[3810] Reading files, and using factories to create vectors of objects from the data in C++ - (2012-07-21)
[3978] Teaching OO - how to avoid lots of window switching early on - (2013-01-17)
[4129] Simple OO demonstration in C++, comparison to Python - (2013-07-01)
[4372] Template / design pattern for C++ constructor and accessors - (2014-12-29)
[4565] Allocation of memory for objects in C++ - Stack v Heap - (2015-10-31)
C051 - C and C based languages - C++ - General [2370] C++, Python, and other training - do we use an IDE - (2009-08-21)
[2504] Learning to program in ... - (2009-11-15)
[2536] All the Cs ... and Java too - (2009-12-13)
[2763] Our C and C++ training course are on Open Source platforms - (2010-05-13)
[2851] Further C++ material - view new or old - (2010-07-04)
[3052] Getting your C++ program to run - (2010-11-15)
[3067] Using C and C++ functions in the same program - how to do it - (2010-11-24)
[3129] Extra courses - C and C++ - (2011-01-12)
[3155] Rake - a build system using code written in Ruby - (2011-02-03)
[3587] C++ Courses - do I get official certification at the end of my Well House course? - (2012-01-20)
[3809] Dwarf Exception Unwind Info - (2012-07-20)
[4335] Flexible public courses - residential or commuting, programming newcomer or experienced, C or C++ - (2014-11-30)
[4355] C++ in 2 days - (2014-12-18)
Some other Articles
Snow scenes - and how snow effected our businessC course inspires new teaching examplesComplete teaching example - C++, inheritance, polymorphismWhat does const mean? C and C++Sharing variables between files of code in C - externSummary of Wiltshire Core Strategy responsesC Structs - what, how and whyThe what and why of C pointers