Chapter11 - 6e
更新时间:2024-01-23 20:07:01 阅读量: 教育文库 文档下载
- chapter11推荐度:
- 相关推荐
Test Bank for Problem Solving with C++: The Object of Programming, 6/e Chapter 11 Friends and Overloaded Operators
TRUE/FALSE
1. Friend functions are members of the class. ANSWER: FALSE
2. All operators can be overloaded. ANSWER: FALSE
3. If you have mutators and accessors, you should not have friend functions also ANSWER: FALSE
4. Friend functions may directly modify or access the private data members. ANSWER: TRUE
5. The following is a properly declared overloaded insertion operator for myClass. ostream& operator <<(ostream &out, const myClass &obj); ANSWER: TRUE
6. Functions that are constant member functions may call the class mutator functions. ANSWER: FALSE
7. Functions that are constant member functions may call constant class accessor functions.
ANSWER: TRUE
8. You cannot create new operators (such as the quote). ANSWER: TRUE
9. Operators must be friends of the class. ANSWER: FALSE
10. You may not change the precedence of operators by overloading them ANSWER: TRUE Short Answer
1. If a given task being performed by a function involves more than one object, then that function should normally be a __________ function. ANSWER: friend
2. If a given task being performed by a function involves one object, then that function should normally be a __________ function. ANSWER: member
3. A _________ function is not a member of the class, but has access to the private members of the class. ANSWER: friend
4. An overloaded extraction or insertion operator should return ___________ ANSWER: a reference to the stream
5. A friend function needs to be passed an object of the class. If the friend only
needs to access the object, but not change its data members, then the object should be passed as _______________ ANSWER: a constant reference
6. An operator that expects only one parameter is called a ________ operator ANSWER: unary
7. An operator that expects two parameters is called a ________ operator. ANSWER: binary
8. In order to do automatic type conversion for your class, you would write _________
ANSWER: overloaded functions or overloaded constructors
Test Bank for Problem Solving with C++: The Object of Programming, 6/e Chapter 11 Friends and Overloaded Operators
9. Putting the keyword const after the function declaration guarantees __________________________
ANSWER: That the function will not change the calling object.
10. Putting the keyword const in front of a pass by reference parameter guarantees ___________________
ANSWER: that the function will not modify that parameter.
Multiple Choice
1. How many members (data and functions) does the following class have? class Rational {
public:
Rational();
Rational(int numer, int denom); Rational(int whole);
int getNumerator(); int getDenominator();
friend void display(ostream& out, const Rational& value); private:
int numerator; int denominator; };
a. 2 b. 6 c. 5 d. 7 e. 8 ANSWER: D
2. Who can access private data in a class?
a. members of the class b. friends of the class c. everyone d. B and C e. no one ANSWER: D
3. Given the following class, which is the correct function header for the display function? class Rational {
public:
Rational();
Rational(int numer, int denom); Rational(int whole);
Test Bank for Problem Solving with C++: The Object of Programming, 6/e Chapter 11 Friends and Overloaded Operators
int getNumerator(); int getDenominator();
friend void display(ostream& out, const Rational& value); private:
int numerator; int denominator; };
a. friend void display(ostream& out, const Rational& value) b. void display(ostream& out, const Rational& value)
c. void Rational::display(ostream& out, const Rational& value)
d. friend void Rational::display(ostream& out, const Rational& value) ANSWER: B
4. Operators can be overloaded as
a. friends of a class b. members of a class
c. non-friends, non-members of a class d. All of the above ANSWER: D
5. If we have a full selection of accessor and mutator functions, why would we have friend functions?
a. You should not have them
b. More efficient access to the private data members.
c. The friend function must call the accessor or mutator functions anyway. d. none of the above ANSWER: B
6. Since accessors functions in a class do not modify or mutate the data members of the object, the function should have the __________ modifier.
a. reference b. friend c. const d. private ANSWER: C
7. Why should you generally pass an object of the class to a friend function as a reference parameter?
a. If the friend function changes the values of the data member(s).
b. If the friend function will not change the values of the data member(s). c. It is more efficient to pass the object by reference. d. A and B e. A and C ANSWER: E
8. If c is a character variable that contains a digit, what does the following function return?
int digit_to_int(char c) {
return ( int(c) – int('0'));
Test Bank for Problem Solving with C++: The Object of Programming, 6/e Chapter 11 Friends and Overloaded Operators }
a. The ASCII value of c b. The character value of c
c. The integer equivalent of the digit stored in c d. none of the above ANSWER: C
9. What is wrong with the following member function definition given the class below? class Rational {
public:
Rational();
Rational(int numer, int denom); Rational(int whole);
int getNumerator() const; int getDenominator() const;
friend void display(ostream& out, const Rational& value); private:
int numerator; int denominator; };
int Rational::getNumerator() const {
numerator = 0; return numerator; }
a. You can not set the numerator to zero
b. The function may not modify numerator, but it can modify denominator c. The function may not modify any of the private data members d. nothing e. A and B f. A and C ANSWER: F
10. Given the following class, what is syntactically wrong with the implementation of the display function? class Rational {
public:
Rational();
Rational(int numer, int denom); Rational(int whole);
int getNumerator();
Test Bank for Problem Solving with C++: The Object of Programming, 6/e Chapter 11 Friends and Overloaded Operators
int getDenominator();
friend void display(ostream& out, const Rational& value); private:
int numerator; int denominator; };
void display(ostream& out, const Rational& value) {
out << value.getNumerator() << '/\}
a. nothing
b. value must be not be pass by reference c. The get functions are not const functions d. out should be pass by value ANSWER: C
11. To overload functions with symbolic names (like + - / <<), you must use the keyword _______ before the symbolic name.
a. const b. operator c. reference d. void ANSWER: B
12. In the following code fragment, which is the calling object for the less-than operator? string s1, s2; if( s1 < s2 )
a. s1 b. s2 c. < d. none ANSWER: A
13. Given the following class declaration, class Rational {
public:
Rational();
Rational(int numer, int denom);
int getNumerator() const; int getDenominator() const;
friend void display(ostream& out, const Rational& value);
friend bool operator(const Rational& left, const Rational& right);
Test Bank for Problem Solving with C++: The Object of Programming, 6/e Chapter 11 Friends and Overloaded Operators
private:
int numerator; int denominator; };
what must we add to the class in order for the following code to compile? Rational myRational(2,3); if ( 3 < myRational)
a. We need another < operator that expects an integer as the second parameter.
b. We need another < operator that expects an integer as the first parameter. c. We need a constructor that expects a ration number d. We need a constructor that expects an integer e. A or D f. B or D ANSWER: F
14. When overloading an operator, which of the following is true?
a. One of the arguments must be an object of the class b. The operator can be a friend or a member of the class.
c. The operator does not have to be a friend or a member of the class d. All of the above e. None of the above ANSWER: D
15. What is wrong with the following overloaded extraction operator declaration? istream& operator >>(istream& in, const myClass &object);
a. Object should not be a pass by reference parameter b. Object should not be a const parameter c. You can not put the & on the return type d. nothing ANSWER: B
16. Which of the following would be an appropriate function declaration to add two rational numbers?
a. void friend operator+( const Rational &left, const Rational &right); b. void operatator+( const Rational &left, const Rational &right);
c. friend Rational operator+( const Rational &left, const Rational &right); d. Rational operator+( const Rational &left, const Rational &right); ANSWER: D
17. How many parameters are there in a binary operator implemented as a friend?
a. 0 b. 1 c. 2
d. as many as you need ANSWER: C
18. How many parameters are there in a unary operator implemented as a friend?
a. 0 b. 1
Test Bank for Problem Solving with C++: The Object of Programming, 6/e Chapter 11 Friends and Overloaded Operators
c. 2
d. as many as you need ANSWER: B
19. Given the following function declaration, friend void display(const myClass& object);
which is the correct header for the definition of the function?
a. void myClass::display(const myClass& object) b. void display(const myClass& object)
c. friend void display(const myClass& object); d. friend void display(const myClass& object) ANSWER: B
20. Which of the following function declarations would be correct to overload the multiply operator for the Rational numbers class?
a. friend Rational operator times(const Rational &left, const Rational &right); b. Rational operator times(const Rational &left, const Rational &right); c. friend Rational operator *(const Rational &left, const Rational &right); d. Rational operator *(const Rational &left, const Rational &right); ANSWER: C
21. Why are the extraction and insertion operators always implemented as friends of the class rather than as members of the class?
a. Because the first parameter must be the stream object. b. They don't, they could be members c. Because they return a reference
d. Because the stream is passed by reference. ANSWER: A
22. If you want to be able to compile the following code, Rational r1; int x;
cout << r1 + x << endl;
which overloaded operator(s) do you need?
a. friend Rational operator+( const Rational& left, int right); b. friend void operator+ (const Rational& left, int right);
c. friend ostream operator << (ostream& out, const Rational& object); d. friend ostream& operator << (ostream& out, const Rational& object); e. A and C f. A and D ANSWER: F
23. What member functions do you need to allow the compiler to perform automatic type conversions from a type different than the class to the class?
a. Overloaded constructors b. Converters
c. This can not be done
d. This already happens automatically ANSWER: A
Test Bank for Problem Solving with C++: The Object of Programming, 6/e Chapter 11 Friends and Overloaded Operators
24. In an overloaded insertion or extraction operator, which object should be the first parameter, the stream or the object of the class?
a. the stream b. the object
c. it doesn't matter d. none of the above ANSWER: A
25. Which of the following operators can not be overloaded?
a. = b. == c. . d. [] ANSWER: C
正在阅读:
Chapter11 - 6e01-23
高校辅导员专业化建设研究综述08-18
2017年辽宁省鞍山市中考数学试卷(含答案解析版)10-08
复变作业参考答案08-27
在2022年全市教育大会暨优秀教师表彰大会上的讲话范文03-23
中考物理选择题专题训练 - 图文10-13
百分数复习讲义01-30
- 1Organization Behavior 15e Chapter 2 Testbank
- 2金融英语 chapter 6
- 3Chapter 6 Objects and Classes
- 4investment chapter6
- 5Chapter 6 - Stock Valuation
- 6Chapter 6 The Total Physical Response
- 7Chapter6(3)-179072
- 8cost accounting test bank chapter 6
- 9cost accounting test bank chapter 6
- 10商务秘书实务Chapter 6 - Writing memos in English
- exercise2
- 铅锌矿详查地质设计 - 图文
- 厨余垃圾、餐厨垃圾堆肥系统设计方案
- 陈明珠开题报告
- 化工原理精选例题
- 政府形象宣传册营销案例
- 小学一至三年级语文阅读专项练习题
- 2014.民诉 期末考试 复习题
- 巅峰智业 - 做好顶层设计对建设城市的重要意义
- (三起)冀教版三年级英语上册Unit4 Lesson24练习题及答案
- 2017年实心轮胎现状及发展趋势分析(目录)
- 基于GIS的农用地定级技术研究定稿
- 2017-2022年中国医疗保健市场调查与市场前景预测报告(目录) - 图文
- 作业
- OFDM技术仿真(MATLAB代码) - 图文
- Android工程师笔试题及答案
- 生命密码联合密码
- 空间地上权若干法律问题探究
- 江苏学业水平测试《机械基础》模拟试题
- 选课走班实施方案
- Chapter11
- 6e
- 新课标高中物理选修3-2课后习题答案免去财富值
- 家长学校教师教案(20)
- 武夷山一中2013-2014学年高二(下)期末考试文科数学试卷与答案
- 2008年中考物理试题分类汇编—平衡力
- 最新北师大版 四年级数学上册第三单元 乘法教学设计与教学反思 - 图文
- 幼儿教育学考试复习资料
- 基于Matlab的指纹图像特征提取
- STM32L051电阻分压ADC电量采集
- 6 传热作业
- 行政事业单位会计制度讲解
- 欧洲古代史 思考题
- 关于水生植物:设计 种植 密度 - 图文
- 地球生命保护伞(社会资源整合) - 图文
- 文件一:广晟公司项目创新简介
- NI 数据采集卡使用入门
- 潜江2016年初中毕业年级四月调考语文参考答案
- 计量经济学模型分析论文 影响我国人均GDP的变量因素分析
- 高考作文序列化训练专题九
- 腹针操作规范
- 内工大 校发54号 签发人邢永明讲解