# **Comprehensive C++ Documentation for Beginners**
## **Table of Contents**
1. [Introduction to C++](#1-introduction-to-c)
2. [Setting Up C++](#2-setting-up-c)
- [Installing a C++ Compiler](#installing-a-c-compiler)
- [Choosing an IDE/Code Editor](#choosing-an-idecode-editor)
3. [C++ Basics](#3-c-basics)
- [Hello World](#hello-world)
- [Variables and Data Types](#variables-and-data-types)
- [Operators](#operators)
- [Input and Output](#input-and-output)
4. [Control Structures](#4-control-structures)
- [Conditional Statements](#conditional-statements)
- [Loops](#loops)
5. [Functions](#5-functions)
6. [Arrays and Strings](#6-arrays-and-strings)
- [Arrays](#arrays)
- [Strings](#strings)
7. [Pointers and References](#7-pointers-and-references)
8. [Object-Oriented Programming (OOP)](#8-object-oriented-programming-oop)
- [Classes and Objects](#classes-and-objects)
- [Inheritance](#inheritance)
- [Polymorphism](#polymorphism)
9. [File Handling](#9-file-handling)
10. [Error Handling](#10-error-handling)
11. [Standard Template Library (STL)](#11-standard-template-library-stl)
12. [Next Steps](#12-next-steps)
---
## **1. Introduction to C++**
C++ is a high-performance, general-purpose programming language developed as an extension
of C. It supports procedural, object-oriented, and generic programming paradigms.
**Key Features:**
- High performance and efficiency
- Strongly typed
- Rich standard library (STL)
- Memory management (manual and smart pointers)
- Multi-paradigm (OOP, procedural, functional)
**Use Cases:**
- Game Development (Unreal Engine)
- System/OS Programming
- Embedded Systems
- High-Frequency Trading
- Graphics and Simulations
---
## **2. Setting Up C++**
### **Installing a C++ Compiler**
1. **Windows**: Install [MinGW](http://www.mingw.org/) or use **Visual Studio** (includes
MSVC compiler).
2. **Linux**: Use `g++` (GNU C++ Compiler). Install via:
```sh
sudo apt install g++
```
3. **Mac**: Install `clang` (Xcode Command Line Tools):
```sh
xcode-select --install
```
### **Choosing an IDE/Code Editor**
- **Visual Studio (VS Code)** (Lightweight with extensions)
- **CLion** (JetBrains IDE for C++)
- **Code::Blocks** (Lightweight, cross-platform)
- **Eclipse CDT** (For large projects)
- **Sublime Text / Atom** (With C++ plugins)
---
## **3. C++ Basics**
### **Hello World**
```cpp
#include <iostream>
using namespace std;
int main() {
cout << "Hello, World!" << endl;
return 0;
```
Save as `hello.cpp` and compile:
```sh
g++ hello.cpp -o hello
./hello
```
### **Variables and Data Types**
- **`int`** – `int age = 25;`
- **`float`** – `float pi = 3.14f;`
- **`double`** – `double distance = 5.67;`
- **`char`** – `char grade = 'A';`
- **`bool`** – `bool is_valid = true;`
- **`string`** – `string name = "Alice";` (Requires `#include <string>`)
### **Operators**
- **Arithmetic**: `+`, `-`, `*`, `/`, `%`, `++`, `--`
- **Comparison**: `==`, `!=`, `>`, `<`, `>=`, `<=`
- **Logical**: `&&`, `||`, `!`
- **Bitwise**: `&`, `|`, `^`, `~`, `<<`, `>>`
### **Input and Output**
```cpp
int age;
cout << "Enter your age: ";
cin >> age;
cout << "You are " << age << " years old." << endl;
```
---
## **4. Control Structures**
### **Conditional Statements**
```cpp
int score = 85;
if (score >= 90) {
cout << "A Grade" << endl;
} else if (score >= 80) {
cout << "B Grade" << endl;
} else {
cout << "C Grade" << endl;
```
### **Loops**
- **`for` Loop**:
```cpp
for (int i = 0; i < 5; i++) {
cout << i << endl;
```
- **`while` Loop**:
```cpp
int i = 0;
while (i < 5) {
cout << i << endl;
i++;
```
- **`do-while` Loop**:
```cpp
int i = 0;
do {
cout << i << endl;
i++;
} while (i < 5);
```
---
## **5. Functions**
```cpp
int add(int a, int b) {
return a + b;
int main() {
cout << add(3, 4) << endl; // Output: 7
return 0;
```
**Function Overloading** (Same name, different parameters):
```cpp
int add(int a, int b) { return a + b; }
float add(float a, float b) { return a + b; }
```
---
## **6. Arrays and Strings**
### **Arrays**
```cpp
int numbers[5] = {1, 2, 3, 4, 5};
cout << numbers[0] << endl; // Output: 1
```
### **Strings**
```cpp
#include <string>
string name = "Alice";
cout << name.length() << endl; // Output: 5
```
---
## **7. Pointers and References**
- **Pointer** (Stores memory address):
```cpp
int num = 10;
int* ptr = #
cout << *ptr << endl; // Output: 10
```
- **Reference** (Alias to a variable):
```cpp
int num = 10;
int& ref = num;
ref = 20;
cout << num << endl; // Output: 20
```
---
## **8. Object-Oriented Programming (OOP)**
### **Classes and Objects**
```cpp
class Dog {
public:
string name;
void bark() {
cout << name << " says Woof!" << endl;
};
int main() {
Dog dog;
dog.name = "Buddy";
dog.bark(); // Output: Buddy says Woof!
return 0;
```
### **Inheritance**
```cpp
class Animal {
public:
void eat() { cout << "Eating..." << endl; }
};
class Dog : public Animal {
public:
void bark() { cout << "Woof!" << endl; }
};
```
### **Polymorphism**
```cpp
class Animal {
public:
virtual void sound() { cout << "Animal sound" << endl; }
};
class Dog : public Animal {
public:
void sound() override { cout << "Woof!" << endl; }
};
```
---
## **9. File Handling**
```cpp
#include <fstream>
// Writing to a file
ofstream outFile("example.txt");
outFile << "Hello, File!";
outFile.close();
// Reading from a file
ifstream inFile("example.txt");
string line;
getline(inFile, line);
cout << line << endl; // Output: Hello, File!
inFile.close();
```
---
## **10. Error Handling**
```cpp
#include <stdexcept>
try {
int age = -5;
if (age < 0) throw invalid_argument("Age cannot be negative!");
} catch (const exception& e) {
cout << "Error: " << e.what() << endl;
```
---
## **11. Standard Template Library (STL)**
- **Vector (Dynamic Array)**:
```cpp
#include <vector>
vector<int> nums = {1, 2, 3};
nums.push_back(4);
```
- **Map (Key-Value Pairs)**:
```cpp
#include <map>
map<string, int> ages = {{"Alice", 25}, {"Bob", 30}};
cout << ages["Alice"] << endl; // Output: 25
```
---
## **12. Next Steps**
- **Practice**: Solve problems on [Codeforces](https://codeforces.com/),
[LeetCode](https://leetcode.com/).
- **Projects**: Build a calculator, game (e.g., Tic-Tac-Toe), or a simple database.
- **Explore Advanced Topics**:
- **Memory Management**: Smart pointers (`unique_ptr`, `shared_ptr`)
- **Multithreading**: `<thread>` library
- **Graphics**: OpenGL, SFML
- **Join Communities**: [C++ Discord](https://discord.gg/cpp), [Stack
Overflow](https://stackoverflow.com/).
---
### **Conclusion**
C++ is a powerful language widely used in performance-critical applications. This guide covers
the fundamentals, but mastering C++ requires hands-on practice and exploration of advanced
concepts. Happy coding! 🚀💻