Chapter11 - 6e
更新时间:2024-04-09 10:31:02 阅读量: 综合文库 文档下载
- 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 - 6e04-09
那一次我感动的哭了作文600字06-18
长江大学拟进行表彰的2017科研工作先进集体和先进个人 - 图文11-09
常识 马哲系列练习题 103-21
小学生对联大全02-10
小学科学四年级下册点亮我的小灯泡教学设计09-09
西式快餐加盟店的费用和利润01-28
word上机操作基础试题08-08
三年级语文上册课内阅读专项练习11-06
- 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
- 多层物业服务方案
- (审判实务)习惯法与少数民族地区民间纠纷解决问题(孙 潋)
- 人教版新课标六年级下册语文全册教案
- 词语打卡
- photoshop实习报告
- 钢结构设计原理综合测试2
- 2014年期末练习题
- 高中数学中的逆向思维解题方法探讨
- 名师原创 全国通用2014-2015学年高二寒假作业 政治(一)Word版
- 北航《建筑结构检测鉴定与加固》在线作业三
- XX县卫生监督所工程建设项目可行性研究报告
- 小学四年级观察作文经典评语
- 浅谈110KV变电站电气一次设计-程泉焱(1)
- 安全员考试题库
- 国家电网公司变电运维管理规定(试行)
- 义务教育课程标准稿征求意见提纲
- 教学秘书面试技巧
- 钢结构工程施工组织设计
- 水利工程概论论文
- 09届九年级数学第四次模拟试卷
- Chapter11
- 6e
- 行政事业单位会计制度讲解
- 天津医科大学总医院第一住院楼施组 - 图文
- 思修绪论练习题及答案
- 参考题库2013.7
- 促进青年员工成长推动企业健康发展
- 同花顺函数
- 屋面工程施工方案-K2 - 图文
- 2013年北京大学管理学相关专业考研真题(回忆版)
- 6 传热作业
- STM32L051电阻分压ADC电量采集
- 发酵罐设计
- 文件一:广晟公司项目创新简介
- 优化方案高考地理二轮复习第一部分专题突破篇二地表形态的塑造突
- 党政机关公文格式18要素解读
- 地球生命保护伞(社会资源整合) - 图文
- 最新北师大版 四年级数学上册第三单元 乘法教学设计与教学反思 -
- 2008年中考物理试题分类汇编—平衡力
- 锅炉安装质量管理体系2015(1)
- 计量经济学模型分析论文 影响我国人均GDP的变量因素分析
- 幼儿教育学考试复习资料