Deck 11: Friends, Overloaded Operators, and Arrays in Classes

Full screen (f)
exit full mode
Question
Operators must be friends of the class.
Use Space or
up arrow
down arrow
to flip the card.
Question
Friend functions may directly modify or access the private data members.
Question
Functions that are constant member functions may call constant class accessor functions.
Question
Putting the keyword const after the function declaration guarantees __________________________
Question
If you have mutators and accessors, you should not have friend functions also
Question
If a given task being performed by a function involves one object, then that function should normally be a __________ function.
Question
Functions that are constant member functions may call the class mutator functions.
Question
You cannot create new operators such as the quote).
Question
In order to do automatic type conversion for your class, you would write _________
Question
An operator that expects two parameters is called a ________ operator.
Question
An overloaded extraction or insertion operator should return ___________
Question
Putting the keyword const in front of a pass by reference parameter guarantees ___________________
Question
The following is a properly declared overloaded insertion operator for myClass.
ostream& operator <
Question
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 _______________
Question
You may not change the precedence of operators by overloading them
Question
All operators can be overloaded.
Question
If a given task being performed by a function involves more than one object, then that function should normally be a __________ function.
Question
An operator that expects only one parameter is called a ________ operator
Question
A _________ function is not a member of the class, but has access to the private members of the class.
Question
Friend functions are members of the class.
Question
Write the function declaration for a copy constructor for a class named myClass
Question
If c is a character variable that contains a digit, what does the following function return?
Int digit_to_intchar c)
{
Return intc) - int'0'));
}

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
Question
Write the function declaration for an assignment operator for a class named myClass
Question
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
Question
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);
Question
To overload functions with symbolic names like + - / <<), you must use the keyword _______ before the symbolic name.

A) const
B) operator
C) reference
D) void
Question
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
Question
Given the following class, which is the correct function header for the display function?
Class Rational
{
Public:
Rational);
Rationalint numer, int denom);
Rationalint whole);
Int getNumerator);
Int getDenominator);
Friend void displayostream& out, const Rational& value);
Private:
Int numerator;
Int denominator;
};

A) friend void displayostream& out, const Rational& value)
B) void displayostream& out, const Rational& value)
C) void Rational::displayostream& out, const Rational& value)
D) friend void Rational::displayostream& out, const Rational& value)
Question
The assignment operator must be a _________ of the class.
Question
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
Question
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
Question
How many members data and functions) does the following class have?
Class Rational
{
Public:
Rational);
Rationalint numer, int denom);
Rationalint whole);
Int getNumerator);
Int getDenominator);
Friend void displayostream& out, const Rational& value);
Private:
Int numerator;
Int denominator;
};

A) 2
B) 6
C) 5
D) 7
E) 8
Question
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
Question
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
Question
Write the function declaration for a destructor for a class named myClass
Question
Both the copy constructor and the assignment operator should make ___________
Question
Given the following class, what is syntactically wrong with the implementation of the display function?
Class Rational
{
Public:
Rational);
Rationalint numer, int denom);
Rationalint whole);
Int getNumerator);
Int getDenominator);
Friend void displayostream& out, const Rational& value);
Private:
Int numerator;
Int denominator;
};
Void displayostream& 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
Question
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
Question
How many parameters are there in a unary operator implemented as a friend?

A) 0
B) 1
C) 2
D) as many as you need
Question
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
Question
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
Question
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)
Question
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
Question
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
Question
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
Question
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
Question
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
Question
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.
Question
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);
Question
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
Question
Which of the following operators can not be overloaded?

A) =
B) ==
C) .
D) []
Question
Given the following class and array declaration, how would you print out the age of the 10th person in the array?
Class personClass
{
Public:
Void setAgeint newAge);
Void setGender char newGender);
Void setSalaryfloat newSalary);
Int getAge);
Char getGender);
Float getSalary);
Private:
Int age;
Char gender;
Float salary;
};
PersonClass people[100];

