Catalog / C++ Cheatsheet
C++ Cheatsheet
A quick reference guide to C++ syntax, data structures, and common operations. Designed for both beginners and experienced programmers to quickly recall essential information.
Fundamentals
Basic Syntax
|
Includes the iostream library for input/output operations. |
|
The main function, where program execution begins. |
|
Prints “Hello, World!” to the console. |
|
Reads input from the console into the specified variable. |
|
Indicates successful program execution. |
|
Statement terminator. |
Variables and Data Types
|
Integer data type (e.g., |
|
Single-precision floating-point number (e.g., |
|
Double-precision floating-point number (e.g., |
|
Character data type (e.g., |
|
Boolean data type (e.g., |
|
String data type (e.g., |
Operators
Arithmetic Operators: Assignment Operators: Comparison Operators: Logical Operators: Increment/Decrement Operators: |
Control Flow
Conditional Statements
|
Executes a block of code if the condition is true. |
|
Executes one block of code if the condition is true, and another block if it is false. |
|
Chained conditional statements. |
|
Selects one of several code blocks to execute based on the value of an expression. |
Loops
|
Executes a block of code repeatedly as long as the condition is true. |
|
Executes a block of code repeatedly as long as the condition is true. |
|
Executes a block of code at least once, and then repeatedly as long as the condition is true. |
|
Exits a loop or switch statement. |
|
Skips the rest of the current iteration of a loop and proceeds to the next iteration. |
Examples
|
|
Functions
Function Declaration & Definition
|
Defines a function with a specified return type, name, and parameters. |
|
A function that does not return a value. |
|
A function that adds two integers and returns the result. |
Function Overloading
Function overloading allows you to define multiple functions with the same name but different parameter lists (different number or types of parameters). |
|
Parameters
Pass by value |
A copy of the variable’s value is passed to the function. Changes to the parameter inside the function do not affect the original variable. |
Pass by reference |
A reference to the variable is passed to the function. Changes to the parameter inside the function do affect the original variable. Use |
Pass by pointer |
The memory address of the variable is passed to the function. Changes to the value at the memory address affect the original variable. Use |
Classes and Objects
Class Definition
|
Defines a new class. |
|
Members are accessible from outside the class. |
|
Members are only accessible from within the class. |
|
Members are accessible from within the class and its derived classes. |
Object Creation
|
Creates an object of the class. |
|
Creates an object on the heap and returns a pointer to it. Remember to |
Example
|