Interesting Facts about C++

Last Updated : 24 Sep, 2025

C++ is a is a powerful, general-purpose programming language created by Bjarne Stroustrup at Bell Labs. It was first introduced as “C with Classes” in 1979 and later became C++ in 1983. Over the years, C++ has become widely used for building software, games, systems, and applications, and it offers unique features that make it stand out from other programming languages.

Here are some awesome facts about C++ that may interest you: 

1. C++ Supports Both Procedural and Object-Oriented Programming, as it allows you to write traditional C-style functions for step-by-step tasks (procedural), while also letting you create classes and objects to model real-world entities (object-oriented).

C++
#include <iostream>
using namespace std;

// Procedural part: function to calculate average marks
double calculateAverage(int totalMarks, int subjects)
{
    return (double)totalMarks / subjects;
}

// Object-Oriented part: class representing a Student
class Student
{
  public:
    string name;
    int totalMarks;

    void displayInfo()
    {
        cout << "Student: " << name << ", Total Marks: " << totalMarks << endl;
    }
};

int main()
{
    // Object-Oriented usage
    Student s1;
    s1.name = "Khushi";
    s1.totalMarks = 450;
    s1.displayInfo();

    // Procedural usage
    double average = calculateAverage(s1.totalMarks, 5); 
    cout << "Average Marks: " << average << endl;

    return 0;
}

Output
Student: Khushi, Total Marks: 450
Average Marks: 90

2. C++ allows low-level memory manipulation using pointers. They store memory addresses and let you access or modify values directly, giving more control but requiring careful use.

C++
#include <iostream>
using namespace std;

int main()
{
    int x = 10;  
    int *p = &x; 

    cout << "Value of x: " << x << endl;
    cout << "Address of x: " << p << endl;
    cout << "Value using pointer: " << *p << endl;

    *p = 20; 
    cout << "New value of x: " << x << endl;

    return 0;
}

Output
Value of x: 10
Address of x: 0x7ffc3dde8edc
Value using pointer: 10
New value of x: 20

3. C++ supports templates, which allow you to write a single piece of code that works with different data types. For example, you can create one function or class using templates, and use it for int, float, string, or any other type without rewriting the code.

C++
#include <iostream>
using namespace std;

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

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

Output
15
6

4. C++ has the Standard Template Library (STL), which provides ready-made containers and algorithms. Containers like vector, map, and set help store data easily, while algorithms which let you perform common tasks quickly, saving you time and effort.

C++
#include <algorithm> 
#include <iostream>
#include <vector>

using namespace std;

int main()
{

    // STL container
    vector<int> numbers = {5, 2, 9, 1, 7};

    // STL algorithm to sort
    sort(numbers.begin(), numbers.end());

    cout << "Sorted numbers: ";
    for (int n : numbers)
    {
        cout << n << " ";
    }
    cout << endl;

    return 0;
}

Output
Sorted numbers: 1 2 5 7 9 

5. C++ provides custom comparator where you can decide how to compare or sort things. You can write your own rules using a function or a small piece of code to control the order in which objects are arranged.

C++
#include <algorithm>
#include <iostream>
using namespace std;

// Custom comparator function
bool compare(int a, int b)
{

    // Sort in descending order
    return a > b;
}

int main()
{
    int arr[] = {3, 1, 4, 2};

    // Use custom comparator
    sort(arr, arr + 4, compare);

    for (int i = 0; i < 4; i++)
        cout << arr[i] << " ";
}

Output
4 3 2 1 

6. RAII (Resource Acquisition Is Initialization) in C++ is a programming technique where resources like memory, files, or sockets are acquired in a constructor and automatically released in the destructor. This ensures automatic cleanup when an object goes out of scope, preventing resource leaks and making code safer and easier to manage.

C++
#include <iostream>
using namespace std;

class File
{
  public:
    File()
    {
        cout << "File opened\n";
    }
    ~File()
    {
        cout << "File closed\n";
    }
};

int main()
{
    File f; 
} 

Output
File opened
File closed

7. A class can inherit from more than one class, allowing it to combine features from different classes called multiple inheritance. This is something rarely supported in other languages and gives you more flexibility in designing complex relationships.

C++
#include <iostream>
using namespace std;

class Dog
{
  public:
    void bark()
    {
        cout << "Dog barks\n";
    }
};

class Cat
{
  public:
    void meow()
    {
        cout << "Cat meows\n";
    }
};

// Derived class inheriting from both Dog and Cat
class Pet : public Dog, public Cat
{
  public:
    void play()
    {
        cout << "Pet plays\n";
    }
};

int main()
{
    Pet p;
    p.bark();
    p.meow();
    p.play();
}

Output
Dog barks
Cat meows
Pet plays

8. You can make operators like + or - work for your own types, so objects can be added, subtracted, or compared just like numbers. this is called operator overloading.

C++
#include <iostream>
using namespace std;

class Number
{
  public:
    int value;

    Number(int v) : value(v)
    {
    }

    // Overload the + operator
    Number operator+(const Number &n) const
    {
        return Number(value + n.value);
    }
};

int main()
{
    Number a(10), b(20);
    Number c = a + b; 
    cout << "Sum: " << c.value << endl;
}

Output
Sum: 30

9. You don’t have to write the type of a variable. The compiler figures out the type automatically from the value you give, making your code shorter and easier to read using the auto keyword.

C++
#include <iostream>
using namespace std;

int main()
{
    auto x = 10;
    auto y = 3.14;
    auto name = "Alice";

    cout << x << ", " << y << ", " << name << endl;
}

Output
10, 3.14, Alice


Comment