====== 継承の基本 その3 ====== ===== アニマルクラス ===== ==== cAnimal.h ==== #pragma once #include using namespace std; class cAnimal { //private://自クラスの中だけで読み書きできる protected: string mName;//メンバ変数 public://自クラス内外から読み書きできる //コンストラクタ cAnimal(); cAnimal(string _name); //ゲッターセッター string getName(); void setName(string _name); //動物の基本機能 void sleep(); void eat(); void getup(); }; ==== cAnimal.cpp ==== #include "cAnimal.h" #include using namespace std; cAnimal::cAnimal() { mName = ""; } cAnimal::cAnimal(string _name) :mName(_name) { } string cAnimal::getName() { return mName; } void cAnimal::setName(string _name) { mName = _name; } void cAnimal::sleep() { cout << this->mName << "は寝ます。ぐーぐー" << endl; } void cAnimal::eat() { cout << this->mName << "は食べます。もぐもぐ" << endl; } void cAnimal::getup() { cout << this->mName << "は起きた!ぱちっ" << endl; } ==== cDog.h ==== #pragma once #include "cAnimal.h" #include class cDog : public cAnimal { private: //犬種 string breedname; public: // cDog(string _name) { this->setName(_name); } cDog(string _name) { mName = _name; } // mName = _name; void setBreedName(string _bname); string getBreedName(); void bark();//bark動物が吠える }; ==== cDog.cpp ==== #include "cDog.h" #include using std::cout; using std::endl; void cDog::bark() { cout << "わんわん!" << endl; } void cDog::setBreedName(string _bname) { breedname = _bname; } string cDog::getBreedName() { return(breedname); } ==== cBird.h ==== #pragma once #include "cAnimal.h" #include using std::string; class cBird : public cAnimal { private: string birdname; public: cBird(string _name, string _birdname); cBird(); void setBrirdName(string _bname) { birdname = _bname; } string getBrirdName() { return birdname; } void fly(); }; ==== cBird.cpp ==== #include "cBird.h" #include using std::cout; using std::endl; cBird::cBird() :cAnimal() { } cBird::cBird(string _name, string _birdname) :cAnimal(_name),birdname(_birdname) { } void cBird::fly() { cout << "パタパタ" << endl; } ==== theMain.cpp ==== #include "cAnimal.h" #include "cDog.h" #include "cBird.h" #include using std::cout; using std::endl; int main() { //cAnimal ani("pochi"); //ani.mName = "ジョン"; //cout << ani.getName() << endl; //ani.eat(); //ani.sleep(); cDog john("ジョン"); //john.setName("ジョン"); cout << john.getName() << endl; john.setBreedName("ボルゾイ"); cout <<"犬種" <