GK SOLUTIONS
AI • IoT • Arduino • Projects & Tutorials
DEFEAT THE FEAR
← Back to Tutorials

⚙️ C++ Programming Tutorial

Master object-oriented programming and modern C++ - from fundamental syntax to the Standard Template Library.

📑 Table of Contents

  1. C++ Basics
  2. Input/Output
  3. Classes & Objects
  4. Inheritance
  5. Polymorphism
  6. Templates
  7. STL Containers
  8. Memory Management
  9. Exception Handling
  10. Projects

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 
#include 

// Vector (dynamic array)
vector nums = {1, 2, 3};
nums.push_back(4);
for (int num : nums) {
    cout << num << " ";
}

// List (linked list)
list names;
names.push_back("Alice");
names.push_front("Bob");

// Map (key-value pairs)
map ages;
ages["John"] = 25;
ages["Jane"] = 28;
cout << ages["John"] << endl;

// Set (unique elements)
set unique_nums = {1, 2, 3, 2, 1};
// Contains: 1, 2, 3

// Iterators
for (vector::iterator it = nums.begin(); it != nums.end(); ++it) {
    cout << *it << " ";
}

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