Tuesday, June 3, 2014

[C++]Characters Array

char **aPara;
    aPara = new char *[size];
    for(int i = 0; i < size; i++)
    {   
        aPara[i] = new char[40];
       aPara[i] = "Test";
        TRACE("Para %d : %s\n", i, aPara[i]);
    }

Tuesday, April 29, 2014

[C++] error lnk2019 unresolved external symbol

These kind of errors getting because of not implementing header defined functions. Therefore make sure you have implemented all functions defined in header.

Friday, April 25, 2014

[C++]Pure Virtual Functions

It's possible that you'd want to include a virtual function in a base class so that it may be redefined in a derived class to suit the objects of that class, but that there is no meaningful definition you could give for the function in the base class.

class Shape {
   protected:
      int width, height;
   public:
      Shape( int a=0, int b=0)
      {
         width = a;
         height = b;
      }
      // pure virtual function
      virtual int area() = 0;
};

The = 0 tells the compiler that the function has no body and above virtual function will be called pure virtual function.

[C++]Virtual Function

A virtual function is a function in a base class that is declared using the keyword virtual. Defining in a base class a virtual function, with another version in a derived class, signals to the compiler that we don't want static linkage for this function.

What we do want is the selection of the function to be called at any given point in the program to be based on the kind of object for which it is called. This sort of operation is referred to as dynamic linkage, or late binding.

[C++]Static members of a class

We can define class members static using static keyword. When we declare a member of a class as static it means no matter how many objects of the class are created, there is only one copy of the static member.
  • Static data is initialized to zero when the first object is created, if no other initialization is present.
  • Can't put it in the class definition but it can be initialized outside the class.
  • Use the scope resolution operator :: to identify which class it belongs to.
Example
#include 
 
using namespace std;

class Box
{
   public:
      static int objectCount;
      // Constructor definition
      Box(double l=2.0, double b=2.0, double h=2.0)
      {
         cout <<"Constructor called." << endl;
         length = l;
         breadth = b;
         height = h;
         // Increase every time object is created
         objectCount++;
      }
      double Volume()
      {
         return length * breadth * height;
      }
   private:
      double length;     // Length of a box
      double breadth;    // Breadth of a box
      double height;     // Height of a box
};

// Initialize static member of class Box
int Box::objectCount = 0;

int main(void)
{
   Box Box1(3.3, 1.2, 1.5);    // Declare box1
   Box Box2(8.5, 6.0, 2.0);    // Declare box2

   // Print total number of objects.
   cout << "Total objects: " << Box::objectCount << endl;

   return 0;
}

Output:
Constructor called.
Constructor called.
Total objects: 2

Thursday, April 3, 2014

[JAVA]Regular Expression

Splitting using regex (regular expression)
 String sData = "-rw-rw-r--+ 1 aime1 svrtech 83338 Apr 2 10:26 sat.log -rw-rw-r--+ 1 aime1 svrtech 2435 Apr 2 10:48 MAT.log -rw-rw-r--+ 1 aime1 svrtech 3470 Apr 2 08:35 ant_build.log ";

 String arrData[] = sData.split("(?<=.log )");

 for(String s : arrData){
        System.out.println(s);
 }
 Result:
-rw-rw-r--+ 1 aime1 svrtech 83338 Apr 2 10:26 sat.log 

-rw-rw-r--+ 1 aime1 svrtech 2435 Apr 2 10:48 MAT.log 

-rw-rw-r--+ 1 aime1 svrtech 3470 Apr 2 08:35 ant_build.log 
Further Reading: http://www.tutorialspoint.com/java/java_regular_expressions.htm

Window Messages

The operating system communicates with your application window by passing messages to it. A message is simply a numeric code that designates a particular event. For example, if the user presses the left mouse button, the window receives a message with the following message code.
#define WM_LBUTTONDOWN    0x0201
Some messages have data associated with them. For example, the WM_LBUTTONDOWN message includes the x-coordinate and y-coordinate of the mouse cursor. 
To pass a message to a window, the operating system calls the window procedure registered for that window. 

Wednesday, April 2, 2014

[C++]Qualifiers

const

Objects of type const cannot be changed by your program during execution

volatile

The modifier volatile tells the compiler that a variable's value may be changed in ways not explicitly specified by the program.

restrict

A pointer qualified by restrict is initially the only means by which the object it points to can be accessed. Only C99 adds a new type qualifier called restrict.

[C++]Constants

Defining Constants:

There are two simple ways in C++ to define constants:
  • Using #define preprocessor.
  • Using const keyword.
     

#define

#define identifier value
Following example explains it in detail:
#include <iostream>
using namespace std;

#define LENGTH 10   
#define WIDTH  5
#define NEWLINE '\n'

int main()
{

   int area;  
   
   area = LENGTH * WIDTH;
   cout << area;
   cout << NEWLINE;
   return 0;
}
Result:
50

const 

const type variable = value;
Following example explains it in detail:
#include <iostream>
using namespace std;

int main()
{
   const int  LENGTH = 10;
   const int  WIDTH  = 5;
   const char NEWLINE = '\n';
   int area;  
   
   area = LENGTH * WIDTH;
   cout << area;
   cout << NEWLINE;
   return 0;
}
Result:
50
Note that it is a good programming practice to define constants in CAPITALS.

 


 

[C++]Literals

Integer literals:

An integer literal can be a decimal, octal, or hexadecimal constant. A prefix specifies the base or radix: 0x or 0X for hexadecimal, 0 for octal, and nothing for decimal.

An integer literal can also have a suffix that is a combination of U and L, for unsigned and long, respectively. The suffix can be uppercase or lowercase and can be in any order.

Here are some examples of integer literals:
212         // Legal
215u        // Legal
0xFeeL      // Legal
078         // Illegal: 8 is not an octal digit
032UU       // Illegal: cannot repeat a suffix
Following are other examples of various types of Integer literals:
85         // decimal
0213       // octal
0x4b       // hexadecimal
30         // int
30u        // unsigned int
30l        // long
30ul       // unsigned long

Floating-point literals:

A floating-point literal has an integer part, a decimal point, a fractional part, and an exponent part. You can represent floating point literals either in decimal form or exponential form.

While representing using decimal form, you must include the decimal point, the exponent, or both and while representing using exponential form, you must include the integer part, the fractional part, or both. The signed exponent is introduced by e or E.

Here are some examples of floating-point literals:
3.14159       // Legal
314159E-5L    // Legal
510E          // Illegal: incomplete exponent
210f          // Illegal: no decimal or exponent
.e55          // Illegal: missing integer or fraction


 

[C++]typedef

You can create a new name for an existing type using typedef. Following is the simple syntax to define a new type using typedef:
typedef type newname; 
For example, the following tells the compiler that feet is another name for int:
typedef int feet;
Now, the following declaration is perfectly legal and creates an integer variable called distance:
feet distance;

[C++]extern

You will use extern keyword to declare a variable at any place. Though you can declare a variable multiple times in your C++ program, but it can be defined only once in a file, a function or a block of code.

#include <iostream>
using namespace std;

// Variable declaration:
extern int a, b;
extern int c;
extern float f;
  
int main ()
{
  // Variable definition:
  int a, b;
  int c;
  float f;
 
  // actual initialization
  a = 10;
  b = 20;
  c = a + b;
 
  cout << c << endl ;

  f = 70.0/3.0;
  cout << f << endl ;
 
  return 0;
}