Namespaces address the problem of naming conflicts between different pieces of code. For example,you might be writing some code that has a function called fun(). One day, you decide to start usinga third-party library, which also has a fun() function. The compiler has no way of knowing whichversion of fun() you are referring to within… Continue reading
Author: Avidlearner
Calling C Code from C++ – Mix C and C++
If you have a project which is written in C/C++ where you are seeing the parts of code has been implemented in Cand some part in C++.Then you might have observed that user might have the need to call C function from C++ code and vice-versa. Let’s see how we can do this. Requirement: Compilers… Continue reading
Abstract Factory Pattern C++ Simplified
THE ABSTRACT FACTORY PATTERN A factory in real life constructs tangible objects, such as Furniture. Similarly, a factory in objectoriented programming constructs objects. When you use factories in your program, portions ofcode that want to create a particular object ask the factory for an instance of the object instead ofcalling the object constructor themselves. Abstract… Continue reading
Difference between new and malloc
malloc() returns a void pointer it is necessary to explicitly typecast it into an appropriate type of pointer. This gets completely avoided when using new operator. E.g int *ptr1 = (int *)malloc(sizeof(int)); float *ptr2 = (float *)malloc(sizeof(float)); ExampleClass *ptr3 = (ExampleClass*)malloc(sizeof(sample)); Here ExampleClass is a user defined class. int *ptr1 = new int; int *ptr2… Continue reading
Bind and placeholders in C++
Bind() introduced in c++11 allows you to bind parameters of a callable in a flexible way. Youcan bind parameters to fixed values, and you can even rearrange parameters in a different order. bind(function pointer, bound arguments) For example, let add3 be a function that takes 3 integer arguments and returns an int that is the… Continue reading
Program to find best possible denomination of input money
In this program, we will be finding possible denomination of money input from user. In this case we are assuming denomination as 1,5,10,25. For example if input money is 65 then denomination will be 25*2,10*1,5*1. Since we are trying to get best possible denomination such that we have least coins, so will store the denomination… Continue reading