実現したいこと
vs17を利用して、C++の勉強をしています。
現在派生クラスを利用するプログラムを作成中です。
その中で派生クラスを利用し、main内でソートを行う過程で、
基底クラスに、vecterコンテナを定義し、派生クラスでも利用したいです。
追記
派生クラスにおいてセットしているspeedを引き出し、ソートを行いたいです。
発生している問題
vectorコンテナを派生クラスで利用できない。
出力結果には何も出力されません。
関連コード
Human
1class Human 2{ 3public: 4 Human(); 5 6 //Setter/Getter 7 std::string GetName(); 8 void SetName(std::string _name); 9 int GetSpeed(); 10 void SetSpeed(int _speed); 11 12 bool operator<(const Human &anothor)const; 13 14private: 15 std::string name; 16 int speed; 17}; 18 19std::string Human::GetName() { 20 return this->name; 21} 22void Human::SetName(std::string _name) { 23 this->name = _name; 24} 25 26int Human::GetSpeed() { 27 return this->speed; 28} 29void Human::SetSpeed(int _speed) { 30 this -> speed = _speed; 31} 32 33bool Human::operator<(const Human &anothor)const { 34 return this->speed < anothor.speed; 35}
LastBoss
1class LastBoss : public Human 2{ 3public: 4 LastBoss(); 5}; 6 7LastBoss::LastBoss(){ 8 SetName("らすぼす"); 9 SetSpeed(15); 10}
Yuusya
1class Yuusya : public Human 2{ 3public: 4 Yuusya(); 5}; 6 7uusya::Yuusya(){ 8 SetName("ゆうしゃ"); 9 SetSpeed(20); 10}
main
1int main(){ 2 std::vector<Human> human; 3 Yuusya yuusya; 4 LastBoss lastBoss; 5 6 std::sort(human.begin(),human.end()); 7 8 for (auto itr : human) { 9 std::cout << "Speed:" << itr.GetSpeed() << " name:" << itr.GetName() << std::endl; 10 } 11 12 return 0; 13}
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/04/19 09:04
2019/04/19 11:31