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.
Adding a factory / static method
Object Orientation: Composite Objects example from a Well House Consultants training course
More on Object Orientation: Composite Objects [link]

This example is described in the following article(s):
   • Designing a base class and subclasses, and their extension, in C++ - [link]

Source code: Shapes003.cpp Module: Q909
// Putting together objects of related types

#include <iostream>
using namespace std;

class Container {
                double getDensity();
                virtual double getVolume() {}; // Code ONLY in subclasses
                virtual bool isItSharp(); // Code in Container AND in subclasses
                                                        // Needed to allow overriding in subclass
                static Container * Factory(double *); // class method - does NOT run on an object
                double density;
                int r,g,b;
class Ball : public Container {
                double getVolume();
                bool isItSharp();
                double d;
class Box : public Container {
                Box(double,double,double,int,int,int); // Overloading - two constructors
                                                                // Tells which based on params
                double getVolume();
                double x,y,z;
class Cube : public Box{

// -------------------------------

Container::Container (double dens, int r, int g, int b) { // Common code for all shapes
        density = dens;
        this->r = r;
        this->g = g;
        this->b = b;

bool Container::isItSharp() {
        return true;

Container * Container::Factory(double info[]) {
        if (info[0] == 1.0) {
                return (new Ball(info[1],info[2],(int)info[3],(int)info[4],(int)info[5]));
        if (info[0] == 2.0) {
                return (new Cube(info[1],info[2],(int)info[3],(int)info[4],(int)info[5]));
        // Error Handling here

bool Ball::isItSharp() {
        return false;

Ball::Ball (double d, double dens, int r, int g, int b) : Container(dens,r,g,b) {
        this->d = d;

Box::Box (double x, double y, double z, double dens, int r, int g, int b) : Container(dens,r,g,b) {
        this->x = x; // Extra values in addition to Container standards
        this->y = y;
        this->z = z;

Box::Box (double x, double y, double dens, int r, int g, int b) : Container(dens,r,g,b) {
        this->x = x;
        this->y = y;
        this->z = x; // First dimension is repeated

Cube::Cube (double xyz, double dens, int r, int g, int b) : Box(xyz, xyz, xyz, dens, r, g, b) {

double Container::getDensity() {
        return density;

double Box::getVolume() {
        return (x * y * z);

double Ball::getVolume() {
        return ((d * d * d / 8.0) * 4.0 / 3.0 * 3.14159);

// -------------------------------

int main() {

        bool verbose = true;
        int k;

        // An example of raw data for some sort of container
        // Let's say 1 = ball, 2 = cube
        double raw_data1[] = {1.0,180.0,15.5,200.0,200.0,155.0}; // ball
        double raw_data2[] = {2.0,170.0,15.5,200.0,200.0,155.0}; // cube
        double raw_data3[] = {1.0,170.0,15.5,200.0,200.0,155.0}; // ball

        Container * Items[9];
        Items[0] = new Ball(200.0,12.5,100,100,255);
        Items[1] = new Ball(180.0,15.5,200,200,155);
        Items[2] = new Box(100.0,150.0,18.5,20,40,160); // 100 x 150 x 100
        Items[3] = new Box(110.0,120.0,160.0,19.5,120,140,170); // Overloaded function (constructor)
        Items[4] = new Cube(90.0,29.5,220,240,177);
        Items[5] = new Cube(190.0,9.5,12,14,17);
                // Let the factory work out what our data is for!
        Items[6] = Container::Factory(raw_data1); // Static or class method
        Items[7] = Container::Factory(raw_data2); // Static or class method
        Items[8] = Container::Factory(raw_data3); // Static or class method

        for (k=0; k<9; k++) {
                double dens = Items[k]->getDensity();
                double volume = Items[k]->getVolume();
                if (verbose) {
                        cout << volume << " " << dens << endl;
                        if (Items[k]->isItSharp()) cout << "It's sharp" << endl;
Learn about this subject

Books covering this topic
Yes. We have over 700 books in our library. Books covering all the subjects we teach are listed here and when you've selected a relevant book we'll link you on to Amazon to order.

Other Examples
This example comes from our "Object Orientation: Composite Objects" training module. You'll find a description of the topic and some other closely related examples on the "Object Orientation: Composite Objects" module index page.

Full description of the source code
You can learn more about this example on the training courses listed on this page, on which you'll be given a full set of training notes.

Many other training modules are available for download (for limited use) from our download centre under an Open Training Notes License.

Other resources
• Our Solutions centre provides a number of longer technical articles.
• Our Opentalk forum archive provides a question and answer centre.
The Horse's mouth provides a daily tip or thought.
• Further resources are available via the resources centre.
• All of these resources can be searched through through our search engine
• And there's a global index here.

Web site author
This web site is written and maintained by Well House Consultants.

Purpose of this website
This is a sample program, class demonstration or answer from a training course. It's main purpose is to provide an after-course service to customers who have attended our public private or on site courses, but the examples are made generally available under conditions described below.

Conditions of use
Past attendees on our training courses are welcome to use individual examples in the course of their programming, but must check the examples they use to ensure that they are suitable for their job. Remember that some of our examples show you how not to do things - check in your notes. Well House Consultants take no responsibility for the suitability of these example programs to customer's needs.

This program is copyright Well House Consultants Ltd. You are forbidden from using it for running your own training courses without our prior written permission. See our page on courseware provision for more details.

Any of our images within this code may NOT be reused on a public URL without our prior permission. For Bona Fide personal use, we will often grant you permission provided that you provide a link back. Commercial use on a website will incur a license fee for each image used - details on request.

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2021: 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/resources/ex.php • PAGE BUILT: Sun Oct 11 14:50:09 2020 • BUILD SYSTEM: JelliaJamb