Skip to content

Instantly share code, notes, and snippets.

@vishal-keshav
Last active January 26, 2019 15:52
Show Gist options
  • Save vishal-keshav/ce0fb86d31c1e766a6bb4e0ab3d5f89e to your computer and use it in GitHub Desktop.
Save vishal-keshav/ce0fb86d31c1e766a6bb4e0ab3d5f89e to your computer and use it in GitHub Desktop.
On Object oriented programming and design patterns in C++ part 11

Design Principles Part 11

Concept of single and multi-instance design

Single instance designs are design that can be used only once. Multi-instance designs are designs that can be used multiple times. In C, multi-instance design can be implemented by structs and passing the address everytime of the structure. However, C++ simplifies this process with class. At compiler level, even the C++ class codes gets converted to something that represents how multi-class instance design is implemented in C.

Below code shows how C implements multi-instance design and how it is done by C++. Also, the efficiency of C++ is equivalent to C, which can be easily demostrated with the compiler generated code.

In the below code, it has been shown that the templates on a class or a function does not c create any overhead, but instead they makes the code even faster by compiling only the relevant parts.

#include "stdafx.h"


using namespace std;


// Implement Stack data structure in C language

// Version - 1 - Single Instance design in C
/*int items[10];
int top = 0;

void push(int item)
{
	items[top] = item;
	top++;
}

int pop()
{
	top--;
	return items[top];
}*/

// Version - 2 - Multi Instance design in C

/*struct Stack
{
	int items[10];
	int top = 0;
};

void push(Stack *s, int item)
{
s->items[s->top] = item;
s->top++;
}

int pop(Stack *s)
{
s->top--;
return s->items[s->top];
}*/

// Version - 3 - Multi Instance design in C++

struct Stack
{
	int items[10];
	int top = 0;
	// gauranteed initialization
	Stack()
	{

	}
	// gauranteed de-initialization
	~Stack()
	{

	}

	//operator overaloding, which is not in C

	void push(int item) // void push(Stack *this, int item)
	{
		items[top] = item; // this->items[this->top] = item;
		top++; // this->top++;
	}

	int pop()
	{
		top--;
		return items[top];
	}
};


// Templated class are never classes, instead they are used to create new classes out of them
// And once class object is created, only those functions are compiled which are called.
template<class T>
class LinkedList
{
public:
	void add()
	{
		abdhdfff;
	}
};

// ---------------------------Client--------------------------
int main()
{
	// Multi-instance design will require address
	/*Stack s1, s2, s3;

	push(&s1, 100);
	push(&s1, 200);
	push(&s1, 300);*/
	Stack s1, s2, s3;

	s1.push(100); //push(&s1, 100);
	s1.push(200); //push(&s1, 100);
	s1.push(300); //push(&s1, 100);

	LinkedList<int> l1;
	//l1.add(5); // If this is uncommented, then only compilation error will happen.
	return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment