Showing posts with label C++. Show all posts
Showing posts with label C++. Show all posts

Function Overloading Vs Function Overriding in C++

Function Overloading means having multiple definitions of the function by changing its signature.

float area(int a);
float area(int a, int b); 

Function Overriding means redefining a base class function in its derived class with same signature i.e return type and parameters.

Linked List in C++

Linked list is a type linear data structure, in which  the elements are not stored at contiguous memory locations like array. Each element is linked using pointers as shown in the image:

Shallow Copy Constructor Vs Deep Copy Constructor

Copy Constructor 
It is a type of constructor which is used to create a copy of an already existing object. A copy constructor is a member function which initialises an object using another object of the same class.To implement this in coding we define a member function class which initialises an object using another object of the same class. The function prototype for copy constructor is as below:
ClassName (const ClassName &existing_obj);

OpenMP (Open Multi-Processing)

What is OpenMP ??
Now days we  mostly  use computers with more than one processor and CPU having multiple cores. Also with growing technology, as demand of processing power is increasing day by day, the cores in CPU's are also increasing. This is where OpenMP comes into role. OpenMP is an easy way to convert a program which uses one core into a program which is able to use multiple core for performance enhancement. OpenMP is designed only for shared memory systems, its meaning is that, it is designed for applications that run on single computer only. If you want to write a high performance application that will run on a cluster of computers, you should go with MPI. 
As per wikis definition OpenMP is an API that supports multi-platform shared memory multiprocessing programming in C, C++, and Fortran.

Introduction to Standard Template Library (STL)

The standard template library (STL) is a set of template classes and functions that supply the programmer with
  • Containers for storing information 
  • Iterators for accessing the information stored 
  • Algorithms for manipulating the content of the containers

STL Containers
Containers are STL classes that are used to store data. STL supplies two types of container classes:
  • Sequential containers
  • Associative container

Lambda Expressions or Lambda Functions in C++11

The most asked-for feature added in C++ in recent years is lambda expressions, which enable us to create the inline anonymous functions.The syntax can seem a bit confusing at first glance, but as you master their use, you will find they provide powerful functionality that can make your source code easier to understand.

A lambda expression resembles a statement and can be defined in a single line of code. The expression is stored in a variable and called like a function.

Call Back Functions

Pointers to Functions
Just as an array name is a constant pointer to the first element of the array, a function name is a constant pointer to the function. It is possible to declare a pointer variable that points to a function, and to invoke the function by using that pointer. It is very useful and it allows us to create programs that decide which functions to invoke based on user input. The only tricky part about function pointers is understanding the type of the object being pointed to. Just like a pointer to int points to an integer variable, a pointer to a function must point to a function of the appropriate return type and signature.

Static Members in C++

When we declare a member of a class as static it means irrespective how many objects of the class are created, there is only one copy of the static member.We can define class members static using static keyword. 
By declaring a function member as static, you make it independent of any particular object of the class. We can call a static member function even if no objects of the class exist and the static functions are accessed using only the class name and the scope resolution operator ::.

C++ Library Development Made Easy on Linux

In this post, I’ll guide you through the process of creating your own static C++ library on a Linux platform and linking it to your project. As a hands-on example, we’ll implement a simple function that adds two integers, then package it as a reusable library. The tutorial will begin with writing the source and header files, and walk you step-by-step through compiling, archiving, and integrating your custom library into a C++ project. We will write first code of source file and header file. 

Can we delete this pointer in a member function of class in C++ ?

 

Yes, we can delete “this” pointer inside a member function only if object has been created dynamically i.e. using “new” keyword. Because delete can be applied on the object that has been created using “new” keyword only. If member function  called using statically allocated object then a runtime crash will happen.


#include<iostream>
using namespace std;

//class that delete this pointer inside a member function.
class Test{

public:
  void function()
 { 
    delete this;
    cout<<"Deleted Object \n";
 } 
};

//Test function
int main()
{

    Test *pointer = new Test(); 
    pointer->function();// Ok

    Test object;
    object.function();// Run time crashing

    return 0;
}

What exactly delete Keyword do in C++


Just think of memory as a storeroom with lots of boxes to put things into. When you call "new", the storekeeper assigns you an unused box with sufficient size to put your stuffs into it, and keep record of  box no in his diary. This number would be the "pointer". Now, when you "delete" that pointer, the reverse happens: the storekeeper notes that this particular box is available again. As storekeeper are not doing anything with the box, so if you look into it after a "delete" you might see your old stuff. Or you might see somebody else’s stuff, if the box was reassigned in the meanwhile.


#include <iostream>

int main()
{
    int* p = new int;
    *p= 100;
    std::cout << " before delete *p =" << *p <<std::endl;
    delete (p);
    std::cout << " after delete *p =" << *p <<std::endl;
    return 0;
}

Output:

before delete *p =100
after delete *p =0


...

lvalue vs rvalue in c++11

What is lvalue ?
lvalue is anything whose address is accessible. It means we can take address of lvalue using & operator.
int x = 1;
In above expression,  address of x can be accessed using expression below, therefore x is lvalue.
int * ptr = &x;
Now consider this example 
int y = x + 1;
In above expression y  is lvalue but  x + 1 is not lavlue. We cant access address of x + 1
int * ptr3 = &(x+1); // Compile Error

Why we need static member function in C++

Static member functions are used to maintain a single copy of a class member function across different objects of the class. Static member functions can be called either by itself, without instantiating any object, by using class name and scope resolution operator (::) or in connection with an object. - 

Restrictions on static member functions are : 

1. They can directly refer to other static members of the class. 

2. Static member functions do not have this pointer. 3. 

Static member function can not be virtual. 

My Kinect 3D Background and Foreground Subtraction Demo

Background subtraction is a classic computer vision technique used to separate the foreground (the subject of interest) from the backg...