Search This Blog

Tuesday, February 19, 2013

Why a constructor cannot be declared as virtual?

A constructor cannot be virtual because at the time when the constructor is invoked the virtual table would not be available in the memory. Hence we cannot have a virtual constructor.

We can't override a constructor. When any method is declared as virtual it should be overridden in its derived class. Since constructor is used for initializing the variables and if declare a constructor as virtual it can’t be overridden.

Chat Client cheater Software

In my previous post Chat client cheater, I posted a sample code to send fake mouse movements and make your chat instant messenger to show you always in online.

Now I created it as a software,  You can download and use it directly for free.

for more details kindly visit,
http://www.smartpricedeal.com/products/chatclientcheater.html

Singleton Class in C++


Singleton pattern is a design pattern that restricts the instantiation of a class to one object.

Application:
  • This pattern can be used in "Database class" to that insert and retrieve record's in threads.
  • Can be used in Logger class.
How to create?
  • Define the constructor as private.
  • Declare copy constructor and Assignment operator as private to avoid creating copy of object
Sample 1( Allocating memory dynamically with new operator):

 
#include "stdafx.h"
#include <iostream>
#include <conio.h>
using namespace std;


class Singleton
{
private:
    static Singleton *m_ptrInstance;
    Singleton(){};
    Singleton(Singleton const&);             
    void operator=(Singleton const&);

public:
    static Singleton* GetInstance()
    {
        if(m_ptrInstance==NULL)
        {
            m_ptrInstance=new Singleton();
        }
        return m_ptrInstance;
    }
    void Display()
    {
        cout<<"This is singleton class"; 
    }
};
Singleton* Singleton::m_ptrInstance=NULL;


int _tmain(int argc, _TCHAR* argv[])
{
    //How to call    
    Singleton::GetInstance()->Display();
    getch();
    return 0;
}
 
Sample 2( Without allocating memory dynamically with new operator):

#include "stdafx.h"
#include <iostream>
#include <conio.h>
using namespace std;


class Singleton
{
private:
    Singleton(){}
    Singleton(Singleton const&);             
    void operator=(Singleton const&);

public:
    static Singleton& GetInstance()
    {
        static Singleton _instance;
        return _instance;
    }
    void Display()
    {
        cout<<"This is singleton class"; 
    }
};


int _tmain(int argc, _TCHAR* argv[])
{
    //How to call    
    Singleton::GetInstance().Display();
    getch();
    return 0;
}

Thursday, January 3, 2013

How to Intialize the Array elements value to NULL?

Usually we initialize the dynamically allocated array values to NULL using the memset.

We can do it while we allocate memory itself, by adding parenthesis ()  after the size.

Sample Code snippet:
 
int* nIntializedArray=new int[3]();

Example:


#include "stdafx.h"
#include <iostream>
#include <string>
#include <conio.h>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
    int i=0;
    cout<<"Uninitialazied Arrary of int* \r\n\r\n";

    int* nUnIntializedArrayptr=new int[3];
    for( i=0; i<3; i++)
    {
        cout<<nUnIntializedArrayptr[i]<<"\r\n";
        
    }
    delete[] nUnIntializedArrayptr;
    cout<<"\r\n\r\n";


    cout<<"Initialazied Arrary of int* \r\n\r\n";
    int* nIntializedArray=new int[3]();
    for( i=0; i<3; i++)
    {
        cout<<nIntializedArray[i]<<"\r\n";
        
    }
    delete[] nIntializedArray;


    cout<<"UnInitialazied char* \r\n\r\n";
    char* sztxt=new char[3];
    cout<<sztxt<<"\r\n";    
    delete[] sztxt;

    cout<<"Initialazied char* \r\n\r\n";
    char* szIntialTxt=new char[3]();
    cout<<szIntialTxt<<"\r\n";    
    delete[] szIntialTxt;


    getch();
    return 0;
}
 
 

How to find the size of a dynamically allocated array?

Usually when we pass a pointer to an array to another function, we pass its size also as the parameter.
Which is not required, if the array is dynamically allocated.

We can find the size of a dynamically allocated array using _msize() function.


    TCHAR *szTxt=new TCHAR[256];
    int nArraySize=0;
    nArraySize=    _msize(szTxt)/sizeof(TCHAR);


Example:
#include "stdafx.h"
#include <iostream>
#include <conio.h>
using namespace std;

void GetIntPtrArray(int* ptrInt)
{
    //To find size of Array
    int nSize=_msize(ptrInt)/sizeof(int);
    cout<<"\r\nThe size of the Integer Array is "<<nSize;
}

void GetCharPtrArray(char* ptrchr)
{
    //To find size of Array
    int nSize=_msize(ptrchr)/sizeof(char);
    cout<<"\r\nThe size of the Char Array is "<<nSize;
}
int _tmain(int argc, _TCHAR* argv[])
{
    int *nArr=new int[10];
    GetIntPtrArray(nArr);

    char *szTxt=new char[256];
    GetCharPtrArray(szTxt);

    getch();
    return 0;
}
 
 

How to update the icon changes when installing?

BMP files can be opened using MS Paint or Windows photo viewer, or with some other programs.
But Icon of that file, is the default program that associated with that extension. you can also change the default program to open files.

Even you can create your own file extension documents for your application. To update the icon of the files and to refresh the windows after default program changed or your application installation can be done using the following code.


SHChangeNotify(SHCNE_ASSOCCHANGED,0,NULL,NULL);

How to check whether internet connection is available?

Some times we need to check the internet connection before doing updates or online license check.

How to find  it? Use the following code.



#include "intshcut.h"
#pragma comment(lib,"url.lib")

if( !InetIsOffline( 0 ))
{
    // Internet Connection is available  
}