0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

C++プログラミング入門: 初心者からエキスパートへの道

Posted at

はじめに

C++は強力で汎用性の高いプログラミング言語です。本記事では、C++の基礎から応用まで、15章にわたって詳しく解説します。各章には豊富なコード例と丁寧な説明を用意しました。

第1章: C++の基礎

C++の基本的な構文や概念を学びます。

#include <iostream>

int main() {
    std::cout << "こんにちは、C++の世界へようこそ!" << std::endl;
    return 0;
}

このプログラムは「こんにちは、C++の世界へようこそ!」と画面に表示します。#includeはヘッダーファイルを取り込む指令、main()は프로그램の開始点、std::coutは標準出力を表します。

第2章: 変数と型

C++で使用される基本的なデータ型と変数の宣言方法を学びます。

int age = 25;
double height = 170.5;
char grade = 'A';
bool isStudent = true;

intは整数、doubleは小数点数、charは1文字、boolは真偽値を表します。

第3章: 制御構造

if文、for文、while文などの制御構造を学びます。

for (int i = 1; i <= 10; i++) {
    if (i % 2 == 0) {
        std::cout << i << "は偶数です。" << std::endl;
    } else {
        std::cout << i << "は奇数です。" << std::endl;
    }
}

この例では1から10までの数字を順に調べ、偶数か奇数かを判定します。

第4章: 関数

関数の定義と呼び出し方を学びます。

int add(int a, int b) {
    return a + b;
}

int result = add(5, 3);
std::cout << "5 + 3 = " << result << std::endl;

add関数は2つの整数を受け取り、その和を返します。

第5章: 配列とベクター

配列とベクターの使い方を学びます。

int arr[5] = {1, 2, 3, 4, 5};

#include <vector>
std::vector<int> vec = {1, 2, 3, 4, 5};
vec.push_back(6);

配列は固定長、ベクターは可変長のデータ構造です。

第6章: ポインタとリファレンス

メモリ管理の基本となるポインタとリファレンスを学びます。

int x = 10;
int* ptr = &x;
int& ref = x;

*ptr = 20;
std::cout << x << std::endl; // 20が出力されます

ポインタ(*)はメモリアドレスを、リファレンス(&)は変数の別名を表します。

第7章: クラスとオブジェクト

オブジェクト指向プログラミングの基本であるクラスとオブジェクトを学びます。

class Dog {
public:
    std::string name;
    void bark() {
        std::cout << name << ":わんわん!" << std::endl;
    }
};

Dog myDog;
myDog.name = "ポチ";
myDog.bark();

Dogクラスはname属性とbarkメソッドを持ちます。

第8章: 継承とポリモーフィズム

クラスの継承と多態性について学びます。

class Animal {
public:
    virtual void makeSound() {
        std::cout << "動物の鳴き声" << std::endl;
    }
};

class Cat : public Animal {
public:
    void makeSound() override {
        std::cout << "にゃーん" << std::endl;
    }
};

Animal* animal = new Cat();
animal->makeSound(); // "にゃーん"と出力されます

CatクラスはAnimalクラスを継承し、makeSoundメソッドをオーバーライドしています。

第9章: 例外処理

エラーを適切に処理する例外処理について学びます。

try {
    int x = 10;
    int y = 0;
    if (y == 0) {
        throw std::runtime_error("0で除算はできません");
    }
    int result = x / y;
} catch (const std::exception& e) {
    std::cout << "エラー: " << e.what() << std::endl;
}

tryブロック内でエラーが発生すると、catchブロックでそれを捕捉します。

第10章: テンプレート

型に依存しない汎用的なコードを書くためのテンプレートを学びます。

template <typename T>
T max(T a, T b) {
    return (a > b) ? a : b;
}

std::cout << max(10, 20) << std::endl; // 20が出力されます
std::cout << max(3.14, 2.72) << std::endl; // 3.14が出力されます

このmax関数は、整数でも小数点数でも使用できます。

第11章: STL(標準テンプレートライブラリ)

C++の強力な機能であるSTLの使い方を学びます。

#include <vector>
#include <algorithm>

std::vector<int> numbers = {5, 2, 8, 1, 9};
std::sort(numbers.begin(), numbers.end());

for (int num : numbers) {
    std::cout << num << " ";
}

この例では、vectorを使用してデータを格納し、sort関数で並べ替えています。

第12章: ファイル入出力

ファイルの読み書きの方法を学びます。

#include <fstream>

std::ofstream outFile("test.txt");
outFile << "こんにちは、ファイル!" << std::endl;
outFile.close();

std::ifstream inFile("test.txt");
std::string line;
std::getline(inFile, line);
std::cout << line << std::endl;
inFile.close();

ofstreamでファイルに書き込み、ifstreamでファイルから読み込みます。

第13章: スマートポインタ

メモリリークを防ぐためのスマートポインタの使い方を学びます。

#include <memory>

class MyClass {
public:
    void doSomething() {
        std::cout << "何かをします" << std::endl;
    }
};

std::unique_ptr<MyClass> ptr = std::make_unique<MyClass>();
ptr->doSomething();

unique_ptrは、オブジェクトの所有権を一つのポインタが独占します。

第14章: ラムダ式

関数オブジェクトを簡潔に書くためのラムダ式を学びます。

auto greet = [](const std::string& name) {
    std::cout << "こんにちは、" << name << "さん!" << std::endl;
};

greet("田中");

ラムダ式を使用すると、その場で関数を定義できます。

第15章: 並行プログラミング

マルチスレッドプログラミングの基礎を学びます。

#include <thread>

void printNumbers(int start, int end) {
    for (int i = start; i <= end; i++) {
        std::cout << i << " ";
    }
}

std::thread t1(printNumbers, 1, 5);
std::thread t2(printNumbers, 6, 10);

t1.join();
t2.join();

この例では、2つのスレッドを使用して並行に数字を出力します。

以上が、C++プログラミングの基礎から応用までの15章にわたる解説です。各章の内容を十分に理解し、実際にコードを書いて試してみることで、C++プログラミングのスキルを着実に向上させることができるでしょう。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?