Cpp Type cast Operator

TYPE CAST OPERATOR

c++ permits type conversion of variable or expression using the type cast operator.traditional C cast are augmented in c++ by a function call notation as a syntactic alternative. The following two versions are equivalent.

(type-name)expression // c notation
type-name (expression) // c++ notation

Example
average=sum/(float)i; // c notation
average=sum/float(i); // c++ notation

A type-name behaves as if it is a function for converting values to a designated(target) type. The function call notation usually leads to simplest(simple) expressions. however, This can be used only when if the type is an identifier, for example

p=int * (q);
is illegal. in such cases, we must use language 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 for example

typedef int * int_pt;
p=int_pt(q);

ANSI C++ adds the following new cast operators

* const_cast
* static_cast
* dynamic_cast
* reinterpret_cast

#include < iostream >
using namespace std;
main()
{
double x = 26.09399;
float y = 18.20;
int z ;
z = (int) x;
cout << "Statement 1 - Value of (int)x is :" < z = (int) y;
cout << "statement 2 - Value of (int)y is :" << z<< endl ;
return 0;
}

Statement 1 – Value of (int)x is :26
statement 2 – Value of (int)y is :18

  • C++ Dynamic Initialization of Objects
  • C++ Copy Constructor
  • C++ Dynamic Constructor
  • C++ Destructors
  • C++ Exercise
  • C++ Operator Overloading
  • C++ Overloading Unary Operators
  • Const pointer in C
  • Void pointer in c
  • C++ Overloading Binary Operators
  • C++ Overloading Binary Operators Using Friends
  • C++ Manipulation String Using Operators
  • C++ Rules for overloading operators
  • C++ Exercise
  • C++ Basic To class Type
  • C++ Class TO Basic Type
  • C++ One class To another class type
  • C++ Exercise
  • C++ Inheritance introduction
  • C++ Single Inheritance
  • C++ Multiple Inheritance
  • C++ Ambiguity Resolution in inheritance
  • C++ Hierarchical Inheritance
  • C++ Hybrid Inheritance
  • C++ Virtual Base Classes
  • C++ Exercise
  • C++ abstract class
  • C++ nesting of classes
  • C++ Exercise
  • C++ polymorphism
  • C++ Exercise
  • C++ pointers
  • C++ Pointers TO object
  • C++ this pointer
  • C++ Pointer to Derived class
  • C++ Virtual functions
  • C++ Exercise
  • C++ streams
  • C++ unformatted I/O operations
  • C++ Put() and get()
  • C++ getline() and write()
  • C++ Formatted console I/O
  • C++ Manipulators
  • C++ Exercise
  • C++ file handling
  • C++ file stream classes
  • C++ Open and closing file
  • C++ open using constructor
  • C++ open using open()
  • C++ Detecting End of file
  • C++ File modes
  • C++ File pointers and Manipulators
  • C++ Sequential I/O
  • C++ Reading and Writing
  • C++ Updating a File
  • C++ Error handling In File
  • C++ Command Line Arguments
  • C++ Exercise
  • C++ Template introduction
  • C++ Class Templates with multiple Parameters
  • C++ Function templates
  • C++ Function templates with multiple parameters
  • C++ member function Template
  • C++ Exercise
  • C++ Exception handling
  • C++ Basics of Exception Handling
  • C++ Exception Handling Mechanism
  • C++ Throwing Mechanism
  • C++ Catch Mechanism
  • C++ Catch all Exceptions
  • C++ Re-Throwing An Exception
  • C++ Specifying Exceptions
  • C++ Exercise