C++ Cheatsheet

👋 Hello World

#include <iostream>
using namespace std;

int main() {
    cout << "Hello, World!" << endl;
    return 0;
}

💡 Tip: Use std::cout for output. Avoid using namespace std in large projects.

📦 Variables & Data Types

#include <iostream>
using namespace std;

int main() {
    int age = 21;
    double pi = 3.14159;
    char grade = 'A';
    string name = "Maxon";

    cout << name << " is " << age << " years old." << endl;
    return 0;
}

💡 Tip: C++ supports string class unlike C.

🔀 Control Flow

#include <iostream>
using namespace std;

int main() {
    for(int i=1; i<=5; i++) {
        if(i % 2 == 0)
            cout << i << " is Even" << endl;
        else
            cout << i << " is Odd" << endl;
    }
    return 0;
}

💡 Tip: Practice with switch, while, and do-while.

🛠 Functions

#include <iostream>
using namespace std;

int add(int a, int b) {
    return a + b;
}

int main() {
    cout << "Sum: " << add(5,3) << endl;
    return 0;
}

💡 Tip: In modern C++, prefer inline or auto return type deduction.

🏛 Object-Oriented Programming

#include <iostream>
using namespace std;

class Car {
public:
    string brand;
    int speed;

    void drive() {
        cout << brand << " drives at " << speed << " km/h" << endl;
    }
};

int main() {
    Car c1;
    c1.brand = "Tesla";
    c1.speed = 120;
    c1.drive();
    return 0;
}

💡 Tip: Encapsulation, Inheritance, Polymorphism = C++ OOP pillars.

⚙️ Constructors & Destructors

#include <iostream>
using namespace std;

class Student {
public:
    string name;
    Student(string n) { name = n; }   // Constructor
    ~Student() { cout << "Destroyed" << endl; } // Destructor
};

int main() {
    Student s("Maxon");
    cout << "Student: " << s.name << endl;
    return 0;
}

💡 Tip: Use constructors for initialization, destructors for cleanup.

🧬 Inheritance

#include <iostream>
using namespace std;

class Animal {
public:
    void sound() { cout << "Some sound" << endl; }
};

class Dog : public Animal {
public:
    void sound() { cout << "Woof!" << endl; }
};

int main() {
    Dog d;
    d.sound();
    return 0;
}

💡 Tip: Use virtual for polymorphism.

📍 Pointers & References

#include <iostream>
using namespace std;

int main() {
    int x = 42;
    int *ptr = &x;
    int &ref = x;

    cout << "Value: " << *ptr << endl;
    cout << "Reference: " << ref << endl;
    return 0;
}

💡 Tip: Prefer references over pointers when possible (safer).

📚 Standard Template Library (STL)

#include <iostream>
#include <vector>
using namespace std;

int main() {
    vector<int> nums = {1,2,3,4,5};
    nums.push_back(6);

    for(int n : nums) cout << n << " ";
    return 0;
}

💡 Tip: STL = <vector>, <map>, <set>, <stack>, <queue>.

📦 Templates (Generic Programming)

#include <iostream>
using namespace std;

template <typename T>
T add(T a, T b) {
    return a + b;
}

int main() {
    cout << add(5,3) << endl;
    cout << add(2.5,1.2) << endl;
    return 0;
}

💡 Tip: Use templates for generic code (like STL containers).

📂 File Handling

#include <iostream>
#include <fstream>
using namespace std;

int main() {
    ofstream out("data.txt");
    out << "Hello File!";
    out.close();

    ifstream in("data.txt");
    string text;
    getline(in, text);
    cout << text << endl;
    return 0;
}

💡 Tip: Always close file streams. Use fstream for both read & write.

⚡ Lambda Functions

#include <iostream>
using namespace std;

int main() {
    auto square = [](int x) { return x*x; };
    cout << square(5) << endl;
    return 0;
}

💡 Tip: Lambdas are powerful for functional-style programming.

🧠 Smart Pointers

#include <iostream>
#include <memory>
using namespace std;

int main() {
    unique_ptr<int> p1 = make_unique<int>(42);
    cout << *p1 << endl;

    shared_ptr<int> p2 = make_shared<int>(99);
    cout << *p2 << endl;

    return 0;
}

💡 Tip: Prefer unique_ptr & shared_ptr over raw pointers.

Post a Comment