C++

#6
TIOBE#2
PYPL#4
GitHub#9
RedMonk#7
IEEESpectrum#4
JetBrains#8
programming languagesystems programmingobject-orientedhigh performancegame development

Programming Language

C++ (C Plus Plus)

Overview

C++ is an object-oriented programming language created by extending the C language. It maintains the high performance of C while adding features such as object-oriented programming, templates, and exception handling, making it widely used from systems programming to application development.

Details

C++ was developed by Bjarne Stroustrup in 1985, evolving from "C with Classes". It allows detailed control of memory management and plays a particularly important role in fields requiring high performance.

Modern C++ (C++11 and later) has added features such as auto type deduction, lambda expressions, smart pointers, move semantics, and range-based for loops, enabling safer and more expressive code. New features continue to be added with C++14, C++17, and C++20, adapting to modern development needs.

Code Examples

Hello World

#include <iostream>

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

Classes and Objects

#include <iostream>
#include <string>

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

public:
    // Constructor
    Person(const std::string& n, int a) : name(n), age(a) {}
    
    // Getters
    const std::string& getName() const { return name; }
    int getAge() const { return age; }
    
    // Method
    void introduce() const {
        std::cout << "Hello, I'm " << name << ". " 
                  << "I'm " << age << " years old." << std::endl;
    }
};

int main() {
    Person person("John", 25);
    person.introduce();
    return 0;
}

Templates

#include <iostream>
#include <vector>

// Function template
template<typename T>
T findMax(const std::vector<T>& vec) {
    if (vec.empty()) {
        throw std::invalid_argument("Empty vector");
    }
    
    T max = vec[0];
    for (const T& element : vec) {
        if (element > max) {
            max = element;
        }
    }
    return max;
}

// Class template
template<typename T>
class Stack {
private:
    std::vector<T> elements;

public:
    void push(const T& element) {
        elements.push_back(element);
    }
    
    T pop() {
        if (elements.empty()) {
            throw std::runtime_error("Stack is empty");
        }
        T top = elements.back();
        elements.pop_back();
        return top;
    }
    
    bool empty() const {
        return elements.empty();
    }
};

int main() {
    std::vector<int> numbers = {1, 5, 3, 9, 2};
    std::cout << "Max value: " << findMax(numbers) << std::endl;
    
    Stack<int> stack;
    stack.push(10);
    stack.push(20);
    std::cout << "Popped: " << stack.pop() << std::endl;
    
    return 0;
}

Modern C++ Features

#include <iostream>
#include <vector>
#include <memory>
#include <algorithm>

class Resource {
private:
    std::string data;
    
public:
    Resource(const std::string& d) : data(d) {
        std::cout << "Resource created: " << data << std::endl;
    }
    
    ~Resource() {
        std::cout << "Resource destroyed: " << data << std::endl;
    }
    
    const std::string& getData() const { return data; }
};

int main() {
    // Auto type deduction
    auto number = 42;
    auto text = std::string("Hello");
    
    // Smart pointers
    auto resource = std::make_unique<Resource>("data");
    std::cout << "Resource data: " << resource->getData() << std::endl;
    
    // Range-based for loop
    std::vector<int> numbers = {1, 2, 3, 4, 5};
    for (const auto& num : numbers) {
        std::cout << num << " ";
    }
    std::cout << std::endl;
    
    // Lambda expressions
    auto isEven = [](int n) { return n % 2 == 0; };
    auto evenCount = std::count_if(numbers.begin(), numbers.end(), isEven);
    std::cout << "Even count: " << evenCount << std::endl;
    
    return 0;
}

STL Containers and Algorithms

#include <iostream>
#include <vector>
#include <map>
#include <algorithm>
#include <numeric>

int main() {
    // vector
    std::vector<int> numbers = {5, 2, 8, 1, 9};
    
    // Sort
    std::sort(numbers.begin(), numbers.end());
    
    // Double each element
    std::transform(numbers.begin(), numbers.end(), numbers.begin(),
                   [](int n) { return n * 2; });
    
    // Calculate sum
    int sum = std::accumulate(numbers.begin(), numbers.end(), 0);
    std::cout << "Sum: " << sum << std::endl;
    
    // map
    std::map<std::string, int> scores;
    scores["Alice"] = 85;
    scores["Bob"] = 92;
    scores["Charlie"] = 78;
    
    for (const auto& [name, score] : scores) {
        std::cout << name << ": " << score << std::endl;
    }
    
    return 0;
}

Memory Management

#include <iostream>
#include <memory>

class Data {
public:
    int value;
    Data(int v) : value(v) {
        std::cout << "Data constructed: " << value << std::endl;
    }
    ~Data() {
        std::cout << "Data destructed: " << value << std::endl;
    }
};

int main() {
    // Raw pointer (not recommended)
    Data* rawPtr = new Data(10);
    delete rawPtr; // Manual deletion required
    
    // Smart pointers (recommended)
    {
        auto uniquePtr = std::make_unique<Data>(20);
        auto sharedPtr = std::make_shared<Data>(30);
        auto anotherShared = sharedPtr; // Reference count: 2
        
        std::cout << "Reference count: " << sharedPtr.use_count() << std::endl;
    } // Automatically deleted when scope ends
    
    return 0;
}

Advantages and Disadvantages

Advantages

  • High Performance: Low-level memory management and CPU efficiency optimization
  • Rich Features: Powerful features like OOP, templates, STL
  • Portability: Available on many platforms
  • Large Ecosystem: Rich libraries and frameworks
  • Industry Standard: Standard language for systems programming and game development
  • Modern Features: Improved development efficiency with C++11+ features

Disadvantages

  • High Learning Curve: Understanding memory management and complex language features required
  • Compilation Time: Long compilation times for large projects
  • Memory Safety: Risk of bugs from manual memory management
  • Complexity: Language complexity due to many features
  • Development Speed: Longer development time compared to higher-level languages

Key Links

Ranking Information

  • Overall Ranking: 6th
  • TIOBE Index: 2nd
  • PYPL PopularitY: 4th
  • GitHub Usage: 9th
  • RedMonk Language Ranking: 7th
  • IEEE Spectrum: 4th
  • JetBrains Developer Survey: 8th

C++ is a powerful programming language that plays an important role in systems development, game development, and embedded systems where high performance is required.