Monday, July 26, 2010

Conversion of Basic Data Type to string in C++ \ VC++

Current post is on how to convert basic data type into string in C++ \ VC++


Have you ever tried of converting basic data types to string in most simple and elegant way!!!

Till now, I used to call 'itoa()' or similar function after which I used to append the char* buffer to string variable.

Lots of temp variable to initalize, worry about charbuff limited size declaration and ugly looking code. I came across this simple and clean function calls which really pleased my eyes :)

Let us take int to string conversion example:

BEFORE:

int myint=0;
char mycharbuf[64]="";
string mystr;

... // Get the int value

itoa(mycharbuff,myint,10);

mystr.append(mycharbuff);

... // Use the string

NOW:

int myint;
string mystr;
stringstream mystream;

... //Get the int value

mystream << myint;

mystr = mystream.str();

... // Use the string

Here you go, with clean code & faster code review of your work.


Appreciate your feedback via comments. Thanks.

Thursday, February 25, 2010

Non-Printable Char to int Conversion Issue in C

Current post is on non-printable character conversion to int issue in C language.


By the topic you will assume, what is this? Even a C student who has just started will know that, there is no harm, as we are upgrading the type from character (char) to integer (int).

However, many people including the advanced programmer do not know is that: “conversion of character having non-printable value to int is machine dependent”.

It means that if you take a variable:

char mynonprintablechar = 129;

And then you try to upgrade to int implicitly, the results which you get, will make you scratch your hair.

Example 1: If - Else Statement


if( mynonprintablechar == 129)
     printf(“You expected me”);
else
     printf(“You may get me”);

Example 2: Switch Statement


switch(mynonprintablechar)
{
    case 129:
        printf(“You expected me”);
        break;
    default:
        printf(“You may get me”);
}

The solution:


1. Have unsigned char as variable type.
2. downcast the int against which you compare to char.
3. cast as unsigned char.

Do drop me a mail, if I saved you from scratching your hair :)

Tuesday, February 9, 2010

What do you mean by 'Pure Virtual Function' in C++ \ VC++?

Current post is on what does 'Pure Virtual Function' mean?.


Lets examine:

What is pure virtual function ?


A pure virtual function is a virtual function which will force derived classes to override. If a class has any  pure virtual funtion(s), the class is called an "abstract class" and you can't create objects of that class.

Declaration Syntax:

return_value classname:: Func_Name( parameters ) = 0;


Example:

class AbstractBaseClass
{
     void ThisIsAPureVirtualFunction(void) = 0;
};


Why it is required?

- To specify an interface that must be provided by all classes deriving from it
- To make a class an abstract base class so that it cannot be instantiated


Can they have definition (method body)?

- Yes, they may have it.

1) If a class needs to be abstract & has no suitable candidate for virtual function, then my making destructor pure virtual & providing the definition, class is made virtual and derived class will have no issue in calling delete function

2) If a class need to provide a default behaviour but not let derived classes just inherit it "silently".


Appreciate your feedback via comments. Thanks.
Disclaimer:

The above post and all the posts in the blog are derived from facts, information, logical interpretation and logical conclusion of printed and internet materials available to me, perceived and produced by 99 gm brain of mine, which by no means always be accurate, consistent and complete.

All the posts are for personal quick reference only.

If any suggestion, correction, misinterpretation, misconception commented, which will be moderated and deleted if required, to avoid unforeseen issues.

If any trademark / copywrite issue is present, do send in a mail and appropriate tag (logo, name, website link) will be attached to the same.

Additional disclaimer will be attached wherever required in the post.