# Detailed Notes on C++
Below is a comprehensive set of notes on C++ programming. At the end, I'll provide instructions
on how you can convert these notes to a PDF version.
## Table of Contents
1. Introduction to C++
2. Basic Syntax and Structure
3. Data Types and Variables
4. Operators
5. Control Structures
6. Functions
7. Arrays and Strings
8. Pointers
9. Object-Oriented Programming
10. File Handling
11. Standard Template Library (STL)
---
## 1. Introduction to C++
C++ is a general-purpose programming language created by Bjarne Stroustrup as an extension
of C.
- Supports both procedural and object-oriented programming
- Used in systems software, game development, embedded systems, etc.
- Key features: portability, speed, memory management, rich library support
## 2. Basic Syntax and Structure
```cpp
#include <iostream> // Header file for I/O
using namespace std; // Use standard namespace
int main() { // Main function
cout << "Hello World!"; // Output statement
return 0; // Return success status
}
```
- `#include` directives add necessary header files
- `main()` is the entry point of every C++ program
- Statements end with semicolons (`;`)
- `cout` is used for output, `cin` for input
## 3. Data Types and Variables
### Fundamental Data Types:
- `int`: Integer (4 bytes)
- `float`: Single-precision floating point (4 bytes)
- `double`: Double-precision floating point (8 bytes)
- `char`: Character (1 byte)
- `bool`: Boolean (true/false)
### Variable Declaration:
```cpp
int age = 25;
float price = 19.99;
char grade = 'A';
bool isValid = true;
```
## 4. Operators
### Arithmetic Operators:
`+`, `-`, `*`, `/`, `%` (modulus)
### Relational Operators:
`==`, `!=`, `>`, `<`, `>=`, `<=`
### Logical Operators:
`&&` (AND), `||` (OR), `!` (NOT)
### Assignment Operators:
`=`, `+=`, `-=`, `*=`, `/=`, `%=`
## 5. Control Structures
### If-else:
```cpp
if (condition) {
// code
} else if (another_condition) {
// code
} else {
// code
}
```
### Loops:
```cpp
// for loop
for (int i = 0; i < 10; i++) {
// code
}
// while loop
while (condition) {
// code
}
// do-while loop
do {
// code
} while (condition);
```
## 6. Functions
```cpp
// Function declaration
return_type function_name(parameters);
// Function definition
int add(int a, int b) {
return a + b;
}
// Function call
int result = add(5, 3);
```
## 7. Arrays and Strings
### Arrays:
```cpp
int numbers[5] = {1, 2, 3, 4, 5};
```
### Strings:
```cpp
#include <string>
string greeting = "Hello";
```
## 8. Pointers
```cpp
int var = 20;
int *ptr = &var; // ptr holds address of var
cout << *ptr; // Dereferencing (outputs 20)
```
## 9. Object-Oriented Programming
### Class and Object:
```cpp
class Car {
public: // Access specifier
string brand; // Attribute
void honk() { // Method
cout << "Beep!";
}
};
// Create object
Car myCar;
myCar.brand = "Toyota";
myCar.honk();
```
### Key OOP Concepts:
- Encapsulation
- Inheritance
- Polymorphism
- Abstraction
## 10. File Handling
```cpp
#include <fstream>
// Writing to file
ofstream outFile("example.txt");
outFile << "Writing to file";
// Reading from file
ifstream inFile("example.txt");
string content;
getline(inFile, content);
```
## 11. Standard Template Library (STL)
### Common Containers:
- `vector`: Dynamic array
- `list`: Doubly-linked list
- `map`: Key-value pairs
- `set`: Unique elements collection
### Example with vector:
```cpp
#include <vector>
vector<int> nums = {1, 2, 3};
nums.push_back(4); // Add element