C++ Basics
Data Types
- Basic Data Types:
int,float,double,char,bool,void - Modifiers:
signed,unsigned,short,long - Derived Types: Arrays (
int arr[10];), Pointers (int* ptr;), References (int& ref = var;) - User-defined Types:
struct,enum,class
cpp
#include <iostream>
using namespace std;
int main() {
int a = 10; // Integer
float b = 3.14; // Floating-point
char c = 'A'; // Character
bool d = true; // Boolean
unsigned int e = 50; // Unsigned integer
cout << "Int: " << a << "\nFloat: " << b << "\nChar: " << c
<< "\nBool: " << d << "\nUnsigned Int: " << e << endl;
return 0;
}Data Structures
Commonly used data structures in C++:
- Arrays:
int arr[5] = {1, 2, 3, 4, 5}; - Vectors: Dynamic arrays in the STL (
std::vector<int> vec;) - Linked Lists: Custom implementation or use
std::listfrom the STL. - Stack and Queue:
std::stackandstd::queue - Map and Set: Associative containers like
std::map,std::unordered_map, andstd::set.
cpp
#include <iostream>
#include <vector>
#include <map>
using namespace std;
int main() {
// Array
int arr[] = {1, 2, 3, 4, 5};
cout << "Array: ";
for (int i : arr) cout << i << " ";
// Vector
vector<int> vec = {10, 20, 30};
vec.push_back(40); // Add an element
cout << "\nVector: ";
for (int v : vec) cout << v << " ";
// Map
map<string, int> age;
age["Alice"] = 25;
age["Bob"] = 30;
cout << "\nMap: ";
for (auto &pair : age) cout << pair.first << ": " << pair.second << " ";
return 0;
}Statements
- Control Statements:
- Conditional:
if,else if,else,switch - Loops:
for,while,do-while - Jump:
break,continue,return,goto
- Conditional:
- Expression Statements: Assignment (
=), Arithmetic (+,-, etc.), Logical (&&,||,!)
cpp
#include <iostream>
using namespace std;
int main() {
int x = 15;
// If-else statement
if (x > 10) cout << "x is greater than 10\n";
else cout << "x is 10 or less\n";
// Switch statement
switch (x) {
case 10: cout << "x is 10\n"; break;
case 15: cout << "x is 15\n"; break;
default: cout << "x is unknown\n";
}
return 0;
}Loops
cpp
// For Loop
for (int i = 0; i < 10; ++i) {
std::cout << i << " ";
}
// While Loop
int i = 0;
while (i < 10) {
std::cout << i++ << " ";
}
// Do-While Loop
int j = 0;
do {
std::cout << j++ << " ";
} while (j < 10);Functions
- Function Overloading: Functions with the same name but different parameters.
- Inline Functions:
inline int square(int x) { return x * x; } - Lambda Expressions:
auto lambda = [](int x) { return x * x; };
cpp
// Declaration and Definition
int add(int a, int b) {
return a + b;
}
// Function Call
int result = add(5, 3);Classes
- Concepts:
- Encapsulation
- Inheritance
- Polymorphism
- Abstraction
cpp
class Rectangle {
public:
int length, width;
Rectangle(int l, int w) : length(l), width(w) {} // Constructor
int area() { return length * width; }
};
// Object Instantiation
Rectangle rect(5, 4);
std::cout << "Area: " << rect.area();Imports
C++ relies on header files for importing functionality:
cpp
#include <iostream> // Input-output operations
#include <vector> // Dynamic array
#include <map> // Key-value pairs
#include <string> // String operations
#include <algorithm> // Standard algorithms (e.g., sort, find)Common Libraries
- Standard Template Library (STL):
- Containers:
vector,list,map,set, etc. - Algorithms:
sort,search,transform - Iterators: Traverse through containers
- Containers:
- Math Library:
<cmath>(e.g.,sqrt,pow) - File I/O:
<fstream>for reading and writing files - Threads:
<thread>for multithreading - Chrono:
<chrono>for time-related functions - Random:
<random>for generating random numbers
Example Code
cpp
#include <iostream>
#include <vector>
#include <algorithm>
int main() {
std::vector<int> nums = {5, 3, 8, 1, 2};
std::sort(nums.begin(), nums.end()); // Sort the vector
for (int num : nums) {
std::cout << num << " ";
}
return 0;
}

