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 and C++
, C++ for C Programmers
, C and C++ Programming
, Learning to program in C and C++
, 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 subject||updated|
|4560||Variables, Pointers and References - C and C++|
In C, you can address a memory location storing a value directly by its variable name, or you can access it via its address using a variable that contains that address - a pointer - declared using an * in the declaration, and you can then use:
* to mean contents of
& to mean ...
|4559||When do I use the this keyword in C++?|
Question: When do I use the this keyword in C++?
Answer: When I expliciy need (or want) to refer to a member variable or object within the current object.
|4377||Designing a base class and subclasses, and their extension, in C++|
My first technical post for 2015 - looking back at some of last year's new C++ examples.
Oh - OK - I'm writing this on 1st January and these examples were written and uploaded yesterday, so they're nothing like as old as it sounds.
On Monday and Tuesday, I wrote classes from scratch and looked at basic ...
|4375||Final examples for 2014 - and a look at our 2015 training course options|
Following on from yesterday's examples [here] in which I talked you through, with links to code samples, the design and creation of a class, starting off with a description of its behaviour in the form of a test program and adding a test object class too, I'm now adding two short sample programs to make ...
|4356||Object factories in C++, Python, PHP and Perl|
Raw materials are converted into a manufactured product in a factory or by a factory procedure, and the sort of product you can make depends on both the raw material that's supplied and on the capability of the factory.
And so it is with Object Oriented Computer Programming - supply raw material ...
|3979||Extended 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 ...
|3811||Associated 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 ...
|3508||Destructor 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?
|3251||C++ - 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 ...
|3244||C 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!)
|3142||Private 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 ...
|3123||C++ 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 ...
|3124||C++ - 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 ...
|3056||C++ - 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.
|2577||Complete 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
|2845||Objects 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 ...
|801||Simple 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 ...
|925||C++ - 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-> ...
|1819||Calling 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, ...
|1674||What 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 ...
|1572||C - 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.
|1217||What 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 ...
|831||Comparison 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 ...
|798||References 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
Some modules are available for download
as a sample of our material or under an Open Training Notes License
for free download from [here]
Topics covered in this module
Deriving one class from another - Inheritance.
Inheritance and access control.
Packaging classes into modules.
If you are looking for a complete course and not just a information on a single subject, visit our Listing and schedule
Well House Consultants specialise in training courses in
. We run
throughout the UK (and beyond for longer courses), and
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.