C++ Codes - Basic to Advanced 💻
رفتن به کانال در Telegram
💡 Daily C++ Codes with Output & Logic Any queries, message 👉 @sai7981 🤖 Get instant code explanations: @cpp_codes_bot 🔗 Join now: @cpp_code_snippets #cpp #dsa #coding #programming
نمایش بیشتر3 188
مشترکین
اطلاعاتی وجود ندارد24 ساعت
+357 روز
+12030 روز
آرشیو پست ها
Swapping Secrets: Can you exchange numbers in C++ without a third variable?
#include <iostream>
int main() {
int a = 10;
int b = 20;
std::cout << "Before swap: a = " << a << ", b = " << b << std::endl;
a = a + b;
b = a - b;
a = a - b;
std::cout << "After swap: a = " << a << ", b = " << b << std::endl;
return 0;
}Can you swap two numbers in C++ using a third variable?
#include <iostream>
int main() {
int a = 10;
int b = 5;
int temp;
std::cout << "Before swap: a = " << a << ", b = " << b << std::endl;
temp = a;
a = b;
b = temp;
std::cout << "After swap: a = " << a << ", b = " << b << std::endl;
return 0;
}Oops! 💥 What happens when your C++ numbers get TOO BIG?
#include <iostream>
#include <limits>
int main() {
int smallNum = 127;
std::cout << "Small Number: " << smallNum << std::endl;
smallNum = smallNum + 1;
std::cout << "Small Number + 1: " << smallNum << std::endl;
short bigNum = 32767;
std::cout << "Big Number: " << bigNum << std::endl;
bigNum = bigNum + 1;
std::cout << "Big Number + 1: " << bigNum << std::endl;
unsigned int maxUnsigned = std::numeric_limits<unsigned int>::max();
std::cout << "Max Unsigned Int: " << maxUnsigned << std::endl;
maxUnsigned = maxUnsigned + 1;
std::cout << "Max Unsigned Int + 1: " << maxUnsigned << std::endl;
return 0;
}Unlocking Memory: How big are your C++ data types?
#include <iostream>
int main() {
std::cout << "Size of int: " << sizeof(int) << " bytes" << std::endl;
std::cout << "Size of char: " << sizeof(char) << " byte" << std::endl;
std::cout << "Size of float: " << sizeof(float) << " bytes" << std::endl;
std::cout << "Size of double: " << sizeof(double) << " bytes" << std::endl;
return 0;
}Unlocking C++: What secrets lie within int, char, float, and double?
#include <iostream>
int main() {
int age = 30;
char initial = 'J';
float price = 99.99f;
double pi = 3.14159265359;
std::cout << "Age: " << age << std::endl;
std::cout << "Initial: " << initial << std::endl;
std::cout << "Price: " << price << std::endl;
std::cout << "Pi: " << pi << std::endl;
return 0;
}ful of potential data loss and use explicit casts cautiously.
Keep practicing, and you'll become a master of data types and type casting in no time! Good luck, and happy coding! 🚀
Hey there, future C++ wizards! 👋 Let's dive into the world of Data Types and Type Casting. It's a foundational concept, but trust me, mastering it will make your coding life MUCH easier. 😉
**What are Data Types?
🧠**
Think of data types as labels or categories for different kinds of information your program needs to handle. It's like organizing your toys into separate bins: one for LEGOs, one for action figures, and another for board games. C++ has built-in data types to handle numbers, text, and even truth values.
- `int`: For whole numbers (like -10, 0, 42). 🧮
- `float`: For decimal numbers (like 3.14, -2.5). 🌊
- `double`: Also for decimal numbers, but with higher precision than `float` (more digits after the decimal). 🔍
- `char`: For single characters (like 'A', 'z', '5'). 🔤
- `bool`: For true/false values. (true or false) ✅/❌
- `string`: For sequences of characters (like "Hello, world!"). 💬
Each data type takes up a certain amount of space in your computer's memory. The `sizeof()` operator can tell you how much space (in bytes) a data type occupies. The size may vary slightly depending on your system.
**Why Data Types Matter? 🤔**
Data types tell the compiler how to interpret the data stored in a variable. If you try to store text in an `int` variable, the compiler will complain! ⚠️ They also determine what operations you can perform on the data. You can add two `int` variables, but you can't (easily) add a `string` and an `int`.
**Type Casting: Changing Hats 🎩 -> 👷♀️ -> 👩🍳**
Type casting, also known as type conversion, is the process of converting a value from one data type to another. Imagine you have a `float` representing a price ($10.99) and you want to display it as a whole dollar amount. You might need to convert the `float` to an `int`.
There are two main types of type casting:
1. **Implicit Conversion (Automatic)**: The compiler performs this automatically when it's safe to do so, usually when converting from a "smaller" type to a "larger" type without losing information.
- Example: `int my_int = 5; double my_double = my_int;` (The `int` 5 is automatically converted to the `double` 5.0)
2. **Explicit Conversion (Manual)**: You tell the compiler exactly how you want to convert the data using casting operators. This is necessary when there's a risk of losing information or when the compiler can't figure out the conversion on its own.
- There are several ways to do this in C++:
- **(C-style cast)**: `int my_int = (int)3.14;` (This is the older way and generally discouraged). 🙅♀️
- **`static_cast`**: `int my_int = static_cast<int>(3.14);` (This is generally the preferred method for standard conversions.) ✅
- **`dynamic_cast`**, `reinterpret_cast`, and `const_cast`: These are more advanced casts used in specific situations (like dealing with inheritance and pointers), which we won't cover in detail here.
**Important Considerations 💡**
- When you convert a `float` or `double` to an `int`, the decimal part is truncated (cut off), NOT rounded. So, `3.99` becomes `3`, and `3.14` also becomes `3`.
- Be careful with explicit type casting! If you force a conversion that doesn't make sense, you could end up with unexpected results or even program crashes. 💥
**Example Time! 💻**
#include <iostream>
#include <string>
int main() {
int age = 30;
double price = 19.99;
std::string name = "Alice";
std::cout << "Name: " << name << std::endl;
std::cout << "Age: " << age << std::endl;
std::cout << "Price: " << price << std::endl;
// Type casting example
int whole_price = static_cast<int>(price);
std::cout << "Whole price: " << whole_price << std::endl; // Output: 19
return 0;
}
**In Summary ✅**
Data types categorize data, ensuring proper handling and operations. Type casting lets you convert between data types, but be mindDecoding Data: Can your C++ program handle numbers, words, and true/false values?
#include <iostream>
int main() {
int age;
double price;
char initial;
bool is_student;
std::string name;
std::cout << "Enter your age: ";
std::cin >> age;
std::cout << "Enter the price of an item: ";
std::cin >> price;
std::cout << "Enter your first initial: ";
std::cin >> initial;
std::cout << "Are you a student? (1 for true, 0 for false): ";
std::cin >> is_student;
std::cout << "Enter your name: ";
std::cin.ignore();
std::getline(std::cin, name);
std::cout << "
Your Information:
";
std::cout << "Name: " << name << "
";
std::cout << "Age: " << age << "
";
std::cout << "Price: " << price << "
";
std::cout << "Initial: " << initial << "
";
std::cout << "Student: " << (is_student ? "Yes" : "No") << "
";
return 0;
}Decoding Data: Can you read and display different data types in C++?
#include <iostream>
int main() {
int age;
double height;
char initial;
std::cout << "Enter your age: ";
std::cin >> age;
std::cout << "Enter your height (in meters): ";
std::cin >> height;
std::cout << "Enter your initial: ";
std::cin >> initial;
std::cout << "
Your age is: " << age << std::endl;
std::cout << "Your height is: " << height << " meters" << std::endl;
std::cout << "Your initial is: " << initial << std::endl;
return 0;
}Decoding Data Types: Can you handle input and output like a C++ pro?
#include <iostream>
int main() {
int age;
double gpa;
char grade;
bool isStudent;
std::string name;
std::cout << "Enter your name: ";
std::cin >> name;
std::cout << "Enter your age: ";
std::cin >> age;
std::cout << "Enter your GPA: ";
std::cin >> gpa;
std::cout << "Enter your grade: ";
std::cin >> grade;
std::cout << "Are you a student? (1 for yes, 0 for no): ";
std::cin >> isStudent;
std::cout << "
Your Name: " << name << std::endl;
std::cout << "Your Age: " << age << std::endl;
std::cout << "Your GPA: " << gpa << std::endl;
std::cout << "Your Grade: " << grade << std::endl;
std::cout << "Is Student: " << isStudent << std::endl;
return 0;
}