#include #include using namespace std; //クラス宣言 (クラスのメンバを書き連ねる) class cGameChar //クラス 基本的にプライベート { private: //アクセス権 string name; int hp; int mp; public: //アクセス権 //コンストラクタ cGameChar(); //引数無しのコンストラクタ cGameChar(string _name, int _hp, int _mp);//引数付きのコンストラクタ //setter セッター関数 void sethp(int _hp); //関数宣言 void setmp(int _mp); //getter ゲッター関数 int gethp(){ return(this->hp); } //インライン定義 int getmp(){ return(this->mp); } //インライン定義 void printCharStatus(); //HP,MPを表示する関数 }; //コンストラクタの定義 cGameChar::cGameChar() :name(""),hp(0),mp(0) { //this->name = ""; //this->hp = 0; //メンバの初期化 //this->mp = 0; //メンバの初期化 cout << "コンストラクタが呼ばれ、メンバが初期化されました" << endl; } //引数付きのコンストラクタの定義(オーバーロード) cGameChar::cGameChar(string _name, int _hp, int _mp) :name(_name), hp(_hp), mp(_mp)// ← メンバイニシャライザ { cout << "引数付きコンストラクタが呼ばれました。" << endl; } void cGameChar::printCharStatus() { cout << "=+=+=+=+=+=+=+=+=+=+" << endl; cout << "NAME: " << name << endl; cout << " HP: " << hp << endl; cout << " MP: " << mp << endl; cout << "=+=+=+=+=+=+=+=+=+=+" << endl; } //関数定義 void cGameChar::sethp(int _hp) { this->hp = _hp; } void cGameChar::setmp(int _mp) { this->mp = _mp; } // cGameCharの中では、自分のものは全部使うことができる! //class宣言は設計図 int main() { //cGameChar hero, boss; cGameChar hero("おるてが", 500,300); cGameChar boss("どるまげす", 1000,100); hero.printCharStatus(); boss.printCharStatus(); }