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)

第10章 クラス :メンバ変数とメンバ関数

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

C++の様々な基本型について学んだ。数値型、文字、文字列などだ。 標準ライブラリのクラスも何度か使った。std::vectorのようなクラステンプレートも含まれる。 この章では、独自のクラスを書く方法を示す。

クラスを使えば、関連する要素をまとめて束ねられる。 例えば、銘柄名と始値、そして価格更新を生成・入力する方法を一緒に束ねられる。 名前空間も関連する関数や型をグループ化するが、クラスの方が選択肢が多い。 要素をクラスにまとめることで新しい型が生まれ、取引ゲームに新たな株の種類を簡単に追加できる。 クラスには意味を伝える名前を付けることもでき、コードの読み取り性を高められる。

1979年頃、C++はビャルネ・ストローストラップによって「クラス付きC」としてを開始した。 彼は大規模なコードベースを扱う際にクラスが有用だと発見した。クラスはより高いレベルで考える手段を提供するからだ。 個々の名前や価格、価格取得方法ではなく、株式という概念そのものを扱えるのだ。 さらに、名前はCスタイルのcharポインタよりも高次な概念であるstd::stringクラスで表現される。 これまで見てきたように、C++にはクラス以外にも多くの要素があるが、クラスは多くのC++コードベースの基盤となる部分だ。

まずはクラスを作成しよう。データから始め、その後で振る舞いを追加する。 この章の終わりまでに、様々な株式のstd::vectorを作成できるようになる。 そうすれば、より大規模な取引ゲームに一歩近づくことになる。

単純なクラス

クラスとは、メンバー変数とメンバー関数を持つことができるユーザー定義型だ。 クラスの最も単純な形は構造体である。

stock.h という名前の新しいファイルを作成する。 名前、最終価格、ボラティリティを持つStockクラスを定義する。この章の残りの部分でこれを拡張し、様々な株価を生成するために使用する。

ヘッダファイルで構造体を宣言する。キーワードstruct に名前を続けて記述する。 単純な構造体の場合、データメンバーの初期化には波括弧{} を使用できる。これは波括弧初期化と呼ばれる。 これにより各型にデフォルト値が設定される。例えば doubleの場合、デフォルト値0.0が設定される。 クラスの最後の波括弧はセミコロンで終わる。例:

#pragma once

#include <string>

namespace stock_prices
{
    struct Stock 1
    { 2
        std::string name{}; 3
        double last_price{}
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