C++クラス

〜 目次 〜

クラス

クラスの簡単な書式は以下の通りである。

class クラス名
{
private:        // 〜〜非公開部分〜〜

    データメンバ
    ・・・
        
public:         //〜〜公開部分〜〜
    
    メンバ関数    
    ・・・
}
キーワードの説明
アクセス指定子意味
publicこれ以降に書かれたメンバは公開される。すべての関数でクラスメンバを使用できる。
privateこれ以降に書かれたメンバは非公開となる。クラスのメンバ関数とフレンド(クラス、関数)でクラスメンバを使用できる。
protectedこの宣言がされたクラスメンバはprivate機能を持ち、派生クラスのメンバ関数からアクセスが可能になる。

例:クラス

#include <iostream.h>
#include <string.h>

class hito            //クラス宣言
{
private:
    char name[40];
    int age;
public:
    void setname(char* ss);    //名前を設定するメンバ関数の宣言
    void setage(int tosi);    //年齢を設定するメンバ関数の宣言
    void disp();        //内容を出力するメンバ関数の宣言
};

void hito::setname(char* ss)    //メンバ関数(setname)の定義
{
    strcpy(name,ss);
}

void hito::setage(int tosi)    //メンバ関数(setage)の定義
{
    if(tosi<0) age=0; ekse age=tosi
}

void hito::disp()        //メンバ関数(disp)の定義
{
    cout << "名前:" << name << endl;
    cout << "年齢:" << age << endl;
}

void main()
{
    hito yamada;

    yamada.setname("山田一郎");
    yamada.setage(30);
    yamada.disp();
}

例:クラス2

#include <iostream.h>
#include <string.h>

class hito            //クラス宣言
{
    char name[40];        //アクセス指定子がない場合はprivateと同じ
    int age;
public:
    void setname(char* ss) { strcpy(name,ss); }        //名前設定関数
    void setage(int tosi) { age=(tosi<0) ? 0: tosi;}    //年齢設定宣言
    char* getname() { return name; }             //名前を返す関数
    int getage() { return age; }             //年齢を返す関数
    void disp();                    //内容を出力関数
};

void hito::disp()            //メンバ関数(disp)の定義
{
    cout << "名前:" << name << endl;
    cout << "年齢:" << age << endl;
}

void main()
{
    hito yamada;
    int nn;

    yamada.setname("山田一郎");
    yamada.setage(30);
    nn=yamada.getage();
    yamada.disp();
    cout << yamada.getname() << yamada.getage() << nn << endl;
}

実行結果

名前:山田一郎
年齢:30
山田一郎3030
↑目次に戻る

コンストラクタとデストラクタ

コンストラクタ・・・オブジェクト宣言時に初期値を設定する特殊な初期化関数。
デストラクタ  ・・・オブジェクトが消滅する時、その後始末をする後処理関数。
        クラス名 : mycls ・・・クラス名の例
コンストラクタ名 : mycls ・・・クラス名と同じ
  デストラクタ名 : ~mycls・・・クラス名に~がつく

例:コンストラクタとデストラクタ

#include <iostream.h>
#include <string.h>

class hito
{
private:
    char name[40];
    int age;
public:
    hito(char *ss, int tosi);    //コンストラクタ
    void setname(char* ss) { strcpy(name,ss); }
    void setage(int tosi) { age=(tosi<0) ? 0: tosi; }
    char* getname() { return name; }
    int getage() { return age; }
    void disp();
    ~hito();    //デストラクタ
};

hito::hito(char *ss, int tosi)    //コンストラクタの定義
{
    strcpy(name,ss);
    if(tosi<0) age=0; else age=tosi;
    cout << "コンストラクタ呼び出し\n";
}

hito::~hito()    //デストラクタの定義
{
    cout << "デストラクタ呼び出し\n";
}

void hito::disp()
{
    cout << "名前:" << name << endl;
    cout << "年齢:" << age << endl;
}

