You’re the Expert!

cpp

Cheatsheets

Print "Hello, World!" to the Console
#include <iostream>

int main() {
    std::cout << "Hello, World!" << std::endl;
    return 0;
}
copy to clipboard
Declare and Use Variables
#include <iostream>

int main() {
    std::string name = "Santosh";
    int age = 25;
    std::cout << "Name: " << name << std::endl;
    std::cout << "Age: " << age << std::endl;
    return 0;
}
copy to clipboard
Perform Arithmetic Operations
#include <iostream>

int main() {
    int a = 10, b = 5;
    std::cout << "Addition: " << a + b << std::endl;
    std::cout << "Subtraction: " << a - b << std::endl;
    std::cout << "Multiplication: " << a * b << std::endl;
    std::cout << "Division: " << a / b << std::endl;
    std::cout << "Modulus: " << a % b << std::endl;
    return 0;
}
copy to clipboard
Conditional Statements (if-else)
#include <iostream>

int main() {
    int age = 20;
    if (age >= 18) {
        std::cout << "Eligible for voting" << std::endl;
    } else {
        std::cout << "Not eligible for voting" << std::endl;
    }
    return 0;
}
copy to clipboard
Loops (for, while)
#include <iostream>

int main() {
    // For loop example
    for (int i = 1; i <= 5; i++) {
        std::cout << "Santosh is learning C++ - Iteration: " << i << std::endl;
    }

    // While loop example
    int count = 1;
    while (count <= 3) {
        std::cout << "Dhruv is exploring C++ - Attempt: " << count << std::endl;
        count++;
    }
    return 0;
}
copy to clipboard

Functions in C++
#include <iostream>

void greet(std::string name) {
    std::cout << "Hello, " << name << "! Welcome to Pynfinity." << std::endl;
}

int main() {
    greet("Santosh");
    greet("Kumar");
    return 0;
}
copy to clipboard
Arrays in C++
#include <iostream>

int main() {
    std::string names[4] = {"Santosh", "Kumar", "Dhruv", "Pynfinity"};
    for (int i = 0; i < 4; i++) {
        std::cout << "Name: " << names[i] << std::endl;
    }
    return 0;
}
copy to clipboard
Pointers in C++
#include <iostream>

int main() {
    int num = 10;
    int *ptr = &num;

    std::cout << "Value of num: " << num << std::endl;
    std::cout << "Address of num: " << &num << std::endl;
    std::cout << "Value stored at ptr: " << *ptr << std::endl;

    return 0;
}
copy to clipboard
Strings in C++
#include <iostream>
#include <string>

int main() {
    std::string str = "Pynfinity Channel";

    std::cout << "String: " << str << std::endl;
    std::cout << "Length of string: " << str.length() << std::endl;

    // Copy string
    std::string copy = str;
    std::cout << "Copied string: " << copy << std::endl;

    return 0;
}
copy to clipboard

Classes and Objects in C++
#include <iostream>

class Person {
public:
    std::string name;
    int age;

    // Constructor
    Person(std::string n, int a) {
        name = n;
        age = a;
    }

    void display() {
        std::cout << "Name: " << name << ", Age: " << age << std::endl;
    }
};

int main() {
    Person p1("Santosh", 25);
    p1.display();

    Person p2("Kumar", 30);
    p2.display();

    return 0;
}
copy to clipboard
File Handling in C++
#include <iostream>
#include <fstream>

int main() {
    std::ofstream outFile("example.txt");

    if (outFile.is_open()) {
        outFile << "This is a test file created by Santosh.\n";
        outFile.close();
    } else {
        std::cout << "Unable to open file" << std::endl;
    }

    // Reading from the file
    std::ifstream inFile("example.txt");
    std::string line;
    if (inFile.is_open()) {
        while (getline(inFile, line)) {
            std::cout << line << std::endl;
        }
        inFile.close();
    } else {
        std::cout << "Unable to open file" << std::endl;
    }

    return 0;
}
copy to clipboard