Category: Example

https://cdn3d.iconscout.com/3d/premium/thumb/business-idea-3d-illustration-download-in-png-blend-fbx-gltf-file-formats–planning-creative-innovative-strategy-pack-illustrations-3010206.png?f=webp

  • Multithreading

    An example that demonstrates how to create and manage threads.

    cppCopy code#include <iostream>
    #include <thread>
    
    void printMessage() {
    
    std::cout &lt;&lt; "Hello from thread!" &lt;&lt; std::endl;
    } int main() {
    std::thread t(printMessage);
    t.join(); // Wait for the thread to finish
    return 0;
    }
  • File I/O

    This example demonstrates how to read from and write to files.

    cppCopy code#include <iostream>
    #include <fstream>
    
    int main() {
    
    std::ofstream outFile("example.txt");
    outFile &lt;&lt; "Hello, file!" &lt;&lt; std::endl;
    outFile.close();
    std::ifstream inFile("example.txt");
    std::string line;
    while (std::getline(inFile, line)) {
        std::cout &lt;&lt; line &lt;&lt; std::endl; // Output: Hello, file!
    }
    inFile.close();
    return 0;
    }
  • Operator Overloading

    This example shows how to overload the + operator for a custom class.

    cppCopy code#include <iostream>
    
    class Vector {
    public:
    
    Vector(int x, int y) : x(x), y(y) {}
    // Overloading the + operator
    Vector operator+(const Vector&amp; other) {
        return Vector(x + other.x, y + other.y);
    }
    void display() {
        std::cout &lt;&lt; "Vector(" &lt;&lt; x &lt;&lt; ", " &lt;&lt; y &lt;&lt; ")" &lt;&lt; std::endl;
    }
    private:
    int x, y;
    }; int main() {
    Vector v1(1, 2);
    Vector v2(3, 4);
    Vector v3 = v1 + v2; // Calls operator+
    v3.display(); // Output: Vector(4, 6)
    return 0;
    }
  • Exception Handling

    An example demonstrating how to handle exceptions in C++.

    cppCopy code#include <iostream>
    #include <stdexcept>
    
    double divide(int a, int b) {
    
    if (b == 0) {
        throw std::invalid_argument("Division by zero!");
    }
    return static_cast&lt;double&gt;(a) / b;
    } int main() {
    try {
        std::cout &lt;&lt; divide(10, 2) &lt;&lt; std::endl; // Output: 5
        std::cout &lt;&lt; divide(10, 0) &lt;&lt; std::endl; // This will throw an exception
    } catch (const std::invalid_argument&amp; e) {
        std::cerr &lt;&lt; "Error: " &lt;&lt; e.what() &lt;&lt; std::endl;
    }
    return 0;
    }
  • Smart Pointers

    Using smart pointers to manage memory automatically.

    cppCopy code#include <iostream>
    #include <memory>
    
    class MyClass {
    public:
    
    MyClass() { std::cout &lt;&lt; "MyClass constructor" &lt;&lt; std::endl; }
    ~MyClass() { std::cout &lt;&lt; "MyClass destructor" &lt;&lt; std::endl; }
    }; int main() {
    std::unique_ptr&lt;MyClass&gt; ptr = std::make_unique&lt;MyClass&gt;();
    // No need to manually delete; the destructor will be called automatically
    return 0;
    }
  • Templates

    This example illustrates how to use templates to create a function that can work with any data type.

    cppCopy code#include <iostream>
    
    template <typename T>
    T max(T a, T b) {
    
    return (a &gt; b) ? a : b;
    } int main() {
    std::cout &lt;&lt; max(10, 20) &lt;&lt; std::endl;       // Output: 20
    std::cout &lt;&lt; max(10.5, 7.5) &lt;&lt; std::endl;   // Output: 10.5
    return 0;
    }
  • Function Overloading

    Demonstrates how to create multiple functions with the same name but different parameters.

    cppCopy code#include <iostream>
    
    void print(int i) {
    
    std::cout &lt;&lt; "Integer: " &lt;&lt; i &lt;&lt; std::endl;
    } void print(double d) {
    std::cout &lt;&lt; "Double: " &lt;&lt; d &lt;&lt; std::endl;
    } int main() {
    print(5);      // Calls print(int)
    print(5.5);    // Calls print(double)
    return 0;
    }
  • Inheritance

    This example shows how to use inheritance to create a derived class.

    cppCopy code#include <iostream>
    
    class Animal {
    public:
    
    void speak() {
        std::cout &lt;&lt; "Animal speaks!" &lt;&lt; std::endl;
    }
    }; class Cat : public Animal { public:
    void speak() {
        std::cout &lt;&lt; "Cat meows!" &lt;&lt; std::endl;
    }
    }; int main() {
    Cat myCat;
    myCat.speak(); // Output: Cat meows!
    return 0;
    }
  • Classes and Objects

    This example demonstrates how to define a class and create objects in C++.

    cppCopy code#include <iostream>
    #include <string>
    
    class Dog {
    public:
    
    Dog(std::string name) : name(name) {}
    
    void bark() {
        std::cout &lt;&lt; name &lt;&lt; " says Woof!" &lt;&lt; std::endl;
    }
    private:
    std::string name;
    }; int main() {
    Dog myDog("Buddy");
    myDog.bark();
    return 0;
    }
  • Basic Hello World Program

    A simple program that prints “Hello, World!” to the console.

    cppCopy code#include <iostream>
    
    int main() {
    
    std::cout &lt;&lt; "Hello, World!" &lt;&lt; std::endl;
    return 0;
    }