由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - C++ Q96: function inheritance
相关主题
bloomberg 电面面经Phone Interview面经
C++ object size一问PayPal@eBay onsite(失败)题目和经验
C++继承问题[合集] PayPal@eBay onsite(失败)题目和经验
bloomberg面经Listen to My Story(2): C++书籍篇
请教template class怎么处理Inheritance问题(面试题)Amazon的一些电面问题
问一个c++ virtual base class的问题Bloomberg网上测试题
[合集] 问几道amazon面试题讨论:关于一个design 问题
攒人品,Amazon 二面面经Bloomberg offer全过程
相关话题的讨论汇总
话题: equals话题: error话题: print话题: bool话题: equal
进入JobHunting版参与讨论
1 (共1页)
c**********e
发帖数: 2007
1
Error information in the following code is Error: equals is not a member of
B.
My question: Why equals() is not a member of B but print() is?
While this seems obvious, what is the general rule that a function will be
inherited?
#include
using namespace std;
class A {
public:
bool equal(A a) { return true; }
void print() { cout << "print\n"; }
};
class B: public A {};
int main() {
B b;
bool x = b.equals(b);
b.print();
return 0;
}
b*****c
发帖数: 1103
2
。。。。。
因为equal(A)而不是equal(B),而且b是B啊
S**I
发帖数: 15689
3
because "equal" is inherited, but "equals" is not.

of

【在 c**********e 的大作中提到】
: Error information in the following code is Error: equals is not a member of
: B.
: My question: Why equals() is not a member of B but print() is?
: While this seems obvious, what is the general rule that a function will be
: inherited?
: #include
: using namespace std;
: class A {
: public:
: bool equal(A a) { return true; }

b*****c
发帖数: 1103
4
哦哦哦,另外一个问题,B不需要显式转换A吗??

【在 S**I 的大作中提到】
: because "equal" is inherited, but "equals" is not.
:
: of

r*******y
发帖数: 1081
5
you defined equal instead of equals

of

【在 c**********e 的大作中提到】
: Error information in the following code is Error: equals is not a member of
: B.
: My question: Why equals() is not a member of B but print() is?
: While this seems obvious, what is the general rule that a function will be
: inherited?
: #include
: using namespace std;
: class A {
: public:
: bool equal(A a) { return true; }

r*******y
发帖数: 1081
6
B will slice down to A

【在 b*****c 的大作中提到】
: 哦哦哦,另外一个问题,B不需要显式转换A吗??
c**********e
发帖数: 2007
7
Faint. I made such a low level mistake. Then what is the output of the
following code?
#include
using namespace std;
class A {
public:
bool equals(A a) { return data==a.data; }
void setData(int input) { data=input; }
private:
int data;
};
class B: public A {};
void main() {
A a;
a.setData(7);
B b;
b.setData(7);
cout << a.equals(b) << endl;
cout << b.equals(a) << endl;
}
t*****n
发帖数: 25
8
1
1

【在 c**********e 的大作中提到】
: Faint. I made such a low level mistake. Then what is the output of the
: following code?
: #include
: using namespace std;
: class A {
: public:
: bool equals(A a) { return data==a.data; }
: void setData(int input) { data=input; }
: private:
: int data;

b*****c
发帖数: 1103
9
//true
1
1
f********n
发帖数: 1163
10
确认一下:
cout << a.equals(b) << endl;
比较的是a的data和b.data?
j********x
发帖数: 2330
11
这还需要确认么

【在 f********n 的大作中提到】
: 确认一下:
: cout << a.equals(b) << endl;
: 比较的是a的data和b.data?

1 (共1页)
进入JobHunting版参与讨论
相关主题
Bloomberg offer全过程请教template class怎么处理Inheritance问题(面试题)
1st Amazon phone interview (1hr)问一个c++ virtual base class的问题
One C++ question[合集] 问几道amazon面试题
BB电面攒人品,Amazon 二面面经
bloomberg 电面面经Phone Interview面经
C++ object size一问PayPal@eBay onsite(失败)题目和经验
C++继承问题[合集] PayPal@eBay onsite(失败)题目和经验
bloomberg面经Listen to My Story(2): C++书籍篇
相关话题的讨论汇总
话题: equals话题: error话题: print话题: bool话题: equal