Your technical depth starts here.
C++ Basics, Syntax, and I/O
Module 1 – C++ Basics, Syntax, and I/O
In Module 1, the goal is: Understand the structure of a simple C++ program Learn basic types, variables, and constants Learn how to compile and run code Use std::cout and std::cin for simple I/O
1. Introduction and “Hello World”
Definition (concept): C++ is a general-purpose, compiled programming language used for system software, game engines, high-performance applications, and teaching programming concepts.wscubetech Basic program structure:
#include <iostream> // Preprocessor directive: include I/O library
int main() { // Entry point function
std::cout << "Hello QuickRef\n"; // Output to the console
return 0; // Exit status 0 (success)
}#include <iostream> tells the compiler to use the standard input/output stream library. int main() is the function where program execution begins. std::cout prints text; << is the “insertion” operator. Compiling and running (Linux/macOS terminal):
g++ hello.cpp -o hello // Compile: source -> executable
./hello // Run the programOn Windows with g++ (MinGW), you might do:
g++ hello.cpp -o hello.exe
hello.exeThis mirrors your cheatsheet’s “Getting Started” section.w3schools+1
2. Basic Syntax and Statements
Definition: A C++ program is made of statements (like std::cout << 5;) grouped into blocks using {}. Each statement ends with a semicolon. Key syntax rules: Curly braces {} define a block (like inside main or if). Each line that does something (print, calculate) ends with ;. Whitespace and newlines usually don’t matter, but make code readable. Example:
#include <iostream>
int main() {
std::cout << "Line 1\n";
std::cout << "Line 2\n"; // Each statement ends with ;
}
3. Primitive Data Types and Variables
Definition: A variable is a named storage location that holds a value of a specific type (like int or double). Types control what kind of data and what range of values can be stored.scribd+1 Common types (as in your cheatsheet): int – integer numbers (typically 4 bytes, around −2,147,483,648 to 2,147,483,647). float – single-precision floating point (decimal numbers). double – double-precision floating point. char – single character (usually 1 byte). bool – Boolean: true or false. void – “no type” (used for functions that don’t return anything). wchar_t – wide character type (typically 2 or 4 bytes).asgteach Declaring and initializing variables:
#include <iostream>
#include <string> // needed for std::string
int main() {
int number = 5; // Integer
float f = 0.95f; // Float (note the f suffix is common)
double PI = 3.14159; // Double
char yes = 'Y'; // Character
std::string s = "ME"; // String
bool isRight = true; // Boolean
std::cout << number << " " << PI << " " << s << "\n";
}Brace initialization (C++11 style):
int age {25}; // Uniform initialization
std::cout << age; // Prints 25This style helps avoid some subtle bugs and is widely used in modern C++.kdab+1 Constants:
const float RATE = 0.8f; // RATE cannot be changed later
4. Operators (Basic Overview)
You already listed many in your cheatsheet; here’s how they look in code. Arithmetic operators:
int a = 10, b = 3;
int sum = a + b; // 13
int diff = a - b; // 7
int prod = a * b; // 30
int quot = a / b; // 3 (integer division)
int rem = a % b; // 1 (remainder)Assignment and compound assignment:
int x = 5;
x += 2; // x = x + 2 -> 7
x *= 3; // x = x * 3 -> 21Relational and logical operators:
int n = 10;
bool isTen = (n == 10); // true
bool big = (n > 5); // true
bool ok = (isTen && big); // trueTernary operator (from your cheatsheet):
int x = 3, y = 5;int max = (x > y) ? x : y; // Result is the larger of x and y std::cout << max << "\n"; // Outputs: 5
5. Comments
Comments document code and are ignored by the compiler.
// This is a single-line comment
/* This is a multi-line comment */ Use comments to explain why you are doing something, especially in longer programs.
6. Basic Input and Output
Output with std::cout:
#include <iostream>
int main() {
std::cout << "Hello QuickRef\n";
std::cout << "Number: " << 42 << "\n";
}
<< chains multiple things into a single output statement.Input with std::cin:
#include <iostream>
int main() {
int num;
std::cout << "Type a number: ";
std::cin >> num; // Reads from keyboard
std::cout << "You entered " << num << "\n";
}This matches the “User Input” section of your cheatsheet and is essential for interactive programs.w3schools
We’ve now: Defined what a C++ program looks like Compiled and run a simple hello.cpp Used basic types, variables, and constants Introduced operators, comments, and simple I/O
