Step by Step C++ Beginner Guide
Table of Contents:
1. History of C++
Creator: Bjarne Stroustrup
Year: Started in 1979, first released in 1985
Why Created:
C language was very fast but lacked some features like classes, objects, data security.
Bjarne wanted a language with both speed of C and features of Object-Oriented Programming (OOP).
Use:
System software (like operating systems)
Game development
Browsers (e.g., Chrome uses C++)
Database engines (MySQL, MongoDB)
Embedded systems
2. Basic Structure of a C++ Program
#includeusing namespace std; int main() { cout << "Hello, World!" ; return 0; }
3. Step-by-step Reading and Explanation
Line 1: #include
Read as: "Hashtag include iostream"
(called "hash" or "preprocessor symbol") → tells compiler to include something before compilation.
include → means add a library.
Meaning: "Bring tools for input and output into my program."
Line 2: using namespace std;
Read as: "using namespace standard semicolon"
using → tells compiler we want to use something.
namespace → is like a box of code (a collection).
std → short for standard (the default box of code).
; → semicolon ends a statement (like a full stop in English).
Meaning: "Use the standard C++ tools without writing std:: every time."
Line 3: int main() {
Read as: "int main open round bracket close round bracket open curly bracket"
int → return type (function will give an integer number at end).
main → the starting point of every C++ program (execution begins here).
() → parentheses (means no input parameters given here).
{ → curly bracket opens → start of function body.
Meaning: "Here begins the main program."
Line 4: cout << "Hello, World!";
Read as: "see-out shift-left double-quote Hello World double-quote semicolon"
cout → character output (used to print on screen).
<< → "insertion operator" (sends data to output).
"Hello, World!" → string (text inside double quotes).
; → end of command.
Meaning: "Print Hello, World! on the screen."
Line 5: return 0;
Read as: "return zero semicolon"
return → send a value back.
0 → means successful execution (no error).
; → statement end.
Meaning: "Tell the operating system program ended successfully."
Line 6: }
Read as: "close curly bracket"
Ends the main function.
Meaning: "Program finished here."
4. 🔣 C++ Symbols — How to Read & Meaning
| Symbol | Read As | Meaning |
|---|---|---|
| # | Hashtag / Sharp | Preprocessor instruction (#include) |
| ; | Semicolon | End of a statement |
| { | Curly bracket open | Start of a block/function/class |
| } | Curly bracket close | End of a block/function/class |
| () | Round bracket open/close | Functions, conditions, arguments |
| [] | Square bracket open/close | Arrays / indexing |
| << | Less than less than | Insert data into output (cout) |
| >> | Greater than greater than | Extract data from input (cin) |
| " " | Double quote | Denotes a string |
| ' ' | Single quote | Denotes a single character |
| // | Double slash | Single-line comment |
| /* ... */ | Slash-star … star-slash | Multi-line comment |
| = | Equals | Assignment operator |
| + - * / % | Plus, minus, multiply, divide, modulo | Arithmetic operations |
| ++ | Plus plus | Increment by 1 |
| -- | Minus minus | Decrement by 1 |
| == | Equals equals | Check equality |
| != | Not equal | Check inequality |
| < > <= >= | Comparison operators | Less than / Greater than / Less equal / Greater equal |
| && | And | Logical AND |
| || | Or | Logical OR |
| ! | Not | Logical NOT |
| : | Colon | Used in labels, ternary operator, inheritance |
| ?: | Question mark colon | Ternary operator (if-else in one line) |
| . | Dot | Access object member |
| -> | Arrow | Access pointer object member |
| :: | Double colon | Scope resolution (std::cout) |
| \n | Backslash n | New line |
| \t | Backslash t | Tab / spacing |
5. 🆔 C++ Identifiers & Basics — Beginner Friendly Guide
1. What is an Identifier?
An identifier is a name you give to anything in C++ like variables, functions, classes, objects, constants, etc. It helps you refer to that item in your program.
2. What is a Data Type?
A data type tells the computer what kind of value a variable can store.
int age = 18; float salary = 5000.5; char grade = 'A';
👉 age, salary, grade = identifiers
👉 int, float, char = data types
👉 18, 5000.5, 'A' = values
3. What is an Operator?
Operators are symbols that perform operations on data.
- + → Addition
- - → Subtraction
- * → Multiplication
- / → Division
- = → Assignment
4. What is a Value?
A value is the actual data stored in a variable.
18, 3.14, 'A', 5000
5. What is a Keyword?
Keywords are reserved words in C++ that have a special meaning and cannot be used as identifiers.
int, float, char, if, else, return
6. 📝 C++ Naming Rules — Complete Step-by-Step Guide
1. General Rules for All Identifiers
- Names must begin with a letter (A–Z, a–z) or underscore (_).
- Names can contain letters, digits (0–9), and underscores.
- Names cannot start with a digit.
- Names cannot use spaces or special characters (@, $, %, -).
- Names cannot be a C++ keyword (like int, for).
- C++ is case-sensitive → Value and value are different.
- Names should be meaningful for readability.
// ✅ Correct int age; float monthly_salary; char gradeA; // ❌ Wrong int 1value; // cannot start with number float salary$; // invalid symbol char for; // keyword not allowed
2. Variable Naming Rules
| Rule | Example ✅ | Bad ❌ |
|---|---|---|
| Use lowercase or camelCase | studentAge | x |
| Can use underscore for clarity | total_salary | ts |
3. Constant Naming Rules
- Use ALL_CAPS with underscores for constants.
const double PI_VALUE = 3.1416; const int MAX_STUDENTS = 100;
4. Function Naming Rules
- Use camelCase or snake_case.
- Name should describe an action.
int addNumbers(int a, int b) { return a + b; }
5. Class & Object Naming Rules
| Entity | Convention | Example |
|---|---|---|
| Class | PascalCase | StudentRecord |
| Object | camelCase | student1 |
class CarModel { public: void startEngine() {} }; CarModel myCar;
6. Array Naming Rules
- Arrays should be plural.
int marks[5]; string names[10];
7. Pointer & Reference Naming Rules
- Pointers: use p or ptr prefix.
- References: use ref suffix.
int value; int* pValue = &value; int& valueRef = value;
8. Namespace Naming Rules
- Use lowercase for namespaces.
namespace school { int students = 50; }
9. Enum Naming Rules
- Enum type: PascalCase
- Enum values: ALL_CAPS
enum Color { RED, GREEN, BLUE };
10. Template Naming Rules
- Use single capital letters (T, U, V) for template parameters.
template <typename T> class Box { T value; };
11. Label Naming Rules
- Labels should be simple and descriptive.
startLoop: for (int i = 0; i < 5; i++) { if (i == 3) goto startLoop; }
12. Typedef / Using Naming Rules
typedef unsigned int uint; using Age = int;
13. Macro Naming Rules
- Use ALL_CAPS for macros.
#define PI 3.14159 #define MAX 100
7. 🔑 Complete C++ Keywords (with Meanings)
C++ keywords are reserved words. They cannot be used as identifiers (variable names, class names, etc.). Here’s the latest list with meanings (C++98 → C++11 → C++20 → C++23).
1. Data Types
| Keyword | Meaning |
|---|---|
| bool | Boolean type (true/false) |
| char | Character type (1 byte) |
| char8_t | UTF-8 character type (added in C++20) |
| char16_t | UTF-16 character type (added in C++11) |
| char32_t | UTF-32 character type (added in C++11) |
| int | Integer type |
| long | Long integer |
| short | Short integer |
| signed | Signed modifier (can be + or -) |
| unsigned | Unsigned modifier (only positive) |
| float | Single-precision floating number |
| double | Double-precision floating number |
| void | No return / empty type |
| wchar_t | Wide character type |
2. Storage Class & Type Modifiers
| Keyword | Meaning |
|---|---|
| const | Constant (cannot change value) |
| volatile | Prevents compiler optimization |
| mutable | Allows modification inside const object |
| auto | Type deduced automatically |
| register | Suggest store in CPU register (deprecated) |
| static | Preserves value between function calls |
| extern | Variable defined outside file |
| thread_local | Each thread has its own instance |
3. Control Flow
| Keyword | Meaning |
|---|---|
| if, else | Conditional branching |
| switch, case, default | Multi-branch selection |
| while, do, for | Looping statements |
| break | Exit from loop/switch |
| continue | Skip current loop iteration |
| goto | Jump to label (not recommended) |
| return | Return value from function |
4. Functions & Exception Handling
| Keyword | Meaning |
|---|---|
| try, catch, throw | Exception handling |
| noexcept | Specifies function won’t throw exceptions |
| operator | Defines custom operator overloading |
| explicit | Prevents implicit type conversion |
| friend | Allows access to private members |
| inline | Suggests inline expansion of function |
| virtual | Defines virtual function for polymorphism |
| override | Ensures function overrides base version |
| final | Prevents further inheritance/overriding |
5. Object-Oriented Keywords
| Keyword | Meaning |
|---|---|
| class, struct, union | Define user-defined types |
| enum | Define enumerations |
| private, protected, public | Access specifiers |
| this | Pointer to current object |
| new, delete | Dynamic memory allocation & deallocation |
6. Casting Operators
| Keyword | Meaning |
|---|---|
| dynamic_cast | Safe downcasting (runtime check) |
| const_cast | Modify const-ness |
| reinterpret_cast | Convert one pointer type to another |
| static_cast | Compile-time conversion |
7. Type Information
| Keyword | Meaning |
|---|---|
| sizeof | Returns size of type/variable |
| decltype | Infers type of expression |
| typeid | Gives runtime type info |
| alignof, alignas | Memory alignment |
8. Namespace & Templates
| Keyword | Meaning |
|---|---|
| namespace | Defines scope grouping |
| using | Alias or bring namespace into scope |
| template | Generic programming |
| typename | Defines template type parameter |
| concept, requires | Constraints in templates (C++20) |
9. Coroutines (C++20)
| Keyword | Meaning |
|---|---|
| co_await | Suspends coroutine until ready |
| co_yield | Produces a value from coroutine |
| co_return | Returns from coroutine |
10. Boolean & Literals
| Keyword | Meaning |
|---|---|
| true, false | Boolean constants |
| nullptr | Null pointer literal (C++11) |
11. Alternative Operator Keywords
C++ also provides alternative spellings for operators:
| Keyword | Equivalent |
|---|---|
| and | && |
| or | || |
| not | ! |
| bitand | & |
| bitor | | |
| xor | ^ |
| compl | ~ |
| and_eq | &= |
| or_eq | |= |
| xor_eq | ^= |
| not_eq | != |
🔢 Total Keywords
C++98: 64 keywords
C++11: +10 new
C++20: +7 new
C++23: +2 new
Alternative Operators: 11
✅ Total Reserved Keywords = 94
Step by Step C++ Beginner Guide
Table of Contents:
1. History of C++
Creator: Bjarne Stroustrup
Year: Started in 1979, first released in 1985
Why Created:
C language was very fast but lacked some features like classes, objects, data security.
Bjarne wanted a language with both speed of C and features of Object-Oriented Programming (OOP).
Use:
System software (like operating systems)
Game development
Browsers (e.g., Chrome uses C++)
Database engines (MySQL, MongoDB)
Embedded systems
2. Basic Structure of a C++ Program
#includeusing namespace std; int main() { cout << "Hello, World!" ; return 0; }
3. Step-by-step Reading and Explanation
Line 1: #include
Read as: "Hashtag include iostream"
(called "hash" or "preprocessor symbol") → tells compiler to include something before compilation.
include → means add a library.
Meaning: "Bring tools for input and output into my program."
Line 2: using namespace std;
Read as: "using namespace standard semicolon"
using → tells compiler we want to use something.
namespace → is like a box of code (a collection).
std → short for standard (the default box of code).
; → semicolon ends a statement (like a full stop in English).
Meaning: "Use the standard C++ tools without writing std:: every time."
Line 3: int main() {
Read as: "int main open round bracket close round bracket open curly bracket"
int → return type (function will give an integer number at end).
main → the starting point of every C++ program (execution begins here).
() → parentheses (means no input parameters given here).
{ → curly bracket opens → start of function body.
Meaning: "Here begins the main program."
Line 4: cout << "Hello, World!";
Read as: "see-out shift-left double-quote Hello World double-quote semicolon"
cout → character output (used to print on screen).
<< → "insertion operator" (sends data to output).
"Hello, World!" → string (text inside double quotes).
; → end of command.
Meaning: "Print Hello, World! on the screen."
Line 5: return 0;
Read as: "return zero semicolon"
return → send a value back.
0 → means successful execution (no error).
; → statement end.
Meaning: "Tell the operating system program ended successfully."
Line 6: }
Read as: "close curly bracket"
Ends the main function.
Meaning: "Program finished here."
4. 🔣 C++ Symbols — How to Read & Meaning
| Symbol | Read As | Meaning |
|---|---|---|
| # | Hashtag / Sharp | Preprocessor instruction (#include) |
| ; | Semicolon | End of a statement |
| { | Curly bracket open | Start of a block/function/class |
| } | Curly bracket close | End of a block/function/class |
| () | Round bracket open/close | Functions, conditions, arguments |
| [] | Square bracket open/close | Arrays / indexing |
| << | Less than less than | Insert data into output (cout) |
| >> | Greater than greater than | Extract data from input (cin) |
| " " | Double quote | Denotes a string |
| ' ' | Single quote | Denotes a single character |
| // | Double slash | Single-line comment |
| /* ... */ | Slash-star … star-slash | Multi-line comment |
| = | Equals | Assignment operator |
| + - * / % | Plus, minus, multiply, divide, modulo | Arithmetic operations |
| ++ | Plus plus | Increment by 1 |
| -- | Minus minus | Decrement by 1 |
| == | Equals equals | Check equality |
| != | Not equal | Check inequality |
| < > <= >= | Comparison operators | Less than / Greater than / Less equal / Greater equal |
| && | And | Logical AND |
| || | Or | Logical OR |
| ! | Not | Logical NOT |
| : | Colon | Used in labels, ternary operator, inheritance |
| ?: | Question mark colon | Ternary operator (if-else in one line) |
| . | Dot | Access object member |
| -> | Arrow | Access pointer object member |
| :: | Double colon | Scope resolution (std::cout) |
| \n | Backslash n | New line |
| \t | Backslash t | Tab / spacing |
5. 🆔 C++ Identifiers & Basics — Beginner Friendly Guide
1. What is an Identifier?
An identifier is a name you give to anything in C++ like variables, functions, classes, objects, constants, etc. It helps you refer to that item in your program.
2. What is a Data Type?
A data type tells the computer what kind of value a variable can store.
int age = 18; float salary = 5000.5; char grade = 'A';
👉 age, salary, grade = identifiers
👉 int, float, char = data types
👉 18, 5000.5, 'A' = values
3. What is an Operator?
Operators are symbols that perform operations on data.
- + → Addition
- - → Subtraction
- * → Multiplication
- / → Division
- = → Assignment
4. What is a Value?
A value is the actual data stored in a variable.
18, 3.14, 'A', 5000
5. What is a Keyword?
Keywords are reserved words in C++ that have a special meaning and cannot be used as identifiers.
int, float, char, if, else, return
6. 📝 C++ Naming Rules — Complete Step-by-Step Guide
1. General Rules for All Identifiers
- Names must begin with a letter (A–Z, a–z) or underscore (_).
- Names can contain letters, digits (0–9), and underscores.
- Names cannot start with a digit.
- Names cannot use spaces or special characters (@, $, %, -).
- Names cannot be a C++ keyword (like int, for).
- C++ is case-sensitive → Value and value are different.
- Names should be meaningful for readability.
// ✅ Correct int age; float monthly_salary; char gradeA; // ❌ Wrong int 1value; // cannot start with number float salary$; // invalid symbol char for; // keyword not allowed
2. Variable Naming Rules
| Rule | Example ✅ | Bad ❌ |
|---|---|---|
| Use lowercase or camelCase | studentAge | x |
| Can use underscore for clarity | total_salary | ts |
3. Constant Naming Rules
- Use ALL_CAPS with underscores for constants.
const double PI_VALUE = 3.1416; const int MAX_STUDENTS = 100;
4. Function Naming Rules
- Use camelCase or snake_case.
- Name should describe an action.
int addNumbers(int a, int b) { return a + b; }
5. Class & Object Naming Rules
| Entity | Convention | Example |
|---|---|---|
| Class | PascalCase | StudentRecord |
| Object | camelCase | student1 |
class CarModel { public: void startEngine() {} }; CarModel myCar;
6. Array Naming Rules
- Arrays should be plural.
int marks[5]; string names[10];
7. Pointer & Reference Naming Rules
- Pointers: use p or ptr prefix.
- References: use ref suffix.
int value; int* pValue = &value; int& valueRef = value;
8. Namespace Naming Rules
- Use lowercase for namespaces.
namespace school { int students = 50; }
9. Enum Naming Rules
- Enum type: PascalCase
- Enum values: ALL_CAPS
enum Color { RED, GREEN, BLUE };
10. Template Naming Rules
- Use single capital letters (T, U, V) for template parameters.
template <typename T> class Box { T value; };
11. Label Naming Rules
- Labels should be simple and descriptive.
startLoop: for (int i = 0; i < 5; i++) { if (i == 3) goto startLoop; }
12. Typedef / Using Naming Rules
typedef unsigned int uint; using Age = int;
13. Macro Naming Rules
- Use ALL_CAPS for macros.
#define PI 3.14159 #define MAX 100
7. 🔑 Complete C++ Keywords (with Meanings)
C++ keywords are reserved words. They cannot be used as identifiers (variable names, class names, etc.). Here’s the latest list with meanings (C++98 → C++11 → C++20 → C++23).
1. Data Types
| Keyword | Meaning |
|---|---|
| bool | Boolean type (true/false) |
| char | Character type (1 byte) |
| char8_t | UTF-8 character type (added in C++20) |
| char16_t | UTF-16 character type (added in C++11) |
| char32_t | UTF-32 character type (added in C++11) |
| int | Integer type |
| long | Long integer |
| short | Short integer |
| signed | Signed modifier (can be + or -) |
| unsigned | Unsigned modifier (only positive) |
| float | Single-precision floating number |
| double | Double-precision floating number |
| void | No return / empty type |
| wchar_t | Wide character type |
2. Storage Class & Type Modifiers
| Keyword | Meaning |
|---|---|
| const | Constant (cannot change value) |
| volatile | Prevents compiler optimization |
| mutable | Allows modification inside const object |
| auto | Type deduced automatically |
| register | Suggest store in CPU register (deprecated) |
| static | Preserves value between function calls |
| extern | Variable defined outside file |
| thread_local | Each thread has its own instance |
3. Control Flow
| Keyword | Meaning |
|---|---|
| if, else | Conditional branching |
| switch, case, default | Multi-branch selection |
| while, do, for | Looping statements |
| break | Exit from loop/switch |
| continue | Skip current loop iteration |
| goto | Jump to label (not recommended) |
| return | Return value from function |
4. Functions & Exception Handling
| Keyword | Meaning |
|---|---|
| try, catch, throw | Exception handling |
| noexcept | Specifies function won’t throw exceptions |
| operator | Defines custom operator overloading |
| explicit | Prevents implicit type conversion |
| friend | Allows access to private members |
| inline | Suggests inline expansion of function |
| virtual | Defines virtual function for polymorphism |
| override | Ensures function overrides base version |
| final | Prevents further inheritance/overriding |
5. Object-Oriented Keywords
| Keyword | Meaning |
|---|---|
| class, struct, union | Define user-defined types |
| enum | Define enumerations |
| private, protected, public | Access specifiers |
| this | Pointer to current object |
| new, delete | Dynamic memory allocation & deallocation |
6. Casting Operators
| Keyword | Meaning |
|---|---|
| dynamic_cast | Safe downcasting (runtime check) |
| const_cast | Modify const-ness |
| reinterpret_cast | Convert one pointer type to another |
| static_cast | Compile-time conversion |
7. Type Information
| Keyword | Meaning |
|---|---|
| sizeof | Returns size of type/variable |
| decltype | Infers type of expression |
| typeid | Gives runtime type info |
| alignof, alignas | Memory alignment |
8. Namespace & Templates
| Keyword | Meaning |
|---|---|
| namespace | Defines scope grouping |
| using | Alias or bring namespace into scope |
| template | Generic programming |
| typename | Defines template type parameter |
| concept, requires | Constraints in templates (C++20) |
9. Coroutines (C++20)
| Keyword | Meaning |
|---|---|
| co_await | Suspends coroutine until ready |
| co_yield | Produces a value from coroutine |
| co_return | Returns from coroutine |
10. Boolean & Literals
| Keyword | Meaning |
|---|---|
| true, false | Boolean constants |
| nullptr | Null pointer literal (C++11) |
11. Alternative Operator Keywords
C++ also provides alternative spellings for operators:
| Keyword | Equivalent |
|---|---|
| and | && |
| or | || |
| not | ! |
| bitand | & |
| bitor | | |
| xor | ^ |
| compl | ~ |
| and_eq | &= |
| or_eq | |= |
| xor_eq | ^= |
| not_eq | != |
🔢 Total Keywords
C++98: 64 keywords
C++11: +10 new
C++20: +7 new
C++23: +2 new
Alternative Operators: 11
✅ Total Reserved Keywords = 94