Tuesday, May 29, 2012

Access member variable from non member function in C++

Friend non member function can access private member variable also. following example help you to understand how to use friend keyword and how to access member private variables from non member function.

#include <stdio.h>

class Woman{
//making it friend
friend void loveWoman();

private:
 int age;
 int salary;

public:
 char name[50];
 void walk();
};

//friend function
void loveWoman(){
 Woman myGirlFriend;

 // access private member variables
 printf("My girlfriends age:%i \n",myGirlFriend.age);

 // access public member variables
 printf("My girlfriends age:%i \n",myGirlFriend.name);
}

//not a friend function
void hateWoman(){
 Woman myhusbandXWife;

 // only can access public member variables
 printf("My girlfriends age:%i \n",myhusbandXWife.name);
}

void main(){
 //call friend non member function
 loveWoman();

 //call non member function
 hateWoman();
}




Sunday, May 27, 2012

SQL Server Tips

1. How to get list of stored procedures in a database

SELECT NAME FROM SYS.ALL_OBJECTS WHERE type= 'P'


2. How to get list of tables in a database

SELECT * from sysobjects where type = 'U'


3. How to get list of databases in a SQL server

SELECT name, collation_name
FROM sys.databases
WHERE name NOT IN ('master', 'tempdb', 'model', 'msdb')

Wednesday, May 23, 2012

How to convert integer number to a binary string in C++.

Converting integer to binary string is a very simple matter. for this there is a special class call "bitset".
so fallow the example bellow and try to play around "bitset"

For more information visit http://www.cplusplus.com/reference/stl/bitset/

#include <iostream>
#include <bitset>

void main(){

  //creating instance using bitset (16 bit). here you can specify the length suc as  8,16,32,64...
  std::bitset< 16 > btFlaged;

  //assigning integer values to instance
  btFlaged = 1234;

  //print bit string in the string
  for(int i =0; i< btFlaged.size(); i++){
    std::cout <<btFlaged.test(i) << std::endl;
  }
}



Sunday, February 12, 2012

Most of the times developers are believe that sprintf is the most performance method for string copy. But I did some small test and result as fallow.

Environment : windows 7
IDE : visual studio 2010
Programing language: Visual C++

Method: Get the current system time. Copied the 100 length strings to specific char buffer 100,000,000 times continually. Get the current system time and check the time different.


Result: following table showing the result time for each function . So as conclusion strcpy is the most fastest  string copy function.  


Test
sprintf
strcpy
strncpy
strcpy_s
Test - 1
0 : 26 : 016
0 : 0 : 261
0 : 0 : 271
0 : 2 : 563
Test - 2
0 : 25 : 874
0 : 0 : 260
0 : 0 : 270
0 : 2 : 529
Test - 3
0 : 25 : 909
0 : 0 : 260
0 : 0 : 270
0 : 2 : 532
Test - 4
0 : 25 : 905
0 : 0 : 260
0 : 0 : 270
0 : 2 : 548
Test - 5
0 : 26 : 017
0 : 0 : 260
0 : 0 : 269
0 : 2 : 531
Test - 6
0 : 25 : 917
0 : 0 : 260
0 : 0 : 270
0 : 2 : 547
Test - 7
0 : 25 : 930
0 : 0 : 260
0 : 0 : 271
0 : 2 : 533
Test - 8
0 : 25 : 914
0 : 0 : 261
0 : 0 : 269
0 : 2 : 535
Test - 9
0 : 25 : 879
0 : 0 : 261
0 : 0 : 272
0 : 2 : 533
Test - 10
0 : 25 : 898
0 : 0 : 260
0 : 0 : 271
0 : 2 : 524


#include <windows.h>
#include <stdio.h>
#include <time.h>
void getDef(SYSTEMTIME t1, SYSTEMTIME t2, char name[]){

 SYSTEMTIME temp;

 if(t1.wMilliseconds < t2.wMilliseconds)
 {
  temp.wMilliseconds = t2.wMilliseconds - t1.wMilliseconds;
 }else if(t1.wMilliseconds > t2.wMilliseconds)
 {
  temp.wMilliseconds = t2.wMilliseconds - t1.wMilliseconds + 1000 ;
  if(t2.wSecond == 0)
  {
   t2.wMinute = t2.wMinute - 1;
   t2.wSecond = 59;
  }else
  {  
   t2.wSecond = t2.wSecond - 1;
  }    }else
  {
   temp.wMilliseconds = 0;
  }    if(t1.wSecond < t2.wSecond)
  {
   temp.wSecond = t2.wSecond - t1.wSecond;
  }else if(t1.wSecond > t2.wSecond)
  {  
   temp.wSecond = t2.wSecond - t1.wSecond + 60;
   t2.wMinute = t2.wMinute - 1;
  }else{    
   temp.wSecond = 0;
  }    if(t1.wMinute < t2.wMinute)
  {
   temp.wMinute = t2.wMinute - t1.wMinute;
  }    else if(t1.wMinute == t2.wMinute)
  {    
   temp.wMinute = 0;
  }    printf("Def %s %d : %d : %d\n",name,        temp.wMinute,temp.wSecond,temp.wMilliseconds);

}
void main(){

 char buffer [105];
 SYSTEMTIME st,st2;

 for (int j = 1; j < 11; j++)
 {
  GetLocalTime(&st);
  for(int i=0; i< 10000000; i++)
  {
   sprintf (buffer, "1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890.");
  }      
  GetLocalTime(&st2);
  getDef(st,st2, "sprintf");

  GetLocalTime(&st);
  for(int i=0; i< 10000000; i++)
  {
   strcpy  (buffer, "1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890.");
  }  
  GetLocalTime(&st2);
  getDef(st,st2, "strcpy");

  GetLocalTime(&st);
  for(int i=0; i< 10000000; i++)
  {
   strncpy  (buffer, "1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890.",100);
  }
  GetLocalTime(&st2);
  getDef(st,st2,"strncpy");


  GetLocalTime(&st);
  for(int i=0; i< 10000000; i++)
  {
   strcpy_s (buffer, "1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890.");
  }
  GetLocalTime(&st2);
  getDef(st,st2,"strcpy_s");
 }

 scanf ("%s",buffer);
}