Skip to Content
C++の紹介 (Japanese Edition)
book

C++の紹介 (Japanese Edition)

by Frances Buontempo
March 2026
Intermediate
348 pages
4h 30m
Japanese
O'Reilly Media, Inc.
Content preview from C++の紹介 (Japanese Edition)

第2章 変数 とキーボード入力

この作品はAIを使って翻訳されている。ご意見、ご感想をお待ちしている:translation-feedback@oreilly.com

この章では、別の短いプログラムで入力を受け付ける。 変数の宣言について学び、さらに多くの関数の書き方の練習をする。 また、エラー処理の一般的なアプローチについても考え始める。これについては次の章で詳しく説明する。 そうすれば、第3章でより大きなプログラムの構築を始める準備が整う。この大きなプログラムは株価データを入力・分析するもので、このプロジェクトは本書の残りの部分で追加していくことになる。

まず入力を受け取る必要がある。その方法を学ぼう。今回も単一ファイルでプログラミングを行う。コードの構築方法を確認したい場合は「ツールの使い方」を参照せよ

出力を作成した際には、std::printlnstd::cout の演算子<< の 2 つの方法を使用した。C++ では入力ストリームstd::cin を使用するオプションが 1 つある。 入力はどこかに保存する必要があるため、まず保存場所を用意しなければならない。

変数の宣言

新しいソースファイルを作成し、input.cppと名付けよう。これで実験の場ができる。 すぐに入力を受け取るプログラムを書くが、まずその入力を受け取る変数が必要だ。 C++の変数には全てがある。前の章の例1-1で main関数の戻り値としてintに出会った。 型を先に指定し、その後に名前を付ける。 値を明示的に指定できる。例えば:

int number=0;

しかし、より一般的なのアプローチでは、変数名の後に{}を付ける「中括弧による初期化」を使う:

int number{};

数値は依然としてゼロで初期化される。 この現代的な手法はC++11で導入されたため、これを使用するコードをビルドする際はstdフラグの使用を忘れるな。 {0}のように数値を中括弧内に記述できるが、ゼロを指定したい場合は不要だ。 ISOコアガイドラインはこの{}による初期化子構文について詳細を定めている。

ヒント

ISOコアガイドラインは、C++ の発明者である Bjarne Stroustrup 氏と、著名な C++ エキスパートである Herb Sutter 氏によって編集された、オープンソースのガイドラインのコレクションである ()。このガイドラインは、C++ プログラマがよりシンプルで、より効率性が高く、より保守性が高いコードを書くことを支援することを目的としている。

numberは 変数だから、後で値を変更するなどして(値を変更可能)にできる。

number = 42;

C++では、変数をconst定数の略)として宣言できる。これは初期値から変更しないことを意味する。 変更しようとするとエラーが発生する。以下はコンパイルされない:

const int number{1};
number = 73;

この本の続きでconstの使い方はさらに出てくるが、ここでは入力に基づいてnumberを変えたいのでconstは使わない。

input.cppファイルで、メイン関数内にintを宣言する:

int main()
{
    int number{}; 1
}

数値を宣言し、{}を使用して 0 ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

エージェントメッシュ (Japanese Edition)

エージェントメッシュ (Japanese Edition)

Eric Broda, Davis Broda

Publisher Resources

ISBN: 0642572352653