Back to Premium Vault
Premium Vault / C++ ProgrammingFree Programming Course

CODESTUDIO / GO BEYOND SYNTAX

C++ ProgrammingSyntax, OOP & Modern C++

A structured C++ course covering setup, syntax, control flow, arrays and functions, object-oriented design, modern C++, templates and preprocessor essentials.

6 learning modules18 review questions30 practice drillsSelf-paced learning
C++ Programming / learning workspace

Your technical depth starts here.

MODULE 1 / 6

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:

cpp
#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):

bash
g++ hello.cpp -o hello   // Compile: source -> executable
./hello                  // Run the program

On Windows with g++ (MinGW), you might do:

bash
g++ hello.cpp -o hello.exe
hello.exe

This 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:

cpp
#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:

cpp
#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):

cpp
int age {25};      // Uniform initialization
std::cout << age;  // Prints 25

This style helps avoid some subtle bugs and is widely used in modern C++.kdab+1 Constants:

cpp
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:

cpp
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:

cpp
int x = 5;
x += 2;  // x = x + 2 -> 7
x *= 3;  // x = x * 3 -> 21

Relational and logical operators:

cpp
int n = 10;
bool isTen = (n == 10);  // true
bool big = (n > 5);      // true
bool ok = (isTen && big); // true

Ternary operator (from your cheatsheet):

cpp
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.

cpp

// 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:

cpp
#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:

cpp
#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