A) cout << people[10];
B) cout << people[9];
C) cout << people[9].age;
D) cout << people[9].getAge);
Question
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.
Question
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);
Question
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
Question
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
Unlock Deck
Sign up to unlock the cards in this deck!
Unlock Deck
Unlock Deck
1/56
auto play flashcards
Play
simple tutorial
Full screen (f)
exit full mode
Deck 11: Friends, Overloaded Operators, and Arrays in Classes
1
Operators must be friends of the class.
False
2
Friend functions may directly modify or access the private data members.
True
3
Functions that are constant member functions may call constant class accessor functions.
True
4
Putting the keyword const after the function declaration guarantees __________________________
Unlock Deck
Unlock for access to all 56 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 56 flashcards in this deck.
Unlock Deck
k this deck
6
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 56 flashcards in this deck.
Unlock Deck
k this deck
7
Functions that are constant member functions may call the class mutator functions.
Unlock Deck
Unlock for access to all 56 flashcards in this deck.
Unlock Deck
k this deck
8
You cannot create new operators such as the quote).
Unlock Deck
Unlock for access to all 56 flashcards in this deck.
Unlock Deck
k this deck
9
In order to do automatic type conversion for your class, you would write _________
Unlock Deck
Unlock for access to all 56 flashcards in this deck.
Unlock Deck
k this deck
10
An operator that expects two parameters is called a ________ operator.
Unlock Deck
Unlock for access to all 56 flashcards in this deck.
Unlock Deck
k this deck
11
An overloaded extraction or insertion operator should return ___________
Unlock Deck
Unlock for access to all 56 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 56 flashcards in this deck.
Unlock Deck
k this deck
13
The following is a properly declared overloaded insertion operator for myClass.
ostream& operator <
Unlock Deck
Unlock for access to all 56 flashcards in this deck.
Unlock Deck
k this deck
14
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 56 flashcards in this deck.
Unlock Deck
k this deck
15
You may not change the precedence of operators by overloading them
Unlock Deck
Unlock for access to all 56 flashcards in this deck.
Unlock Deck
k this deck
16
All operators can be overloaded.
Unlock Deck
Unlock for access to all 56 flashcards in this deck.
Unlock Deck
k this deck
17
If a given task being performed by a function involves more than one object, then that function should normally be a __________ function.
Unlock Deck
Unlock for access to all 56 flashcards in this deck.
Unlock Deck
k this deck
18
An operator that expects only one parameter is called a ________ operator
Unlock Deck
Unlock for access to all 56 flashcards in this deck.
Unlock Deck
k this deck
19
A _________ function is not a member of the class, but has access to the private members of the class.
Unlock Deck
Unlock for access to all 56 flashcards in this deck.
Unlock Deck
k this deck
20
Friend functions are members of the class.
Unlock Deck
Unlock for access to all 56 flashcards in this deck.
Unlock Deck
k this deck
21
Write the function declaration for a copy constructor for a class named myClass
Unlock Deck
Unlock for access to all 56 flashcards in this deck.
Unlock Deck
k this deck
22
If c is a character variable that contains a digit, what does the following function return?
Int digit_to_intchar c)
{
Return intc) - int'0'));
}

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
Unlock Deck
Unlock for access to all 56 flashcards in this deck.
Unlock Deck
k this deck
23
Write the function declaration for an assignment operator for a class named myClass
Unlock Deck
Unlock for access to all 56 flashcards in this deck.
Unlock Deck
k this deck
24
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
Unlock Deck
Unlock for access to all 56 flashcards in this deck.
Unlock Deck
k this deck
25
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);
Unlock Deck
Unlock for access to all 56 flashcards in this deck.
Unlock Deck
k this deck
26
To overload functions with symbolic names like + - / <<), you must use the keyword _______ before the symbolic name.

A) const
B) operator
C) reference
D) void
Unlock Deck
Unlock for access to all 56 flashcards in this deck.
Unlock Deck
k this deck
27
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
Unlock Deck
Unlock for access to all 56 flashcards in this deck.
Unlock Deck
k this deck
28
Given the following class, which is the correct function header for the display function?
Class Rational
{
Public:
Rational);
Rationalint numer, int denom);
Rationalint whole);
Int getNumerator);
Int getDenominator);
Friend void displayostream& out, const Rational& value);
Private:
Int numerator;
Int denominator;
};

A) friend void displayostream& out, const Rational& value)
B) void displayostream& out, const Rational& value)
C) void Rational::displayostream& out, const Rational& value)
D) friend void Rational::displayostream& out, const Rational& value)
Unlock Deck
Unlock for access to all 56 flashcards in this deck.
Unlock Deck
k this deck
29
The assignment operator must be a _________ of the class.
Unlock Deck
Unlock for access to all 56 flashcards in this deck.
Unlock Deck
k this deck
30
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
Unlock Deck
Unlock for access to all 56 flashcards in this deck.
Unlock Deck
k this deck
31
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
Unlock Deck
Unlock for access to all 56 flashcards in this deck.
Unlock Deck
k this deck
32
How many members data and functions) does the following class have?
Class Rational
{
Public:
Rational);
Rationalint numer, int denom);
Rationalint whole);
Int getNumerator);
Int getDenominator);
Friend void displayostream& out, const Rational& value);
Private:
Int numerator;
Int denominator;
};

