Chapter11_6e友元.doc_第1页
Chapter11_6e友元.doc_第2页
Chapter11_6e友元.doc_第3页
Chapter11_6e友元.doc_第4页
Chapter11_6e友元.doc_第5页
已阅读5页,还剩3页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

Test Bank for Problem Solving with C+: The Object of Programming, 6/e Chapter 11 Friends and Overloaded OperatorsTRUE/FALSE1. Friend functions are members of the class.ANSWER: FALSE2. All operators can be overloaded.ANSWER: FALSE3. If you have mutators and accessors, you should not have friend functions alsoANSWER: FALSE4. Friend functions may directly modify or access the private data members.ANSWER: TRUE5. The following is a properly declared overloaded insertion operator for myClass.ostream& operator (ostream &out, const myClass &obj);ANSWER: TRUE6. Functions that are constant member functions may call the class mutator functions.ANSWER: FALSE7. Functions that are constant member functions may call constant class accessor functions.ANSWER: TRUE8. You cannot create new operators (such as the quote).ANSWER: TRUE9. Operators must be friends of the class.ANSWER: FALSE10. You may not change the precedence of operators by overloading themANSWER: TRUEShort Answer1. If a given task being performed by a function involves more than one object, then that function should normally be a _ function.ANSWER: friend2. If a given task being performed by a function involves one object, then that function should normally be a _ function.ANSWER: member3. A _ function is not a member of the class, but has access to the private members of the class.ANSWER: friend4. An overloaded extraction or insertion operator should return _ANSWER: a reference to the stream5. 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 reference6. An operator that expects only one parameter is called a _ operatorANSWER: unary7. An operator that expects two parameters is called a _ operator.ANSWER: binary8. In order to do automatic type conversion for your class, you would write _ANSWER: overloaded functions or overloaded constructors9. 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 Choice1. How many members (data and functions) does the following class have?class Rationalpublic: 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. 2b. 6c. 5d. 7e. 8ANSWER: D2. Who can access private data in a class?a. members of the classb. friends of the classc. everyoned. B and C A and Be. no oneANSWER: D3. Given the following class, which is the correct function header for the display function?class Rationalpublic: 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. 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: B4. Operators can be overloaded asa. friends of a classb. members of a classc. non-friends, non-members of a classd. All of the aboveANSWER: D5. If we have a full selection of accessor and mutator functions, why would we have friend functions?a. You should not have themb. More efficient access to the private data members.c. The friend function must call the accessor or mutator functions anyway.d. none of the aboveANSWER: B6. Since accessors functions in a class do not modify or mutate the data members of the object, the function should have the _ modifier.a. referenceb. friendc. constd. privateANSWER: C7. 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 Be. A and CANSWER: E8. 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);a. The ASCII value of cb. The character value of cc. The integer equivalent of the digit stored in cd. none of the aboveANSWER: C9. What is wrong with the following member function definition given the class below?class Rationalpublic: 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() constnumerator = 0;return numerator;a. You can not set the numerator to zerob. The function may not modify numerator, but it can modify denominatorc. The function may not modify any of the private data membersd. nothinge. A and Bf. A and CANSWER: F10. Given the following class, what is syntactically wrong with the implementation of the display function?class Rationalpublic: 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;void display(ostream& out, const Rational& value)out value.getNumerator() /value.getDenominator();a. nothingb. value must be not be pass by referencec. The get functions are not const functionsd. out should be pass by valueANSWER: C11. To overload functions with symbolic names (like + - / ), you must use the keyword _ before the symbolic name.a. constb. operatorc. referenced. voidANSWER: B12. In the following code fragment, which is the calling object for the less-than operator?string s1, s2;if( s1 s2 )a. s1b. s2c. d. noneANSWER: A13. Given the following class declaration, class Rationalpublic: 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);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 (istream& in, const myClass &object);a. Object should not be a pass by reference parameterb. Object should not be a const parameterc. You can not put the & on the return typed. nothingANSWER: B16. 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 C?17. How many parameters are there in a binary operator implemented as a friend?a. 0b. 1c. 2d. as many as you needANSWER: C18. How many parameters are there in a unary operator implemented as a friend?a. 0b. 1c. 2d. as many as you needANSWER: B19. 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: B20. 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: C21. 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 dont, they could be membersc. Because they return a referenced. Because the stream is passed by reference.ANSWER: A22. If you want to be able to

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论