Tuesday, May 4, 2010

From C to C++ - misc...

Miscellaneous points:

Function declaration:
In C++ function must be declared before it is used. Function declaration/prototype should provide return type, number and type of arguments it take.

Example:
void display(char *);
void display(char *name); // better

Old C Structure: C++ has construct called structure which same as class except that in structure default access is public while in classes default access is private

Union and Enum: C++ has constructs called union and enum.
Example:


enum Gender { MALE, FEMALE };

union Data {
char name[20];
int age;
Gender sex;
};

Data d;
Gender sex;

Union is similar to structure except that it uses same memory location for all three items. i.e. size of the union is 20 (max of 3 items) while size of the same struct would be 28.

In C++ there is also a concept of anonymous union and enums

union {
char name[20];
int age;
};

enum { MALE, FEMALE };


name[0] = 'A';
age = 20;
int sex = MALE;

void *: We can assign any pointer to void* but cannot assign void* to another pointer, explicit conversion (type casting) is needed in 2nd operation.

Example:
void *vpointer;
chr *name;

name = (char *) vpointer; // conversion is needed
vpointer = name; // conversion not needed

bool: C++ has datatype similar to boolean in Java except that any integer can be converted to bool and vice-versa. integer != 0 is true while integer value 0 is interpreted as false

Example:
int num = 10;
bool isnum = true;

isnum = num;
cout <<>
num = isnum;
cout <<>

typedef: Typedef provides alias for an existing type. It doesn't create new type of its own.

typedef MYTYPE char*;
void display(char *);
void display(MYTYPE); // same as above hence throws an error

inline function: There are two kinds of inline functions.
  • Function explicitly declared as inline
Example:
inline void display() { // It's just a request to the compiler. Compiler can choose to ignore the request.
cout << "hello";
}

  • Function defined in the class body. Its more of C++ lingo than actual inline function as described above.

class sample {
public:
void display() {
cout << "hello"; // In lingo it is called defined inline.
}
};

Casting: Other than implicit data conversion C++ supports 4 different kind of type casting.
  • dynamic_cast: Checks at runtime if object is of given type. it returns address if object is of given type else returns 0
Sample *sample;
Base *base;
if (sample = dynamic_cast
(base)) {
...
}
  • static_cast: Used to convert between compatible types.
    • castless conversion i.e. int to long etc.
int rate = 10;
long base = static_cast(rate);
    • conversion from void*
Base* base = static_cast<>(vptr);

    • narrowing conversion i.e. from float/long to int
long rate = 10.6;
int base = static_cast(rate);

    • from derived to base and vice versa.
Base* base = static_cast (&derived);
  • const_cast: Used to convert const to non-const i.e.
const int a = 10;
int *p = const_cast(&a);

  • reinterpret_cast: Least safe of them all. Used to convert pointer to integers and vice versa.
int num = 65000;
int *address = reinterpret_cast(num);


RTTI (RunTime Type Identification): Other than dynamic_cast C++ provides another way to know the type of the object in runtime.
Example:
int num = 10;
typeid(num).name();

No comments:

Post a Comment