void subfunction()
{
    cout << "--subfunction--" << endl;
    hito tanaka("田中二郎",20);
    tanaka.disp();
}

void main()
{
    cout << "start\n";
    hito yamada("山田一郎",10);
    
    yamada.disp();
    subfunction();
    cout << "end\n";
}

実行結果

start
コンストラクタ呼び出し    //yamadaのコンストラクタ
名前:山田一郎
年齢:10
--subfunction--
コンストラクタ呼び出し    //tanakaのコンストラクタ
名前:田中二郎
年齢:20
デストラクタ呼び出し    //tanakaのデストラクタ
end
デストラクタ呼び出し    //yamadaのデストラクタ
↑目次に戻る

クラスのファイル分割

  1. ヘッダファイル

    クラスの宣言部を格納する。


  2. ソースファイル

    クラスの外部定義部分を格納する。ここでは、上のヘッダファイルもインクルードする。


  3. クラスを利用するプログラム

    クラスを利用するプログラムを記述。ここでも、ヘッダファイルをインクルードする。

例:クラスのファイル分割

1.クラスヘッダファイル(hito.h)

#ifndef _INC_HITO    //まだ読み込まれていなければ以下の処理をする
#define _INC_HITO    //すでに読み込まれているという目印をつける

#include <string.h>

class hito
{
    char name[40];
    int age;
public:
    hito();
    hito(char* ss);
    hito(int tosi);
    hito(char *ss, int tosi);
    void setname(char* ss) { strcpy(name,ss); }
    void setage(int tosi) { age=(tosi<0) ? 0: tosi; }
    char* getname() { return name; }
    int getage() { return age; }
    void disp();
};
#endif    //INC_HITO

2.クラスソースファイル(hito.cpp)

#include "hito.h"    //作成したクラスヘッダファイルを読み込む
#include <string.h>
#include <iostream.h>

hito::hito()
{
    strcpy(name,"(未定義)");
    age=0;
}

hito::hito(char *ss)
{
    strcpy(name,ss);
    age=0;
}

hito::hito(int tosi)
{
    strcpy(name,"(未定義)");
    if(tosi<0) age=0; else age=tosi;
}

hito::hito(char *ss, int tosi)
{
    strcpy(name,ss);
    if(tosi<0) age=0; else age=tosi;
}

void hito::disp()
{
    cout << "名前:" << name << endl;
    cout << "年齢:" << age << endl;
}

3.クラス利用ファイル(hitotest.cpp)

#include <iostream.h>
#include "hito.h"    //作成したクラスヘッダファイルを読み込む

void main()
{
    hito man1;
    hito man2("山田二郎");
    hito man3(30);
    hito man4("山田四郎",40);
    hito man5="山田五郎";
    hito man6=60;
    hito yamada[2];
    hito tanaka[3]={ hito("田中和雄",70), hito("田中花子",80) };
    cout << "----------------- man1 - man6" << endl;
    man1.disp();
    man2.disp();
    man3.disp();
    man4.disp();
    man5.disp();
    man6.disp();
    cout << "----------------- 山田家" << endl;
    for(int i=0; i<2; i++){
        cout << "i=" << i << endl;
        yamada[i].disp();
    }
    cout << "----------------- 田中家" << endl;
    for(i=0; i<3; i++){
        cout << "i=" << i << endl;
        tanaka[i].disp();
    }
}

実行結果

----------------- man1 - man6
名前:(未定義)
年齢:0
名前:山田二郎
年齢:0
名前:(未定義)
年齢:30
名前:山田四郎
年齢:40
名前:山田五郎
年齢:0
名前:(未定義)
年齢:60
----------------- 山田家
i=0
名前:(未定義)
年齢:0
i=1
名前:(未定義)
年齢:0
----------------- 田中家
i=0
名前:田中和雄
年齢:70
i=1
名前:田中花子
年齢:80
i=2
名前:(未定義)
年齢:0
↑目次に戻る

参考文献

新Visual C++6.0入門 ビギナー編 part2 C++入門


戻る
LastUpdate 2000/09/09