一道题
定义基类Base,有两个共有成员函数fn1()、fn2(),私有派生出Derived类,如果想在Derived类的对象中使用基类函数fn1(),应怎么办?
我的代码:
#include
using namespace std;
class Base
{
public:
Base(){}
~Base(){}
void fn1();
void fn2();
};
void Base::fn1()
{
cout << "Base fn1 is used." << endl;
}void Base::fn2()
{
cout << "Base fn2 is used." << endl;
}class Derived :public Base
{
public:
Derived(){}
~Derived(){}
};
int main()
{
Derived n;
n.fn1();
n.fn2();
return 0;
}
教程的代码:
#include
using namespace std;
class Base
{
public:
int fn1() const { cout << "Base fn1 is used." << endl;
return 1;
}
int fn2() const { cout << "Base fn2 is used." << endl;
return 2;
}
};
class Derived : private Base
{
public:
int fn1() { cout << "Derived fn1 is used." << endl;
return **Base::fn1();
** };
int fn2() { cout << "Derived fn2 is used." << endl;
return **Base::fn2();
** };
};
int main()
{
Derived derive;
derive.fn1();
derive.fn2();
return 0;
}
【一道题】不是很明白加粗地方的想法。
推荐阅读
- parallels|parallels desktop 解决网络初始化失败问题
- JS中的各种宽高度定义及其应用
- jhipster|jhipster 升级无效问题
- “精神病患者”的角度问题
- 画解算法(1.|画解算法:1. 两数之和)
- 【生信技能树】R语言练习题|【生信技能树】R语言练习题 - 中级
- SpringBoot调用公共模块的自定义注解失效的解决
- python自定义封装带颜色的logging模块
- 解决SpringBoot引用别的模块无法注入的问题
- leetcode|leetcode 92. 反转链表 II