Here’s a brief overview of C++, one of the most powerful and widely used programming languages, especially in areas like game development, system programming, and high-performance applications:

Here’s a brief overview of C++, one of the most powerful and widely used programming languages, especially in areas like game development, system programming, and high-performance applications:

Overview of C++

  • History: C++ was developed by Bjarne Stroustrup starting in 1979 at Bell Labs. It was designed as an extension of the C programming language, adding object-oriented, generic, and functional features.
  • Key Features:
    • Object-Oriented Programming (OOP): Supports encapsulation, inheritance, and polymorphism.
    • Templates: Allows for generic programming, which is crucial for creating reusable code.
    • Standard Template Library (STL): Provides a rich set of algorithms, containers, and iterators.
    • Memory Management: Direct control over memory with new and delete, though modern C++ encourages smart pointers for safety.
    • Performance: Known for its efficiency, close to the hardware, making it ideal for performance-critical applications.
  • Syntax and Structure:
    • C++ retains C’s syntax but adds features like classes, templates, and exceptions.
    • Example of a simple C++ program:
      cpp#include <iostream> int main() { std::cout << "Hello, World!" << std::endl; return 0; }
  • Modern C++ (C++11, C++14, C++17, C++20):
    • Introduced many features like auto, lambda expressions, move semantics, and more expressive syntax for containers and algorithms.
    • Example of using auto and a lambda:
      cpp#include <vector> #include <algorithm> int main() { std::vector<int> numbers = {1, 2, 3, 4, 5}; auto result = std::find_if(numbers.begin(), numbers.end(), [](int x) { return x > 3; }); if (result != numbers.end()) { std::cout << "First number greater than 3: " << *result << std::endl; } return 0; }
  • Use Cases:
    • Game Development: Used in engines like Unreal Engine.
    • System Software: Operating systems, device drivers.
    • High-Performance Computing: Scientific simulations, financial systems.
    • Embedded Systems: Due to its efficiency and control over hardware.
  • Learning C++:
    • Start with basic C programming concepts.
    • Learn object-oriented programming principles.
    • Dive into templates and the STL.
    • Understand modern C++ features for safer, more expressive code.
  • Challenges:
    • Complexity: C++ is vast with many features, which can be overwhelming for beginners.
    • Memory Management: Manual memory handling can lead to bugs if not managed correctly.

If you have any specific questions about C++ or need help with a particular aspect of the language, feel free to ask!

C++ 純粋仮想関数

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

struct Character {
    virtual void action() = 0;
};

struct Warrior : public Character {
    void action() { cout << "Warrior attacks with a sword!" << endl; }
};

struct Mage : public Character {
    void action() { cout << "Mage casts a fireball!" << endl; }
};

int main() {
    Warrior w;
    w.action();
    Mage m;
    m.action();

    vector<Character*> characters = { &w, &m };
    for (auto c : characters) {
        c->action();
    }
}

C++ デストラクタ

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

struct Person {
	string name;
	Person(const string& newName) : name(newName) {}
	~Person() {
		cout << name << "は解体された\n";
	}
};

int main() {
	Person a1("Taro");
	Person* pA2 = new Person("Jiro");
	Person* pA3 = new Person("Saburo");
	auto pA4 = make_shared<Person>("Shiro");
	cout << a1.name << endl;
	cout << pA2->name << endl;
	cout << pA3->name << endl;
	cout << pA4->name << endl;

	delete pA2;
}

C++ search

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

int main() {
    vector<int> v{ 2,3,5,1,4 };
    auto begin = v.cbegin();
    auto end = v.cend();

    int target = 3;
    auto pos = find(begin, end, target);
    if (pos == end)
        cout << "見つからない\n";
    else
        cout << "見つかった: " << *pos << endl; // 出力値: 見つかった: 3

    target = 6; // Correcting typo from 'traget' to 'target'
    pos = find(begin, end, target);
    if (pos == end)
        cout << "見つからない\n"; // 出力値: 見つからない
    else
        cout << "見つかった: " << *pos << endl;

    return 0;
}

C++ 反復子

#include <iostream>
#include <vector>
#include <list>
#include <numeric>

template <typename T>
int total(T first, T last) {
    int sum = 0;
    for (T p = first; p != last; ++p) sum += *p;
    return sum;
}

int main() {
    std::vector<int> v{ 1, 2, 3, 4, 5 };
    std::cout << total(v.begin(), v.end()) << std::endl;
    std::cout << std::accumulate(v.begin(), v.end(), 0) << std::endl;

    int a[] = { 1, 2, 3, 4, 5 };
    std::cout << total(std::begin(a), std::end(a)) << std::endl;
    std::cout << std::accumulate(std::begin(a), std::end(a), 0) << std::endl;

    std::list<int> li{ 1, 2, 3, 4, 5 };
    std::cout << total(li.begin(), li.end()) << std::endl;
    std::cout << std::accumulate(li.begin(), li.end(), 0) << std::endl;

    return 0;
}