0% found this document useful (0 votes)
2 views

Type Cast Operator.ppt

The document explains the type cast operator in C++, which allows explicit type conversion of variables using both C notation and C++ notation. It also introduces ANSI C++'s new cast operations such as const_cast, static_cast, dynamic_cast, and reinterpret_cast. Additionally, it discusses memory management operators in C++ like new and delete, which provide better memory allocation and deallocation compared to traditional C functions like malloc and free.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Type Cast Operator.ppt

The document explains the type cast operator in C++, which allows explicit type conversion of variables using both C notation and C++ notation. It also introduces ANSI C++'s new cast operations such as const_cast, static_cast, dynamic_cast, and reinterpret_cast. Additionally, it discusses memory management operators in C++ like new and delete, which provide better memory allocation and deallocation compared to traditional C functions like malloc and free.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Type cast operator

Type cast operator


• C++ permits explicit type conversion of variables
or expressions using the type cast operator.
• (type – name) expression C notation
• Average= sum/ float (i); // C++ notation
• A type – name behaves as if it is a function for
converting values to a designated type. The
function-call notation usually leads to simplest
expressions. However, it can be used only if the
type is an identifier. For example,
• P= int * (q);
• Is illegal. In such eases, we must use C type
notation.
• P= (int *) q;
• Alternatively, we can use typedef to create an
identifier of the required type and use it in the
functional notation.
• Typedef int * int-pt;
• P= int-pt (q);
• ANSI C++ adds the following new cast
operation:
• * const-cast
• *static-cast
• *dynamic-cast
• *reinterpret-case
Memory Management Operators
• C user malloc () and calloc() functions to allocate
memory dynamically at run time. Similarly, it
uses the function free() to free dynamically
allocated memory. We use dynamic allocation
techniques when it is not known in advance how
much of memory space is needed. Although C=
supports these functions, it also defines two
unary operators new and delete that perform the
task of allocating and freeing the memory in a
better and easier way. Since these operators
manipulate memory on the free store, they are
also known as free store operators.


• An object can be created by using new, and destroyed
by using delete, as and when required. A data object
created inside a block with new, will remain in existence
until it is explicitly destroyed by using delete. Thus, the
lifetime of an object is directly under our control and is
unrelated to the block structure of the program.
• The new operator can be used to create objects of
any type. It takes the following general form:

• pointer-variable = new data-type;


• Here, pointer-variable is a pointer of type data-type.
The new operator allocates sufficient memory to hold a
data object of type data-type and returns the address of
the object.

You might also like