0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

C++ クラス基礎 継承 メンバ初期化リスト ポインタ

Posted at

サンプル

C++のクラス基礎として
・継承:親クラスと子クラス
・メンバ初期化リスト
・ポインタ
・コンストラクタ
・デストラクタ
を実行される流れとともに確認できます。
基礎としてはこんなところでしょうか。

Cplusplus_class.cpp
# include <iostream>

class Food 
{
public:
    Food()//コンストラクタ
    {
        std::cout << "Food コンストラクタ" << std::endl;
    }
    ~Food()//デストラクタ
    {
        std::cout << "Food デストラクタ" << std::endl;
    }
};

class Rice : public Food
{
public:
    //メンバ初期化リスト コンストラクタ
    Rice() : Hakumai(0), Genmai(0), Rokkoku()
    {
        std::cout << "Rice コンストラクタ" << std::endl;
    }
    ~Rice()//デストラクタ
    {
        std::cout << "Rice デストラクタ" << std::endl;
    }
    void ShowHakumaiValue() {
        std::cout << "Riceの中のHakumai = " << Hakumai << std::endl;
    }
private:
    //メンバ変数
    int Hakumai;// = 0;
    int Genmai;// = 0;
    bool Rokkoku;
};

int main()
{
    std::cout << "スタート" << std::endl;
    //ポインタ
    Rice *rice;
    rice = new Rice();
    rice->ShowHakumaiValue();
    delete rice;
    std::cout << "エンド" << std::endl;
    return 0;
}

出力結果

スタート
Food コンストラクタ
Rice コンストラクタ
Riceの中のHakumai = 0
Rice デストラクタ
Food デストラクタ
エンド
0
2
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
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?