Deck 11: Friends, overloaded Operators, and Arrays in Classes
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Unlock Deck
Sign up to unlock the cards in this deck!
Unlock Deck
Unlock Deck
1/49
Play
Full screen (f)
Deck 11: Friends, overloaded Operators, and Arrays in Classes
1
If a given task being performed by a function involves more than one object,then that function should normally be a __________ function.
friend
2
A _________ function is not a member of the class,but has access to the private members of the class.
friend
3
An operator that expects only one parameter is called a ________ operator
unary
4
Friend functions may directly modify or access the private data members.
Unlock Deck
Unlock for access to all 49 flashcards in this deck.
Unlock Deck
k this deck
5
If you have mutators and accessors,you should not have friend functions also
Unlock Deck
Unlock for access to all 49 flashcards in this deck.
Unlock Deck
k this deck
6
You may not change the precedence of operators by overloading them
Unlock Deck
Unlock for access to all 49 flashcards in this deck.
Unlock Deck
k this deck
7
An overloaded extraction or insertion operator should return ___________
Unlock Deck
Unlock for access to all 49 flashcards in this deck.
Unlock Deck
k this deck
8
Putting the keyword const after the function declaration guarantees __________________________
Unlock Deck
Unlock for access to all 49 flashcards in this deck.
Unlock Deck
k this deck
9
All operators can be overloaded.
Unlock Deck
Unlock for access to all 49 flashcards in this deck.
Unlock Deck
k this deck
10
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 _______________
Unlock Deck
Unlock for access to all 49 flashcards in this deck.
Unlock Deck
k this deck
11
If a given task being performed by a function involves one object,then that function should normally be a __________ function.
Unlock Deck
Unlock for access to all 49 flashcards in this deck.
Unlock Deck
k this deck
12
Putting the keyword const in front of a pass by reference parameter guarantees ___________________
Unlock Deck
Unlock for access to all 49 flashcards in this deck.
Unlock Deck
k this deck
13
Functions that are constant member functions may call constant class accessor functions.
Unlock Deck
Unlock for access to all 49 flashcards in this deck.
Unlock Deck
k this deck
14
Friend functions are members of the class.
Unlock Deck
Unlock for access to all 49 flashcards in this deck.
Unlock Deck
k this deck
15
Operators must be friends of the class.
Unlock Deck
Unlock for access to all 49 flashcards in this deck.
Unlock Deck
k this deck
16
The following is a properly declared overloaded insertion operator for myClass.
ostream& operator <
ostream& operator <
Unlock Deck
Unlock for access to all 49 flashcards in this deck.
Unlock Deck
k this deck
17
You cannot create new operators such as the quote).
Unlock Deck
Unlock for access to all 49 flashcards in this deck.
Unlock Deck
k this deck
18
Functions that are constant member functions may call the class mutator functions.
Unlock Deck
Unlock for access to all 49 flashcards in this deck.
Unlock Deck
k this deck
19
An operator that expects two parameters is called a ________ operator.
Unlock Deck
Unlock for access to all 49 flashcards in this deck.
Unlock Deck
k this deck
20
In order to do automatic type conversion for your class,you would write _________
Unlock Deck
Unlock for access to all 49 flashcards in this deck.
Unlock Deck
k this deck
21
Which of the following function declarations would be correct to overload the multiply operator for the Rational numbers class?
A)friend Rational operator timesconst Rational &left,const Rational &right);
B)Rational operator timesconst Rational &left,const Rational &right);
C)friend Rational operator *const Rational &left,const Rational &right);
D)Rational operator *const Rational &left,const Rational &right);
A)friend Rational operator timesconst Rational &left,const Rational &right);
B)Rational operator timesconst Rational &left,const Rational &right);
C)friend Rational operator *const Rational &left,const Rational &right);
D)Rational operator *const Rational &left,const Rational &right);
Unlock Deck
Unlock for access to all 49 flashcards in this deck.
Unlock Deck
k this deck
22
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
A)friends of a class
B)members of a class
C)non-friends,non-members of a class
D)All of the above
Unlock Deck
Unlock for access to all 49 flashcards in this deck.
Unlock Deck
k this deck
23
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
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
Unlock Deck
Unlock for access to all 49 flashcards in this deck.
Unlock Deck
k this deck
24
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
A)Overloaded constructors
B)Converters
C)This can not be done
D)This already happens automatically
Unlock Deck
Unlock for access to all 49 flashcards in this deck.
Unlock Deck
k this deck
25
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
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
Unlock Deck
Unlock for access to all 49 flashcards in this deck.
Unlock Deck
k this deck
26
Write the function declaration for an assignment operator for a class named myClass
Unlock Deck
Unlock for access to all 49 flashcards in this deck.
Unlock Deck
k this deck
27
Since accessor 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
A)reference
B)friend
C)const
D)private
Unlock Deck
Unlock for access to all 49 flashcards in this deck.
Unlock Deck
k this deck
28
Which of the following operators can not be overloaded?
A)=
B)==
C).
D)[]
A)=
B)==
C).
D)[]
Unlock Deck
Unlock for access to all 49 flashcards in this deck.
Unlock Deck
k this deck
29
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
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
Unlock Deck
Unlock for access to all 49 flashcards in this deck.
Unlock Deck
k this deck
30
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 members).
B)If the friend function will not change the values of the data members).
C)It is more efficient to pass the object by reference.
D)A and B
E)A and C
A)If the friend function changes the values of the data members).
B)If the friend function will not change the values of the data members).
C)It is more efficient to pass the object by reference.
D)A and B
E)A and C
Unlock Deck
Unlock for access to all 49 flashcards in this deck.
Unlock Deck
k this deck
31
The assignment operator must be a _________ of the class.
Unlock Deck
Unlock for access to all 49 flashcards in this deck.
Unlock Deck
k this deck
32
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
A)members of the class
B)friends of the class
C)everyone
D)B and C
E)no one
Unlock Deck
Unlock for access to all 49 flashcards in this deck.
Unlock Deck
k this deck
33
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.
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.
Unlock Deck
Unlock for access to all 49 flashcards in this deck.
Unlock Deck
k this deck
34
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
A)the stream
B)the object
C)it doesn't matter
D)none of the above
Unlock Deck
Unlock for access to all 49 flashcards in this deck.
Unlock Deck
k this deck
35
Write the function declaration for a copy constructor for a class named myClass
Unlock Deck
Unlock for access to all 49 flashcards in this deck.
Unlock Deck
k this deck
36
Given the following function declaration,
Friend void displayconst myClass& object);
Which is the correct header for the definition of the function?
A)void myClass::displayconst myClass& object)
B)void displayconst myClass& object)
C)friend void displayconst myClass& object);
D)friend void displayconst myClass& object)
Friend void displayconst myClass& object);
Which is the correct header for the definition of the function?
A)void myClass::displayconst myClass& object)
B)void displayconst myClass& object)
C)friend void displayconst myClass& object);
D)friend void displayconst myClass& object)
Unlock Deck
Unlock for access to all 49 flashcards in this deck.
Unlock Deck
k this deck
37
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
String s1,s2;
If s1 < s2 )
A)s1
B)s2
C)<
D)none
Unlock Deck
Unlock for access to all 49 flashcards in this deck.
Unlock Deck
k this deck
38
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);
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);
Unlock Deck
Unlock for access to all 49 flashcards in this deck.
Unlock Deck
k this deck
39
Both the copy constructor and the assignment operator should make ___________
Unlock Deck
Unlock for access to all 49 flashcards in this deck.
Unlock Deck
k this deck
40
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
A)0
B)1
C)2
D)as many as you need
Unlock Deck
Unlock for access to all 49 flashcards in this deck.
Unlock Deck
k this deck
41
When a dynamic array with a class for a base type is declared,which constructor is called?
A)the copy constructor
B)the destructor
C)the default constructor
D)an explicit constructor
A)the copy constructor
B)the destructor
C)the default constructor
D)an explicit constructor
Unlock Deck
Unlock for access to all 49 flashcards in this deck.
Unlock Deck
k this deck
42
If obj1 and obj2 are both objects of a class that uses dynamic memory allocation,but the class does not have an assignment operator,what happens if you execute the following code?
Obj1=obj2;
A)A syntax error occurs,you can not assign one object to another object without the = operator
B)A run-time error occurs,because the C++ system does not know how to do the assignment.
C)The pointers)to the dynamically declared memory in obj2 are copied to the corresponding pointers in obj1.
D)There is a complete and independent copy of all the dynamic memory from obj2 to obj1
Obj1=obj2;
A)A syntax error occurs,you can not assign one object to another object without the = operator
B)A run-time error occurs,because the C++ system does not know how to do the assignment.
C)The pointers)to the dynamically declared memory in obj2 are copied to the corresponding pointers in obj1.
D)There is a complete and independent copy of all the dynamic memory from obj2 to obj1
Unlock Deck
Unlock for access to all 49 flashcards in this deck.
Unlock Deck
k this deck
43
The copy constructor for a class is called
A)when an object of the class is passed by value to a function.
B)when an object of the class is initialized by another object of the class
C)when a function returns an object of the class
D)all of the above
A)when an object of the class is passed by value to a function.
B)when an object of the class is initialized by another object of the class
C)when a function returns an object of the class
D)all of the above
Unlock Deck
Unlock for access to all 49 flashcards in this deck.
Unlock Deck
k this deck
44
Which of the following statements are true?
A)Array elements may be user-defined types structs or classes)
B)If a function is expecting a variable of the base type of the array,then you can pass an element of the array to the function.
C)All of the above.
D)None of the above.
A)Array elements may be user-defined types structs or classes)
B)If a function is expecting a variable of the base type of the array,then you can pass an element of the array to the function.
C)All of the above.
D)None of the above.
Unlock Deck
Unlock for access to all 49 flashcards in this deck.
Unlock Deck
k this deck
45
Which of the following are valid declarations for an assignment operator for a class named myClass?
A)void friend operator = myClass& left,const myClass& source);
B)void operator = myClass& left,const myClass& source);
C)void friend operator = const myClass& source);
D)void operator = const myClass& source);
A)void friend operator = myClass& left,const myClass& source);
B)void operator = myClass& left,const myClass& source);
C)void friend operator = const myClass& source);
D)void operator = const myClass& source);
Unlock Deck
Unlock for access to all 49 flashcards in this deck.
Unlock Deck
k this deck
46
Which of the following are not correct?
A)The destructor of a class is not named the same as the name of the class,but preceded with a tilde
B)The destructor of a class is not called when an object of the class goes out of scope
C)The destructor of a class is not a member of the class
D)The destructor of a class is a void function
E)all of the above
A)The destructor of a class is not named the same as the name of the class,but preceded with a tilde
B)The destructor of a class is not called when an object of the class goes out of scope
C)The destructor of a class is not a member of the class
D)The destructor of a class is a void function
E)all of the above
Unlock Deck
Unlock for access to all 49 flashcards in this deck.
Unlock Deck
k this deck
47
Which of the following statements are true?
A)A dynamic array can have a base type which is a class or a struct.
B)A class or a struct may have a member which is a dynamic array.
C)A and B
D)Neither
A)A dynamic array can have a base type which is a class or a struct.
B)A class or a struct may have a member which is a dynamic array.
C)A and B
D)Neither
Unlock Deck
Unlock for access to all 49 flashcards in this deck.
Unlock Deck
k this deck
48
The destructor for a class is called
A)explicitly from the main program
B)when the class is instantiated
C)when the object of the class goes out of scope
D)Only at the end of main
A)explicitly from the main program
B)when the class is instantiated
C)when the object of the class goes out of scope
D)Only at the end of main
Unlock Deck
Unlock for access to all 49 flashcards in this deck.
Unlock Deck
k this deck
49
What happens when you define a class that used dynamic memory allocation and define a destructor but no copy constructor?
A)If an object of the class is plugged in for a call-by-value parameter,when the function ends,the parameter's dynamic memory is returned to the freestore at the end of the function execution.
B)When an object that was used as an argument for a call-by-value parameter goes out of scope,it will cause a run-time error.
C)It is possible to modify the values in the argument in the function.
D)All of the above
E)None of the above
A)If an object of the class is plugged in for a call-by-value parameter,when the function ends,the parameter's dynamic memory is returned to the freestore at the end of the function execution.
B)When an object that was used as an argument for a call-by-value parameter goes out of scope,it will cause a run-time error.
C)It is possible to modify the values in the argument in the function.
D)All of the above
E)None of the above
Unlock Deck
Unlock for access to all 49 flashcards in this deck.
Unlock Deck
k this deck