A) 2
B) 6
C) 5
D) 7
E) 8
Unlock Deck
Unlock for access to all 56 flashcards in this deck.
Unlock Deck
k this deck
33
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
Unlock Deck
Unlock for access to all 56 flashcards in this deck.
Unlock Deck
k this deck
34
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
Unlock Deck
Unlock for access to all 56 flashcards in this deck.
Unlock Deck
k this deck
35
Write the function declaration for a destructor for a class named myClass
Unlock Deck
Unlock for access to all 56 flashcards in this deck.
Unlock Deck
k this deck
36
Both the copy constructor and the assignment operator should make ___________
Unlock Deck
Unlock for access to all 56 flashcards in this deck.
Unlock Deck
k this deck
37
Given the following class, what is syntactically wrong with the implementation of the display function?
Class Rational
{
Public:
Rational);
Rationalint numer, int denom);
Rationalint whole);
Int getNumerator);
Int getDenominator);
Friend void displayostream& out, const Rational& value);
Private:
Int numerator;
Int denominator;
};
Void displayostream& 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
Unlock Deck
Unlock for access to all 56 flashcards in this deck.
Unlock Deck
k this deck
38
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
Unlock Deck
Unlock for access to all 56 flashcards in this deck.
Unlock Deck
k this deck
39
How many parameters are there in a unary operator implemented as a friend?

A) 0
B) 1
C) 2
D) as many as you need
Unlock Deck
Unlock for access to all 56 flashcards in this deck.
Unlock Deck
k this deck
40
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
Unlock Deck
Unlock for access to all 56 flashcards in this deck.
Unlock Deck
k this deck
41
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
Unlock Deck
Unlock for access to all 56 flashcards in this deck.
Unlock Deck
k this deck
42
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)
Unlock Deck
Unlock for access to all 56 flashcards in this deck.
Unlock Deck
k this deck
43
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
Unlock Deck
Unlock for access to all 56 flashcards in this deck.
Unlock Deck
k this deck
44
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
Unlock Deck
Unlock for access to all 56 flashcards in this deck.
Unlock Deck
k this deck
45
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
Unlock Deck
Unlock for access to all 56 flashcards in this deck.
Unlock Deck
k this deck
46
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
Unlock Deck
Unlock for access to all 56 flashcards in this deck.
Unlock Deck
k this deck
47
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
Unlock Deck
Unlock for access to all 56 flashcards in this deck.
Unlock Deck
k this deck
48
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.
Unlock Deck
Unlock for access to all 56 flashcards in this deck.
Unlock Deck
k this deck
49
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);
Unlock Deck
Unlock for access to all 56 flashcards in this deck.
Unlock Deck
k this deck
50
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
Unlock Deck
Unlock for access to all 56 flashcards in this deck.
Unlock Deck
k this deck
51
Which of the following operators can not be overloaded?

A) =
B) ==
C) .
D) []
Unlock Deck
Unlock for access to all 56 flashcards in this deck.
Unlock Deck
k this deck
52
Given the following class and array declaration, how would you print out the age of the 10th person in the array?
Class personClass
{
Public:
Void setAgeint newAge);
Void setGender char newGender);
Void setSalaryfloat newSalary);
Int getAge);
Char getGender);
Float getSalary);
Private:
Int age;
Char gender;
Float salary;
};
PersonClass people[100];

A) cout << people[10];
B) cout << people[9];
C) cout << people[9].age;
D) cout << people[9].getAge);
Unlock Deck
Unlock for access to all 56 flashcards in this deck.
Unlock Deck
k this deck
53
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.
Unlock Deck
Unlock for access to all 56 flashcards in this deck.
Unlock Deck
k this deck
54
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);
Unlock Deck
Unlock for access to all 56 flashcards in this deck.
Unlock Deck
k this deck
55
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
Unlock Deck
Unlock for access to all 56 flashcards in this deck.
Unlock Deck
k this deck
56
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
Unlock Deck
Unlock for access to all 56 flashcards in this deck.
Unlock Deck
k this deck
locked card icon
Unlock Deck
Unlock for access to all 56 flashcards in this deck.