オーバーライドができない理由が分からず、こまっていたところです。
ここにコードを記します。
//main.cpp #include "commonInclude.h" #include"cat.h" #include"fatCat.h" int main() { cat cat1 = cat("1stCat"); cat1.naku(); cat cat2 = fatCat(23); cat2.naku(); } //commonInclude.h #include <iostream> #include <string> using namespace std; //cat.h #ifndef catIncluded #define catIncluded #include "commonInclude.h" class cat { protected: string name; public: cat(); cat(string); virtual void naku(); }; #endif //cat.cpp #include "cat.h" cat::cat(){ name = "ななし"; } cat::cat(string s){ name = s; } void cat::naku(){ cout<<"おいらの名前は"<<name<<"だ。"<<endl; } //fatCat.h #ifndef fatCatIncluded #define fatCatIncluded #include "commonInclude.h" #include "cat.h" class fatCat : public cat{ int weight; public: fatCat(int); void naku(); }; #endif //fatCat.cpp #include "fatCat.h" fatCat::fatCat(int num){ weight = num; name = "ななし"; } void fatCat::naku() { cout<<"おいらは太っている、"<<name<<"だ。"<< endl; cout << "体重は"<< weight << "だ。" <<endl; }
この実行時に、
おいらの名前は1stCatだ。
おいらは太っている、ななしだ。
体重は23だ。
と表示したいところが、
おいらの名前は1stCatだ。
おいらの名前はななしだ。
と表示されてしまいます。
原因と解決法を教えてください<(_ _)>