C++ Notes - Beginner to Advanced
Full C++ Notes with Explanations
C++ Notes - Beginner to Advanced (Handwritten Style)
1. Introduction to C++
C++ is a powerful, high-performance programming language created by Bjarne Stroustrup in 1985.
It is an extension of the C language with features that support object-oriented programming (OOP).
C++ is used in systems programming, game development, real-time simulations, and more.
2. Structure of a C++ Program
Every C++ program must include a main() function. The basic structure is:
#include <iostream> // Preprocessor directive for input-output
using namespace std; // Avoids writing std:: repeatedly
int main() {
cout << "Hello, World!"; // Output statement
return 0; // Indicates successful execution
}
3. Data Types
C++ supports various data types:
- int: Stores integers (e.g., 10, -5)
- float: Stores decimal values (e.g., 3.14)
- double: More precise decimal values
- char: Stores single character (e.g., 'A')
- bool: Stores true or false
- string: Represents a sequence of characters (needs #include <string>)
Page 1
C++ Notes - Beginner to Advanced
4. Variables and Constants
Variables are containers for storing data values.
Example:
int age = 25;
Constants are declared using 'const' keyword and cannot be changed.
Example:
const float PI = 3.14;
5. Operators
- Arithmetic: +, -, *, /, %
- Relational: ==, !=, >, <, >=, <=
- Logical: && (AND), || (OR), ! (NOT)
- Assignment: =, +=, -=, *=
6. Control Structures
- if, if-else: Used to execute code blocks conditionally.
Example:
if (x > 0) {
cout << "Positive";
} else {
cout << "Non-positive";
}
- Loops:
for (int i = 0; i < 5; i++) {
cout << i;
}
while (condition) {
// loop body
}
Page 2
C++ Notes - Beginner to Advanced
do {
// body
} while (condition);
7. Functions
Functions allow code reuse.
Example:
int sum(int a, int b) {
return a + b;
}
int main() {
cout << sum(3, 4);
return 0;
}
8. Arrays and Strings
Arrays store multiple values of the same type.
int arr[3] = {1, 2, 3};
Strings are sequences of characters.
string name = "Alice";
9. Object-Oriented Programming (OOP)
C++ supports:
- Encapsulation: Wrapping data and code
- Inheritance: Deriving new classes from existing ones
- Polymorphism: Same function behaving differently
- Abstraction: Hiding internal details
Page 3
C++ Notes - Beginner to Advanced
Example:
class Person {
public:
string name;
void greet() {
cout << "Hello " << name;
}
};
10. Pointers and References
Pointers hold the memory address of variables.
int a = 5;
int* p = &a;
References are aliases:
int &ref = a;
11. File Handling
#include <fstream>
ofstream file("data.txt");
file << "Writing to file";
ifstream file("data.txt");
string content;
file >> content;
12. Templates
Used to write generic functions or classes.
template <typename T>
T max(T a, T b) {
Page 4
C++ Notes - Beginner to Advanced
return (a > b) ? a : b;
}
13. Exception Handling
Catches errors during runtime:
try {
// risky code
} catch (exception& e) {
cout << e.what();
}
14. Standard Template Library (STL)
STL includes:
- Containers: vector, list, map, set
- Algorithms: sort, reverse
- Iterators: Access elements in containers
vector<int> v = {1, 2, 3};
sort(v.begin(), v.end());
15. Advanced Topics
- Lambda Expressions: auto f = [](int x){ return x+1; };
- Smart Pointers: unique_ptr, shared_ptr
- Multithreading: <thread> library
- Move Semantics: Optimizes resource transfer
These notes provide a solid foundation for mastering C++ step-by-step with real understanding.
Page 5