Tag: C++ / MFC

  • Factory Patterns in C++

    In a nutshell, the factory design pattern enables the practitioner to create objects that share a common theme but without having to specify their exact classes. In essence the factory pattern is used to “Define an interface for creating an object, but let the classes that implement the interface decide which class to instantiate. The Factory method lets a class defer instantiation to subclasses.
    (more…)

  • How to interface with Excel in C++

    Interfacing Excel in C++ is task that I needed to overcome recently, so I thought I would post some code and instructions on the said topic. Some online articles that I found to be useful include the following:
    (more…)

  • Getting started with Windows GDI graphics applications in C++

    A guide to getting started with Windows graphics applications for the very first time. The Windows Graphics Device Interface (GDI) forms the basis of drawing lines and objects, and from this device contexts. I will not go into any detail about these concepts in this post. I just want to show a simple means of getting started with things like the drawing of lines and objects in Windows applications. There is nothing stopping you from reading further on the subject to increase your understanding.
    (more…)

  • Kruskal’s Algorithm in C++

    A simple C++ implementation of Kruskal’s algorithm for finding minimal spanning trees in networks. Though I have a previous posting that accomplishes exactly the same thing, I thought that a simple implementation would be useful, one using a straightforward Graph data structure for modelling network links and nodes, does not have a graphical user interface and does not use the Boost Graph Library, which can be complicated to use and not to everyone’s preference.
    (more…)

  • Static Classes and Static Class Members in C#

    Static classes – what are they?

    In C#, static classes have one important difference to that of non-static classes: they cannot be instantiated. That is, the new operator cannot be used to create an instance of the class type. Since no instance of a static class can exist, its members can only be accessed using the class name itself.
    (more…)

  • A Simple Binary Tree Implementation in C++

    A very basic binary tree implementation in C++ that defines a binary tree node, adds new nodes and prints the tree.

    #include <stdio.h>
    
    class Node
    {
    public:
    	Node( int v )
    	{
    		data = v;
    		left = 0;
    		right = 0;
    	}
    
    	int data;
    	Node* left;
    	Node* right;
    };
    
    void Add( Node** root, Node* n )
    {
    	if ( !*root  )
    	{
    		*root = n;
    		return;
    	}
    
    	if ( (*root)->data < n->data )
    	{
    		Add( &(*root)->right, n );
    	}
    	else
    	{
    		Add( &(*root)->left, n );
    	}
    }
    
    void Print( Node* node )
    {
    	if ( !node )  return;	
    	Print( node->left );
    	printf( "value = %i\n", node->data );
    	Print( node->right );
    }
    
    int main()
    {
    	Node* root = 0;
    
    	Add( &root, new Node( 1 ) );
    	Add( &root, new Node( 2 ) );
    	Add( &root, new Node( -1 ) );
    	Add( &root, new Node( 12 ) );
    
    	Print( root );
    	return 0;
    }
    

    Output:

  • Hash Tables as a means of fast lookup in STL / C++.

    Introduction

    In a previous life I was involved in the design of routing optimization software for the telecoms industry. Finding the least cost route for a traffic demand between communicating network sites necessitates a search for all the tariffs provided by all of the carriers. (more…)

  • A Recursive Algorithm to Find all Paths Between Two Given Nodes in C++ and C#

    Problem Outline

    This I tackled previously when working on the design and implementation of routing optimization algorithms for telecommunications networks. Given that a wide area network with nodes and interconnecting links can be modelled as a graph with vertices and edges, the problem is to find all path combinations (more…)

  • Implementing Kruskal’s Algorithm in C#

    This post is essentially a blatant lifting of Omar Gamil’s CodeProject article on the same subject. I have been using the project as means of getting into C# programming and using things like Windows Forms etc in Visual Studio environments for the first time. (more…)

  • The Big Three in C++

    Constructor, destructors and assignment operators

    There is a rule of thumb in C++ that if a class defines a destructor, constructor and copy assignment operator – then it should explicitly define these and not rely on their default implementation.

    Why do we need them?

    In the example below, the absence of an explicit copy constructor will simply make an exact copy of the class and you end up with two classes pointing to the same memory address – not what you want.  When you delete the array pointer in one class for example, the other class will be pointing to memory to which you have no idea as what it contains.

    Consider the following example class which is used to house an array of integers plus its size. It is written in such a way as to guarantee a crash:

    #include ,algorithm>
    
    class Array
    {
    private:
        int size;
        int* vals;  
    
    public:
        ~Array();
        Array( int s, int* v );
    };
    
    Array::~Array()
    {
       delete vals;
       vals = NULL;
    }
    
    Array::Array( int s, int* v )
    {
        size = s;
        vals = new int[ size ];
        std::copy( v, v + size, vals );
    }
    
    int main()
    {
       int vals[ 4 ] = { 1, 2, 3, 4 };
    
       Array a1( 4, vals );
       Array a2( a1 );
       return 0;
    }
    // Bam!
    

    Once the program goes out of scope (exits the main loop), the class destructor is called. Twice. Once for Array object a1 and then for a2. Remember that the compiler-generated default copy constructor simply makes a copy of the pointer vals.  It does not know that it also needs to allocate memory for a new vals.  When a1 is deleted, its destructor frees up vals.  Subsequent use of vals in the other instance a2 when once more trying to delete it in the destructor causes the program to fall over, since that instance of vals does not exist any more.

    Using explicitly defined copy constructors

    There is clearly a need to explicitly define a copy constructor that correctly copies vals.  Suppose someone has now defined an acceptable copy constructor, in addition to the default constructor:

    class Array
    {
    private:
       int size;
       int* vals;  
    
    public:
       ~Array();
       Array( int s, int* v );
       Array( const Array& a );
    };
    
    Array::Array( const Array &a )
    {
       size = a.size;
       vals = new int[ a.size ];
       std::copy( a.vals, a.vals + size, vals );
    }
    

    This improves the situation in that the code will not crash when exiting the main loop. But we’re not in the clear yet. It is still vulnerable to a crash as soon as someone has a go at assigning an instance of Array.

    Now try this:

    int main()
    {
       int vals[ 4 ] = { 1, 2, 3, 4 };
       Array a1( 4, vals );
       Array a2( a1 );
       a1 = a2;
       return 0;
    }
    // Bam!
    

    As before two instances of vals are allocated, and the two then get deleted just fine. So why the crash again?  As before, both instances of Array are pointing to the same instance of vals.  This is due to the default assignment operator that gets called, unless one is explicitly defined.  It only knows how to assign the pointer to vals.  Consequently you also need to explicitly define an assignment operator to complement the copy constructor. Full code listing as follows.

    The full picture: copy constructors and assignment operator

    #include <algorithm>
    
    class Array
    {
    private:
       int size;
       int* vals;
    
    public:
       ~Array();
       Array( int s, int* v );
       Array( const Array& a );
       Array& operator=( const Array& a );
    };
    
    Array::~Array()
    {
       delete vals;
       vals = NULL;
    }
    
    Array::Array( int s, int* v )
    {
       size = s;
       vals = new int[ size ];
       std::copy( v, v + size, vals );
    }
    
    Array::Array( const Array &a )
    {
       size = a.size;
       vals = new int[ a.size ];
       std::copy( a.vals, a.vals + size, vals );
    }
    
    Array& Array::operator =(const Array &a)
    {
       if ( &a != this )
       {
          size = a.size;
          vals = new int[ a.size ];
          std::copy( a.vals, a.vals + size, vals );
       }
       return *this;
    }
    
    int main()
    {
       int vals[ 4 ] = { 1, 2, 3, 4 };
       Array a1( 4, vals );
       Array a2( a1 );
       a1 = a2;
       return 0;
    }