Home Accessibility Courses Twitter The Mouth Facebook Resources Site Map About Us Contact
For 2021 - online Python 3 training - see ((here)).

Our plans were to retire in summer 2020 and see the world, but Coronavirus has lead us into a lot of lockdown programming in Python 3 and PHP 7.
We can now offer tailored online training - small groups, real tutors - works really well for groups of 4 to 14 delegates. Anywhere in the world; course language English.

Please ask about private 'maintenance' training for Python 2, Tcl, Perl, PHP, Lua, etc.
Creating, setting up and using objects in C++

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 ;-) ):

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)

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++
  [1925] Introduction to Object Oriented Programming - (2008-12-06)
  [2577] Complete teaching example - C++, inheritance, polymorphism - (2010-01-15)
  [2578] Where are your objects stored 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)

Back to
Where are your objects stored in C++?
Previous and next
Horse's mouth home
Forward to
C course inspires new teaching examples
Some other Articles
Reducing image size on digital photos - PHP
Starting to arrange the picture library
Snow scenes - and how snow effected our business
C course inspires new teaching examples
Creating, setting up and using objects in C++
What does const mean? C and C++
Sharing variables between files of code in C - extern
Summary of Wiltshire Core Strategy responses
4759 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96 at 50 posts per page

This is a page archived from The Horse's Mouth at http://www.wellho.net/horse/ - the diary and writings of Graham Ellis. Every attempt was made to provide current information at the time the page was written, but things do move forward in our business - new software releases, price changes, new techniques. Please check back via our main site for current courses, prices, versions, etc - any mention of a price in "The Horse's Mouth" cannot be taken as an offer to supply at that price.

Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2022: 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.net/mouth/2579_Cre ... in-C-.html • PAGE BUILT: Sun Oct 11 16:07:41 2020 • BUILD SYSTEM: JelliaJamb