C++を学ぶ
知っていることより知らないことの方が多いC++
処理の一部だけを特殊化したい場合に、派生クラスでオーバーライドしたものを使う。
こんな感じで良いのだろうか。
#include <iostream> using namespace::std; // 基底 class cBase { public: cBase(){} virtual ~cBase() {} void exec() { cout << "In cBase::exec -> "; sub_exec(); // 継承先でオーバーライドした処理を実行 } virtual void sub_exec() { cout << "cBase::sub_exec" << endl; } }; // 派生1 class cDerive1 : public cBase { public: cDerive1() {} ~cDerive1() {} void sub_exec() { cout << "cDerive1::sub_exec" << endl; } }; // 派生2 class cDerive2 : public cBase { public: cDerive2() {} ~cDerive2() {} void sub_exec() { cout << "cDerive2::sub_exec" << endl; } }; int main() { cBase* base = new cDerive1(); base->exec(); delete base; base = new cDerive2(); base->exec(); delete base; return 0; }