Early on this week's
C++ programming course (including the C fundamentals), I wrote a new and short example that defined and used a class of objects of type "Hotel". Really, this initial example was very much shorter than any real program you would write and it used a very limited number of the facilities, but it did (and does) show the first steps towards creating objects of a type.
We created a hotel by declaring it and passing in any initial parameters to its constructor method:
Hotel hilton(40);
We set variables within the object using the same . notation for members that we had used in the underlying C for structure members (perhaps there's a clue as to how it works internally there!):
hilton.roomcount = 18;
We also set variables / states / used our object via methods - a much
better way that setting them directly (come on the course - I'll explain why ;-) ):
hilton.setoccu(0.75,350);
And we called back results also using object methods - named pieces of code that run on the objects:
avoccu = hilton.getavoc();
Within the main source file for this program, we loaded in a header file that tells the compiler about the various methods which are availabe on our Hotel class, and the variables each hotel contains:
#include "g_hotel.h";
but, significantly, that header file does NOT contain the actual code of the hotel class methods - that's written and compiled up elsewhere, perhaps by another programmer or even another company, and may be shared between a whole lot of different programs. This is one of the critically useful elements of the Object Oriented approach - the separation out of code into separate logical and separately maintained files. All the person who is making use of the classes need to know is how to call them, what they return, and what their effect is.
The complete code from which the examples above are taken is
[here]. We went on to enhance the class, adding options to it ... so the header file became rather more complex than is really needed in this simple example ... and that extended header file is
[here]. And the source code for the Hotel class (final version from the course) is
[here].
• You'll find further (shared) use of the same class in our extended application demonstration - which used multiple classes built upon the single base class ("different types of hotel") on our site - there's quite a few files involved - so start off at the overview description
[here].
• You'll find details of how to store your objects in more permanent memory on the heap rather than on the stack
[here].
(written 2010-01-16)
5a95
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
C232 - C and C based languages - Defining and using classes in C++ [3978] Teaching OO - how to avoid lots of window switching early on - (2013-01-17)
[3810] Reading files, and using factories to create vectors of objects from the data in C++ - (2012-07-21)
[3721] Naming blocks of code, structures and Object Orientation - efficient coding in manageable chunks - (2012-05-06)
[3716] Learning C++ - a design pattern for your first class - (2012-05-02)
[3250] C++ - how we teach the language and the concepts behind the language - (2011-04-17)
[2578] Where are your objects stored in C++? - (2010-01-16)
[2577] Complete teaching example - C++, inheritance, polymorphism - (2010-01-15)
[1925] Introduction to Object Oriented Programming - (2008-12-06)
Some other Articles
Reducing image size on digital photos - PHPStarting to arrange the picture librarySnow scenes - and how snow effected our businessC course inspires new teaching examplesCreating, setting up and using objects in C++What does const mean? C and C++Sharing variables between files of code in C - externSummary of Wiltshire Core Strategy responses