Tag: C# / .NET / 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…)

  • 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…)

  • Avoiding Memory Leaks using Boost Libraries

    Using boost::scoped_array

    When we want to dynamically allocate an array of objects for some purpose, the C++ programming language offers us the new and delete operators that are intended to replace the traditional malloc() and free() subroutines that are part of the standard library : (more…)

  • Using Template Classes to Handle Generic Data Types in STL Containers

    A code snippet with which utilizes both template classes and STL to handle generic data types. In keeping with the spirit of this blog, I have kept the explanation to a minimum and hopefully the code posted below should be self-explanatory.  The idea is for readers to get the gist of what I am saying so that they can go off and make up more relevant examples of their own. (more…)

  • Some neat examples of C++ String Formatting

    Some neat string formatting

    A pretty neat snippet found on stackoverflow some time ago (kudos David Rodriguez), that is worth repeating. Some time ago, a ‘moderator’ at StackOverflow removed this thread “for reasons of moderation”. Fair enough, but the result is that code that many would find pretty darn useful is no longer searchable on that site. How very constructive. Here is a snapshot of that particular StackOverflow page as preserved by the WayBack Machine:

    http://web.archive.org/web/20090420233428/http://stackoverflow.com/questions/469696/what-is-your-most-useful-c-c-snippet/470999

    I’m gonna start using this. Essentially it is a bit of in-house stream formatting. This class make_string produces an instance of an object derived from ostream, which has an implicit conversion to a std::string: (more…)

  • Multidimensional Arrays in C++

    Declaring and initializing multi-dimensional arrays in C++ can be done not just by way of traditional pointer arithmetic, but using the STL / Boost libraries as well.  Here are some examples:

    (more…)