Loading....
Coupon Accepted Successfully!

 

New Type-Casting Operators

We have studied that a type-casting operation is used to change a value from one type into another. This can be applied at a place where the automatic conversion of data types is not done.

Type casting

double b = (double)j // c type-casting style

double a = double (d) // c++ type-casting style

The above statements are correct, and nothing wrong happens when they are applied in practical applications. New type-casting operators introduced by the ANSI/ISO committee are as follows:

1.     static_cast

2.     dynamic_cast

3.     reinterpret_cast

4.     constant_cast

The static_cast operator

The static_cast operator is used for the conversion of standard data types. Using this operator, base class pointers can be converted into derived class pointers. Their syntax is as follows:

static_cast <data-type> object

The data_type indicates the target data type of the cast. The specified object is converted into a new data type. The new syntax of type casting is easy. Hence, the above keyword is frequently used by the programmer instead of the old format.

 

21.5 Write a program perform type casting using static_cast operator.

#include<iostream.h>

int main()

{

int k=65;

cout<<“\n size of k=”<<sizeof(k);

cout<<“\n value of k=”<<k;

double d=static_cast<double>(k);

cout<<“\n size of d=”<<sizeof(d);

cout<<“\n value of d=”<<d;

char c=static_cast<char>(k);

cout<<“\n c=”<<c;

return 0;

}

 

OUTPUT

size of k=4

value of k=65

size of d=8

value of d=65

c = A

 

 

Explanation: In the above program, the integer k is initialized with 65. The variable d is of a double data type. The value of k is assigned to d using the static_cast operator. The value of k is assigned to char data-type variable c. The output displays the contents of the variable with the numbers of bytes occupied by them.

The const_cast operator

The const_cast operator explicitly modifies the const or volatile of a variable. It is used in the following format:

const_cast <data-type> (object)

In this type of casting, our goal is to change the const or volatile nature of the variable. Hence, target and source data types are identical. This type of casting is frequently applied for removing the const attribute of a variable.

21.6 Write a program to convert constant to non-constant.

#include<iostream.h>

void main()

{

const int x=0;

int *p=(int *)&x;

p=const_cast<int*>(&x);

}

class data

{private:

int d;

public:

void joy() const

{

(const_cast<data*>(this))->d=100;

}

};

 

Explanation: In the above program, the address of the constant variable is assigned to the pointer of the non-constant variable, and this is done using the operator const_cast.

The reinterpret_cast operator

The reinterpret_cast operator is useful when the programmer wants to transform one type into a dissimilar type. In practical applications, it is used to modify a pointer kind of an object to an integer kind or vice versa. It is used in the following format:

reinterpret_cast <data-type> (object )

21.7 Write a program to convert pointers to integers using reinterpret_cast operator.

#include<iostream.h>

void main()

{

int b=487;

int *rp=reinterpret_cast<int*>(b);

cout<<endl<<“rp=”<<rp;

rp++;

cout<<endl<<“rp=”<<rp;

b=reinterpret_cast<int>(rp);

cout<<endl<<“b=”<<b;

b++;

cout<<endl<<“b=”<<b;

}

 

OUTPUT

rp=0x000001E7

rp=0x000001EB

b=491

b=492

 

Explanation: In the above program, b is an integer type of variable and initialized with 487. The variable rp is a pointer and initialized with the address of the variable. The rp is increased and the value is increased by four. The output shows the values of the variable.

21.8 Write a program to convert void pointer to char pointer using reinterpret_cast ­operator.

#include<iostream.h>

#include<string.h>

void *getadd()

{

static char text[50];

return text;

}

void main()

{

char *p=reinterpret_cast<char*>(getadd());

//char *p=getadd();

strcpy (p,” Well Come”);

cout<<p;

}

 

OUTPUT

Well Come

 

Explanation: In the above program, the function getadd() returns the base address of the array text [50] as a void address. The return type is void *. In the function main(), the obtained address is converted into character type using the operator reinterpret_cast and assigned to the character pointer p. The strcpy() function copies a string to the pointer p. Finally, the cout statement displays the text stored in the pointer p.

The dynamic_cast operator

The dynamic_cast operator is used to change the type of an object during program execution. It is frequently used to type cast on polymorphic objects, that is, when the base class has a virtual function. This operator casts the base class pointer onto the derived class pointer. The operation with dynamic_cast is also known as type-secure downcast. It is used in the following format:

dynamic_cast <data-type> (object)

The object should be a base class object. Its type is tested and altered. This always does valid conversion. It verifies that the casting is allowable at execution time. It returns null, in case type casting is defective.

RTTI using typeid operator

The runtime type information (RTTI) is a new improvement made by the ANSI/ISO committee. The typeid() operator is used to obtain the exact type of object or ­variable during program execution. This operator returns a reference to an object maintained by the system. This object identifies the type of argument. Its syntax is as follows:

char *object class = typeid (object). name();

21.9 Write a program using typeid() to identify the type of object.

#include<iostream.h>

#include<typeinfo.h>

class one

{

public:

virtual void say()

{

}

};

class two: public one

{};

class three: public one

{};

void main()

{

one *o;

cout<<endl<<typeid(o).name();

two *t;

cout<<endl<<typeid(t).name();

}

 

OUTPUT

class one *

class two *

 

 

Explanation: In the above program, class one has one virtual function, say()Class two is derived from class one. In the function main(), *o and *t are pointer objects of class one and two, respectively. The typeid() operator identifies the type of objects and displays the class name from which they are created.

 

21.10 Write a program to identify the type of variable using typeid().

#include<iostream.h>

#include<typeinfo.h>

void main()

{

int i;

cout<<endl<<typeid(i).name();

float f;

cout<<endl<<typeid(f).name();

}

 

OUTPUT

int

float

 

 

Explanation: In the above program, two variables of int (i) and float (f) are declared. Using the typeid() function, their types are displayed. The output is intfloat, that is, the type of variables (i) and (f).





Test Your Skills Now!
Take a Quiz now
Reviewer Name