首页 > 试题广场 >

定义基类 Base,有两个共有成员函数 fn1()、fn2(

[问答题]

定义基类 Base,有两个共有成员函数 fn1()fn2(),私有派生出 Derived 类,如果想在 Derived 类的对象中使用基类函数 fn1(),应怎么办?

推荐

解:

class Base
{
public:
int fn1() const { return 1; }
int fn2() const { return 2; }
};
class Derived : private Base
{
public:
int fn1() { return Base::fn1();};
int fn2() { return Base::fn2();};
};
void main()
{
Derived a;
a.fn1();
}



发表于 2018-04-18 20:47:37 回复(0)