⚙️ C++ Programming Tutorial
Master object-oriented programming and modern C++ - from fundamental syntax to the Standard Template Library.
01 What is C++?
Explore the power of C++ and its role in modern software development.
02 Setup & First Program
Configure your compiler and write your initial C++ application.
03 Variables & Data Types
Learn about C++ fundamental types and modern 'auto' deduction.
04 Classes & Objects
Entry into Object-Oriented Programming (OOP) with classes.
05 Inheritance
Extend existing code and create hierarchical relationships.
06 Polymorphism
Learn about virtual functions and abstract base classes.
07 Templates
Write generic code that works with any data type.
08 STL & Memory
Introduction to standard containers and smart pointers.
📑 Table of Contents
C++ Basics
C++ is an extension of C that adds object-oriented programming capabilities. It's used in game engines (Unreal, Godot), high-performance applications, and systems software.
Your First C++ Program:
#include
using namespace std;
int main() {
cout << "Hello, C++!" << endl;
return 0;
}
Variables & Data Types:
// Auto keyword
auto age = 25; // Compiler deduces int
auto name = "John"; // const char*
auto pi = 3.14; // double
// Type casting
int x = 10;
double y = (double)x; // C-style cast
double z = static_cast(x); // Modern cast
// Constants
const int MAX = 100;
constexpr int SIZE = 50;
Input/Output
#include
#include
using namespace std;
int main() {
// Output
cout << "Hello, World!" << endl;
cout << "Number: " << 42 << endl;
// Input
int age;
cout << "Enter your age: ";
cin >> age;
// String input
string name;
cout << "Enter your name: ";
cin >> name; // Input until space
// Input entire line
getline(cin, name); // Input entire line including spaces
// Formatted output
cout << "Age: " << age << ", Name: " << name << endl;
return 0;
}
Classes & Objects
class Car {
private:
string brand;
int year;
public:
// Constructor
Car(string b, int y) {
brand = b;
year = y;
}
// Member functions
void display() {
cout << brand << " (" << year << ")" << endl;
}
// Getter
string getBrand() {
return brand;
}
// Setter
void setBrand(string b) {
brand = b;
}
};
int main() {
Car car1("Toyota", 2020);
car1.display(); // Output: Toyota (2020)
return 0;
}
Inheritance
// Base class
class Vehicle {
protected:
string color;
public:
Vehicle(string c) : color(c) {}
virtual void describe() {
cout << "Vehicle color: " << color << endl;
}
};
// Derived class
class Car : public Vehicle {
private:
int doors;
public:
Car(string c, int d) : Vehicle(c), doors(d) {}
void describe() override {
cout << "Car with " << doors << " doors, color: " << color << endl;
}
};
int main() {
Car car("red", 4);
car.describe(); // Output: Car with 4 doors, color: red
return 0;
}
Polymorphism
// Abstract base class
class Animal {
public:
virtual void makeSound() = 0; // Pure virtual
virtual ~Animal() {}
};
class Dog : public Animal {
public:
void makeSound() override {
cout << "Woof!" << endl;
}
};
class Cat : public Animal {
public:
void makeSound() override {
cout << "Meow!" << endl;
}
};
int main() {
Dog dog;
Cat cat;
dog.makeSound(); // Woof!
cat.makeSound(); // Meow!
// Using pointers
Animal *animals[2] = {&dog, &cat};
for (int i = 0; i < 2; i++) {
animals[i]->makeSound();
}
return 0;
}
Templates
// Function template
template
T add(T a, T b) {
return a + b;
}
int main() {
cout << add(5, 3) << endl; // 8 (int)
cout << add(3.5, 2.1) << endl; // 5.6 (double)
return 0;
}
// Class template
template
class Box {
private:
T value;
public:
Box(T v) : value(v) {}
T getValue() {
return value;
}
void setValue(T v) {
value = v;
}
};
// Usage
Box intBox(42);
Box strBox("Hello");
STL Containers
#include
#include
#include
Memory Management
// Dynamic memory allocation
int *ptr = new int; // Allocate single int
*ptr = 42;
delete ptr; // Deallocate
// Dynamic array
int *arr = new int[10];
delete[] arr; // Note: delete[] for arrays
// Smart pointers (modern C++)
#include
unique_ptr ptr1(new int(42));
// Auto deleted when ptr1 goes out of scope
shared_ptr ptr2 = make_shared(42);
shared_ptr ptr3 = ptr2;
// Deleted when all copies go out of scope
Exception Handling
int main() {
try {
int age;
cout << "Enter your age: ";
cin >> age;
if (age < 0 || age > 150) {
throw invalid_argument("Age must be between 0 and 150");
}
cout << "Your age is: " << age << endl;
}
catch (invalid_argument &e) {
cout << "Error: " << e.what() << endl;
}
catch (exception &e) {
cout << "Generic error: " << e.what() << endl;
}
catch (...) {
cout << "Unknown error" << endl;
}
return 0;
}
Projects & Practice
Beginner Projects:
- Student Management System (OOP)
- Bank Account Simulator
- Simple Game with Classes
- Hospital Management System
- Library Management System
Intermediate Projects:
- Data Structure Implementations
- File Management System
- 2D Game with Graphics
- Web Server (Basic)
- Chat Application
Advanced Projects:
- Game Engine Component
- Operating System Simulator
- Compiler/Interpreter
- Real-time Graphics Engine
- Distributed System Project