Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 85 additions & 0 deletions variable_and_datatypes/casting.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
// First things first. What IS casting?

// Casting is essentially just converting one data type into another.

// Casting comes in various forms, such as: Implicit casting, explicit casting.
// Implicit casting means that the compiler automatically converts the variable.
// Explicit casting means that the programmer explicitly converts the variable using a cast operator.

// Namely, in C++, you can use static_cast, dynamic_cast, const_cast, and reinterpret_cast for explicit casting.

#include <iostream>
#include <string> // You will see why we need this soon.

int main() {
std::cout << "Lets see how a cast works.\n";

// First, implicit casting...
// Take a variable. Say... a floating point variable.
float number = 3.14159f;
std::cout << "The number is: " << number << "\n";
// Now we wanna cast this to an integer.
int casted_number = number;
std::cout << "The casted number is: " << casted_number << "\n";
// Here, the first output will be 3.14159, and the second will be 3.
// This is because implicit casting cuts off the decimal part when converting from float to int.

// Another example of implicit casting...
int integer = 98;
std::cout << "Integer: " << integer << "\n";
// Now, lets cast this integer to a character.
char casted_char = integer;
std::cout << "Casted Integer: " << casted_char << "\n";
// The output will be 'b', because the ASCII character assigned to 98 is 'b'.


// Explicit casting...
// Let's take the same float variable.
float another_number = 2.71828f;
std::cout << "Another number: " << another_number << "\n";
// Now, we want to explicitly cast this to an integer.
int explicit_casted_number = static_cast<int>(another_number);
std::cout << "Casted number: " << explicit_casted_number << "\n";
// The output will be 2, because we explicitly told the compiler to convert it to an integer.
// Here, we used static_cast to perform the explicit casting.

// We can do this with C-style casting as well.
int c_style_casted_number = (int)another_number;
std::cout << "C-style casted number: " << c_style_casted_number << "\n";
// The output will still be 2, as we are explicitly converting the float to an integer.
// There are others like dynamic_cast, const_cast, and reinterpret_cast, but they are used for more specific purposes.

// Lets do another one with a string.
std::string str = "123";
std::cout << "String: " << str << "\n";
// Now, we want to convert this string to an integer.
int string_to_int = static_cast<int>(std::stoi(str));
std::cout << "Integer: " << string_to_int << "\n";
// The output will be 123, as we converted the string "123" to an integer.
// Just to meet your curiousity, stoi simply means "s(string) to i(integer).
// It's a function that comes with the "string" library we added at the top.

// Lets try with "67.345"
std::string str_float = "67.345";
std::cout << "String of a floating number: " << str_float << "\n";
// Now, we want to convert this string to a float.
float string_to_float = static_cast<float>(std::stof(str_float));
std::cout << "Floating type value of that string: " << string_to_float << "\n";
// The output will be 67.345, as we converted the string "67.345" to a float.

// Fun fact.
// Try with a string that contains characters, like "677ndc".
// To do that, we will need a second argument.
std::string str_with_chars = "677ndc";
std::cout << "String with characters: " << str_with_chars << "\n";
// Now, we want to convert this string to an integer.
size_t pos;
int string_with_chars_to_int = static_cast<int>(std::stoi(str_with_chars, &pos));
std::cout << "Integer of that string: " << string_with_chars_to_int << "\n";
// The otput will be 677. and the value of pos will be 3, which means it stopped converting at the first non-numeric character.
std::cout << "Position of first non-numeric character: " << pos << "\n";

// Note: size_t is an unsigned integer type that is used to represent the size of any object in bytes.

return 0;
}