次のコードでは、クラスの関数set(int, int)
でBase
、派生クラスの関数を呼び出したいと思いますshowK()
。これを行う方法はありますか?
showK()
クラスで関数を宣言Base
できず、仮想化できません。これは私にとっての制限です。
class Base{
int i, j;
public:
void set( int, int );
void show() { cout << i << " " << j << "\n"; }
};
void Base:: set(int a, int b)
{
i=a; j=b;
//Here I want to call the function showk() of class derived . Is there a way to call?.
}
class derived : public base {
int k;
public:
derived(int x) { k=x; }
virtual void showk() { cout << k << "\n"; }
};
int main()
{
derived ob(3);
ob.set(1, 2); // access member of base
ob.show(); // access member of base
ob.showk(); // uses member of derived class
return 0;
}
前もって感謝します。