A comprehensive developer's guide to C++ programming. Deep-dive into class designs, move semantics, template metaprogramming, STL, smart pointers, multithreading, and concurrency.
unique_ptr, shared_ptr) over raw pointers, and leverage move constructors to transfer ownership without cloning resources.vector, map) and algorithms (std::sort) are highly optimized and standard-compliant.C++ extends procedural logic into class-based abstractions. Classes define blueprints, and objects represent instances.
#include <iostream>
#include <string>
class Developer {
private:
std::string name;
std::string primaryLang;
public:
// Constructor
Developer(std::string devName, std::string lang) : name(devName), primaryLang(lang) {}
// Method
void introduce() const {
std::cout << "Hi, I am " << name << ", coding in " << primaryLang << ".\n";
}
};
int main() {
Developer dev("Ajit Dev", "C++");
dev.introduce();
return 0;
}
Lifecycles are strictly managed by Constructors, Copy Constructors, Move Constructors, and Destructors.
#include <iostream>
class Vector {
private:
int* data;
int size;
public:
// Default Constructor
Vector(int s) : size(s) {
data = new int[size];
std::cout << "Allocated vector of size " << size << "\n";
}
// Destructor
~Vector() {
delete[] data;
std::cout << "Destroyed vector, memory freed\n";
}
// Copy Constructor (Deep Copy)
Vector(const Vector& other) : size(other.size) {
data = new int[size];
for (int i = 0; i < size; i++) {
data[i] = other.data[i];
}
std::cout << "Deep copied vector\n";
}
// Move Constructor (Shallow Transfer)
Vector(Vector&& other) noexcept : data(other.data), size(other.size) {
other.data = nullptr;
other.size = 0;
std::cout << "Moved vector elements\n";
}
};
Add custom meanings to standard operator tags for your user-defined classes.
#include <iostream>
class Complex {
public:
double real, imag;
Complex(double r = 0, double i = 0) : real(r), imag(i) {}
// Overloading '+' operator
Complex operator + (const Complex& obj) {
return Complex(real + obj.real, imag + obj.imag);
}
};
Polymorphism permits treating derived subclass pointers as base parent class pointers.
virtual void method() = 0;) create Abstract Classes.#include <iostream>
class Shape {
public:
virtual void draw() const = 0; // Pure virtual function
virtual ~Shape() = default;
};
class Circle : public Shape {
public:
void draw() const override {
std::cout << "Drawing Circle\n";
}
};
Templates enable writing code that works independent of specific types.
#include <iostream>
template <typename T>
T findMax(T a, T b) {
return (a > b) ? a : b;
}
int main() {
std::cout << findMax<int>(10, 20) << "\n";
std::cout << findMax<double>(5.5, 4.5) << "\n";
return 0;
}
STL provides robust standard containers, iterators, and algorithms:
std::vector (dynamic array), std::list (double linked list), std::map (red-black tree index), std::unordered_map (hash-table lookup).std::sort, std::reverse, std::binary_search.#include <iostream>
#include <vector>
#include <algorithm>
int main() {
std::vector<int> nums = {40, 10, 30, 20};
std::sort(nums.begin(), nums.end());
for (int n : nums) {
std::cout << n << " ";
}
std::cout << "\n";
return 0;
}
C++11 introduces automated memory ownership rules to replace raw new/delete:
std::unique_ptr: Unique ownership. Destroys resource when pointer leaves scope.std::shared_ptr: Reference counted sharing.std::weak_ptr: Non-owning reference pointer to prevent shared cyclic references.#include <iostream>
#include <memory>
class Resource {
public:
Resource() { std::cout << "Resource created\n"; }
~Resource() { std::cout << "Resource released\n"; }
};
int main() {
std::unique_ptr<Resource> ptr = std::make_unique<Resource>();
return 0; // Resource released automatically here
}
Separate error handling logic from standard flow by using try, throw, and catch.
#include <iostream>
#include <stdexcept>
int divide(int a, int b) {
if (b == 0) throw std::runtime_error("Division by zero!");
return a / b;
}
C++11 introduces std::thread, std::mutex (mutual exclusion locks), and standard async operations to utilize multi-core processing threads.
#include <iostream>
#include <thread>
void printMessage(std::string msg) {
std::cout << msg << "\n";
}
int main() {
std::thread t(printMessage, "Hello from concurrent execution thread!");
t.join(); // wait for execution thread to finish
return 0;
}
virtual.&) refers to objects with permanent memory address coordinates, while an rvalue reference (&&) refers to temporary objects (literals, compiler returns) whose state can be moved.