What is Type Casting?
Type casting means converting one data type into another.
Two types:
-
Implicit Casting (Automatic / Type Conversion)
-
Explicit Casting (Manual / Type Casting)
1. Implicit Type Casting
👉 Done automatically by the compiler when needed.
👉 Also called type promotion.
Example:
int a = 5;
double b = a + 2.5; // 'a' is automatically converted to double
cout << b;
Rule:
Small → Big type (e.g., int → float → double)
2. Explicit Type Casting
👉 Done manually by the programmer.
👉 Two ways:
(a) C-Style Cast
double pi = 3.14;
int x = (int)pi; // Explicit cast
cout << x; // Output: 3
b) C++ Style Casts
C++ provides 4 safer casting operators
Cast Type
Syntax
Use Case
Example
static_cast
static_cast<type>(expr)
Normal
conversions (safe)
double d =
static_cast<double>(5);
dynamic_cast
dynamic_cast<type*>(ptr)
Downcasting
in inheritance
Derived* d =
dynamic_cast<Derived*>(basePtr);
const_cast
const_cast<type>(expr)
Add/remove const
display(const_cast<char*>(msg));
reinterpret_cast
reinterpret_cast<type>(expr)
Low-level,
bitwise conversion
char* c =
reinterpret_cast<char*>(&n);
static_cast
Used for normal conversions between related types.
int x = 10;
double y = static_cast<double>(x);
cout << y; // 10.0
dynamic_cast
Used with inheritance (and virtual functions).
Checks type safely at runtime.
class Animal { public: virtual void sound() {} };
class Dog : public Animal {};
Animal* a = new Dog();
Dog* d = dynamic_cast<Dog*>(a);
if (d) cout << "It's a Dog!";
const_cast
Used to remove constness of a variable.
void show(char* msg) { cout << msg; }
const char* text = "Hello";
show(const_cast<char*>(text)); // Removes const
Program: Type Casting and Casting Operators in C++
#include <iostream>
using namespace std;
class Animal {
public:
virtual void sound() { cout << "Some generic animal sound\n"; }
};
class Dog : public Animal {
public:
void sound() override { cout << "Dog barks!\n"; }
};
void printMessage(char* msg) {
cout << "Message: " << msg << endl;
}
int main() {
// ------------------------------
// 1. Implicit Type Casting
// ------------------------------
int a = 5;
double b = a + 2.5; // int automatically converted to double
cout << "Implicit casting (int + double): " << b << endl;
// ------------------------------
// 2. Explicit (C-style) Casting
// ------------------------------
double pi = 3.14159;
int intPi = (int)pi; // manual cast to int
cout << "Explicit (C-style) casting double -> int: " << intPi << endl;
// ------------------------------
// 3. static_cast
// ------------------------------
int x = 10;
double y = static_cast<double>(x); // safer cast
cout << "static_cast int -> double: " << y << endl;
// ------------------------------
// 4. dynamic_cast
// ------------------------------
Animal* animalPtr = new Dog(); // Base pointer to Derived object
Dog* dogPtr = dynamic_cast<Dog*>(animalPtr);
if (dogPtr)
cout << "dynamic_cast successful! It’s a Dog.\n";
else
cout << "dynamic_cast failed.\n";
// ------------------------------
// 5. const_cast
// ------------------------------
const char* message = "Hello Students!";
printMessage(const_cast<char*>(message)); // remove const safely
// ------------------------------
// 6. reinterpret_cast
// ------------------------------
int num = 65;
char* ch = reinterpret_cast<char*>(&num);
cout << "reinterpret_cast int -> char*: " << *ch << endl;
// Cleanup
delete animalPtr;
return 0;
}
0 Comments