Training, Open Source Computer Languages

This is page

Our email: • Phone: 01144 1225 708225

C and C based languages module C233
OO in C++ - beyond the basics
Exercises, examples and other material relating to training module C233. This topic is presented on public courses Learning to program in C++, C++ for C Programmers, C and C++ Programming

If you want to define one object that's similar (but not identical) in structure and behaviour to another, you do so through inheritance. If you then have an array of similar objects, you can have C++ decide which particular behavious to use on each element in turn and this is known as polymorphism. In C++, you can define a type of object that inherits its behaviour from seveal others if you wish, and you can group toghether a whole lot of similar types of objects into modules - both of these are more advanced facilities that we cover in this module.
Articles and tips on this subjectupdated
3979Extended and Associated objects - what is the difference - C++ example
When you use inheritance, you create a tree of object types, where one type is based on another. For example, you define a base class of "transport" and then you extend that to define similar classes with extras called "bus" and "train". That's a very different matter to using a class connected in ...
3811Associated Classes - using objects of one class within another
Wheh you're designing an object oriented class, you'll often use other objects within it. For example, you may design a Ferry object and within it use a Time object (for when it's scheduled to leave, perhaps):   Ferry *mersey = new Ferry(new Time(17,45),14); There's really no special syntax ...
3508Destructor methods in C++ - a primer
Constructor methods - named pieces of code which create a new object - are key to any object oriented program / programming. Without objects, an object oriented program isn't really an object oriented program, and they need to be created somehow. But what happens at the other end of the life cycle? When ...
3251C++ - objects that are based on other objects, saving coding and adding robustness
In a previous article ([here]), I talked about how we introduce the basic concepts of classes, methods and objects on our C++ courses. I'm now going to give you some links to the next series of example which I wrote during that same course. Having covered object basics, we moved on to look at related ...
3244C and C++ - preprocess, compile, load, run - what each step is for
C and C++ original source code goes through a number of steps on its way to becoming a runnable program. 1. The C Preprocessor is run on the original source. This takes the source lines that start with a # character and acts on them as directed. (It does a bit more too - more about that anon!) 2. ...
3142Private and Public - and things between
A public train service is one which is available for anyone to travel on; a private one only takes limited passengers as invites / made available by the operator. And there could be intermediate levels too - I understand that at times the British Transport Police protect the last train from Weymouth ...
3123C++ objects - some short, single file demonstrations
One of the key features of the object oriented paradigm is that it provides a system through which code that's naturally associated with one type of data is accessed through a single closely defined and controlled set of calls (its API / Application Programmer Interface) ... so it's natural on a larger ...
3124C++ - putting the language elements together into a program
On the final day of the C++ course yesterday, I demonstrated a number of advanced features and pulled together various strands that we had been learning through the week. At one level, each feature of the language can be explained and taught, but there's a further level that's need to show how they ...
3056C++ - a complete example with polymorphism, and how to split it into project files
One of the things that newcomers to Object Oriented projects find most difficult is to know how all the various parts of the OO paradigm are related to each other - and that's especially the case on larger C++ projects, where the number of files of source involved is likely to be substantial. Yesterday ...
2577Complete teaching example - C++, inheritance, polymorphism
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 2. ...
2845Objects and Inheritance in C++ - an easy start
We can end up with a lot of files when we do even a simple C++ example to show inheritance - a file for the main program, a file each for the base class and subclass, and a header file for the prototypes for each of the base class and subclass too. So it's much easier to do it all as one file - but ...
801Simple polymorphism example - C++
I've been preparing some new C++ notes, in particular showing how you can create an array of objects of different derived types, and then call methods on each object and have the run time environment select which particular piece of code is to be run each time around a loop (experienced OO programmers ...
925C++ - just beyond the basics. More you can do
Things you can do in C++ ... just a bit beyond the first basics of a class: 1. You can declare that a method is const if it doesn't change any of the instance variables and that will make it a bit more efficient when you run it. 2. You can refer to an instance variable within a method using this-> ...
1819Calling base class constructors
In all object oriented languages, you have a facility called inheritance where you can define one type of thing ("class of object") based on another, and the newly defined class ("subclass" or "extended class") takes the initial ("base") class as it starting point. In your code for your base class, ...
1674What a lot of files! (C++ / Polymorphism demo)
There were no less than SEVEN files in the example I wrote to show a "simple" demonstration of polymorphism in C++ yesterday ... 1. The methods for a base class of file objects - Film.cpp 2. The extra methods on top of the base class for a derived class of Blockbuster objects - Blockbuster.cpp 3. The ...
1572C - structs and unions, C++ classes and polymorphism
C's structs allow you to define a variable type in which you can hold a variety of elements by name ... and that's very similar to how you define all the variables within a C++ class. In a C++ class, though, you also define your methods which take that language forward into the Object Oriented world. C's ...
1217What are factory and singleton classes?
Do you find some of the OO terminolgy baffling? Once you've learnt about constructors and methods, inheritance, overloading and polymorphism and statics, you might think you're there. Then someone mentions a "factory class" or a "singleton" ... Fear not - factory and singleton classes are posh names ...
831Comparison of Object Oriented Philosophy - Python, Java, C++, Perl
There are two different philosophies that have been adopted by the authors of Object Oriented languages. The first approach is to set the thing up in such a way that a programmer who uses someone else's code as the basis for his isn't going to be trusted to use that other person's code in a sensible ...
798References and Pointers in C++
I always know that when I'm running a C Course, the concept of pointers will be one of the more difficult elements for some of the delegates to grasp. No big deal, I can explain them in a number of ways, provide examples from different angle, and we'll be over the hiccough in progress in not too long ...
Examples from our training material
Make_3   Makefile for our C++ inheritance / polymorphism demo (2)
accom2.cpp   Class storing strings within an object
accom3.cpp   An array of objects
accom4.cpp   An array of objects
accomchooser.cpp   Class using classes and derived classes with inheritance
allinone.cpp.ex   Inheritance and Polymorphism example (all in 1 file)
animal.cpp   the code for the base class (2)
animal.h   Header file for a base class (2)
beasts.cpp   Definition of polymorphic class methods
beasts.h   Definition of polymorphic class headers
booklet.cpp   Destructors - how and when they run
classinclass.cpp   Associated objects - one object used within another
first_inheritance.cpp   What is Inheritance / how is it implemented in C++ (1)
hotel.cpp   Class member code   Class descriptor
human.cpp   second subclass of animal - human (2)
human.h   extended subclass definintion
loader   Complete first Polymorphism example
makefile   Compile / Load instructions for examples in this module
melkshamhotel.cpp   Derived class member code   include file to define melkshamhotel
midi.cpp   Inheriting a Constructor
pert.cpp   Overloading, Inheritance, Virtual, Polymorphism and more
pet.cpp   code for second subclass - pet (2)
pet.h   Definition of subclass headers (2)
petite.cpp   Abstract base class, two subclasses, polymorphic array
ptx.cpp   inheritance and polymorphism - bus and train
regionhotel.cpp   Derived class member code   include file to define region hotel
second_inheritance.cpp   What is Inheritance / how is it implemented in C++ (2)
shapes_01.cpp   Complete inheritance and polymorphism demo
shapes_02.cpp   Complete demonstration - data stored on the heap
shapes_03.cpp   Overloading +
shapes_combined   All the files for the shape demo on one page!
small.cpp   Declare, define, use a class - all in one file
testh.cpp   First denmo of polymorphism
testh2.cpp   Demo of polymorphism across an array
tinin.cpp   base class, extended class, test code
trans.cpp   Associative classes example
Background information
Some modules are available for download as a sample of our material or under an Open Training Notes License for free download from
Topics covered in this module
Deriving one class from another - Inheritance.
Inheritance and access control.
Packaging classes into modules.
Extending Classes.
Multiple inheritance.
Complete learning
If you are looking for a complete course and not just a information on a single subject, visit our Listing and schedule page.

Well House Consultants specialise in training courses in Python, Perl, PHP, and MySQL. We run Private Courses throughout the UK (and beyond for longer courses), and Public Courses at our training centre in Melksham, Wiltshire, England. It's surprisingly cost effective to come on our public courses - even if you live in a different country or continent to us.

We have a technical library of over 700 books on the subjects on which we teach. These books are available for reference at our training centre. Also available is the Opentalk Forum for discussion of technical questions.

© WELL HOUSE CONSULTANTS LTD., 2014: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 899360 • EMAIL: • WEB: • SKYPE: wellho

PAGE: • PAGE BUILT: Wed Mar 6 10:39:20 2013 • BUILD SYSTEM: wizard