C++ Fundamentals for Game Development
Let's explore some fundamental C++ concepts essential for game development:
1. Variables and Data Types
Variables store data in your program. C++ has various data types:
- int: Whole numbers (e.g., 10, -5, 0)
- float: Single-precision floating-point numbers (e.g., 3.14, -2.5)
- double: Double-precision floating-point numbers (e.g., 1.23456789)
- char: Single characters (e.g., 'A', '!', ' ')
- bool: True or False values (e.g., true, false)
#include
int main() {
int age = 25; // Integer variable
float score = 9.5; // Floating-point variable
char initial = 'J'; // Character variable
bool isPlaying = true; // Boolean variable
std::cout << "Age: " << age << std::endl;
std::cout << "Score: " << score << std::endl;
std::cout << "Initial: " << initial << std::endl;
std::cout << "Playing: " << isPlaying << std::endl;
return 0;
}
2. Operators
Operators perform actions on data. Common operators in C++ include:
- Arithmetic Operators: +, -, *, /, %, ++ (increment), -- (decrement)
- Comparison Operators: == (equal to), != (not equal to), > (greater than), < (less than), >= (greater than or equal to), <= (less than or equal to)
- Logical Operators: && (logical AND), || (logical OR), ! (logical NOT)
#include
int main() {
int x = 10, y = 5;
std::cout << "x + y = " << x + y << std::endl;
std::cout << "x - y = " << x - y << std::endl;
if (x > y) {
std::cout << "x is greater than y" << std::endl;
} else {
std::cout << "y is greater than or equal to x" << std::endl;
}
return 0;
}