Deck 8: Operator Overloading,friends,and References

Full screen (f)
exit full mode
Question
You can change the behavior of + for the int type using operator overloading.
Use Space or
up arrow
down arrow
to flip the card.
Question
Overloading an operator cannot change the precedence of that operator with respect to other operators.
Question
When overloading an operator,you can create a new operator different from the more usual operators.
Question
Overloading a binary operator as a member requires two arguments.
Question
A friend function has access only to the private members and member functions of the class (and all objects of that class)of which it is a friend.
Question
Overloaded operator <<,when used as an output,returns an ostream& to allow chains of output statements,and similarly the overloaded operator >> returns an istream&
Question
Consider this operator overloading for class Money,modified from Display 8.1 by omitting the const modifier on the return type:
Money operator+(const Money& amt1,const Money& amt2);
Is the following expression legal? If legal,what could it mean,and where does the information that is assigned go?
Money m1(17.99),m2(23.57)m3(15,22);
(m1 + m2)= m3;
Question
When overloading an operator,you can change the behavior of the operator,making + do things that feel like multiplication,but this is unwise.
Question
An overloaded operator[] must be a non-static class member.
Question
If a unary operator is overloaded as a stand-alone function,the argument may be any type.
Question
An operator overloading is essentially a function that uses a different syntax for invocation.
Question
It is impossible to get short-circuit behavior for overloaded operator && or operator ||
Question
The operator prefix operator ++ is an l-value,and the text asserts that we should emulate this with operator overloads by returning a reference from prefix versions of operator++.
Question
When overloading an operator,you cannot change the number of arguments an operator takes.
Question
Overloading a binary operator as a stand-alone function requires two arguments.
Question
A class can have friends that are functions as well as friend classes.
Question
When an operator is overloaded as a member of a class,the first parameter listed in parentheses,is the calling object.
Question
C++ allows overloading of the function application operator ( ).Explain.
Question
An overloaded operator= function must be a non-static class member.
Question
One can guarantee left to right evaluation of the arguments to a comma expression overloading.
Question
Given the class definition:
class A
{
public:
//constructors
// other members
private:
int x;
int y;
};
Give declarations of operator functions for each of the following ways to overload operator + You must state where the declaration goes,whether within the class in the public or private section or outside the class.The operator + may be overloaded
a)as friend function
b)as member function
c)as non-friend,non-member function
Question
Why have we not needed to overload the assignment operator so far?
Question
Which operators cannot be overloaded?
Question
If I need to build an object for return from a function,I can construct that function directly in the return statement.
Question
Show how to overload the operators << and >> to create stream output for this class.
Make these functions friends of the class Pair.The expected form of a pair is (2,3)for both input and output.To make this problem manageable,you should only provide to accept and discard the parentheses and comma,but you should not check that these particular characters were typed.Output should be the expected form.
class IntPair
{
int first;
int second;
public:
IntPair(int firstValue,int secondValue);
int getFirst( )const;
int getSecond( )const;
};
Question
Which operators can be overloaded only as non-static class members?
Question
Explain the similarities and differences between (binary)operators such as +,*,-,/ and a function.
Question
What do you need to add to the class definition to overload operator < so that it applies to the type Money from Display 8.1? Given this extract from the class Money from Display 8.1 of the text.
class Money
{
public:
Money( );
// other constructors
// other public members
int getCents( )const;
int getDollars( )const;
private:
int dollars;
int cents;
// other private members
};
Question
What are some reasons for using friend operator overloading?
Question
What objections to the use of friend functions and classes do some experts give?
Question
Consider the class definition:
class IntPair
{
int first;
int second;
public:
IntPair(int firstValue,int secondValue);
// prefix operator++ here
// postfix operator ++ here
int getFirst( )const;
int getSecond( )const;
};
a)Give declarations for prefix and postfix versions of operator++
b)Give definitions for prefix and postfix versions of operator++
Unlock Deck
Sign up to unlock the cards in this deck!
Unlock Deck
Unlock Deck
1/31
auto play flashcards
Play
simple tutorial
Full screen (f)
exit full mode
Deck 8: Operator Overloading,friends,and References
1
You can change the behavior of + for the int type using operator overloading.
False
2
Overloading an operator cannot change the precedence of that operator with respect to other operators.
True
3
When overloading an operator,you can create a new operator different from the more usual operators.
False
4
Overloading a binary operator as a member requires two arguments.
Unlock Deck
Unlock for access to all 31 flashcards in this deck.
Unlock Deck
k this deck
5
A friend function has access only to the private members and member functions of the class (and all objects of that class)of which it is a friend.
Unlock Deck
Unlock for access to all 31 flashcards in this deck.
Unlock Deck
k this deck
6
Overloaded operator <<,when used as an output,returns an ostream& to allow chains of output statements,and similarly the overloaded operator >> returns an istream&
Unlock Deck
Unlock for access to all 31 flashcards in this deck.
Unlock Deck
k this deck
7
Consider this operator overloading for class Money,modified from Display 8.1 by omitting the const modifier on the return type:
Money operator+(const Money& amt1,const Money& amt2);
Is the following expression legal? If legal,what could it mean,and where does the information that is assigned go?
Money m1(17.99),m2(23.57)m3(15,22);
(m1 + m2)= m3;
Unlock Deck
Unlock for access to all 31 flashcards in this deck.
Unlock Deck
k this deck
8
When overloading an operator,you can change the behavior of the operator,making + do things that feel like multiplication,but this is unwise.
Unlock Deck
Unlock for access to all 31 flashcards in this deck.
Unlock Deck
k this deck
9
An overloaded operator[] must be a non-static class member.
Unlock Deck
Unlock for access to all 31 flashcards in this deck.
Unlock Deck
k this deck
10
If a unary operator is overloaded as a stand-alone function,the argument may be any type.
Unlock Deck
Unlock for access to all 31 flashcards in this deck.
Unlock Deck
k this deck
11
An operator overloading is essentially a function that uses a different syntax for invocation.
Unlock Deck
Unlock for access to all 31 flashcards in this deck.
Unlock Deck
k this deck
12
It is impossible to get short-circuit behavior for overloaded operator && or operator ||
Unlock Deck
Unlock for access to all 31 flashcards in this deck.
Unlock Deck
k this deck
13
The operator prefix operator ++ is an l-value,and the text asserts that we should emulate this with operator overloads by returning a reference from prefix versions of operator++.
Unlock Deck
Unlock for access to all 31 flashcards in this deck.
Unlock Deck
k this deck
14
When overloading an operator,you cannot change the number of arguments an operator takes.
Unlock Deck
Unlock for access to all 31 flashcards in this deck.
Unlock Deck
k this deck
15
Overloading a binary operator as a stand-alone function requires two arguments.
Unlock Deck
Unlock for access to all 31 flashcards in this deck.
Unlock Deck
k this deck
16
A class can have friends that are functions as well as friend classes.
Unlock Deck
Unlock for access to all 31 flashcards in this deck.
Unlock Deck
k this deck
17
When an operator is overloaded as a member of a class,the first parameter listed in parentheses,is the calling object.
Unlock Deck
Unlock for access to all 31 flashcards in this deck.
Unlock Deck
k this deck
18
C++ allows overloading of the function application operator ( ).Explain.
Unlock Deck
Unlock for access to all 31 flashcards in this deck.
Unlock Deck
k this deck
19
An overloaded operator= function must be a non-static class member.
Unlock Deck
Unlock for access to all 31 flashcards in this deck.
Unlock Deck
k this deck
20
One can guarantee left to right evaluation of the arguments to a comma expression overloading.
Unlock Deck
Unlock for access to all 31 flashcards in this deck.
Unlock Deck
k this deck
21
Given the class definition:
class A
{
public:
//constructors
// other members
private:
int x;
int y;
};
Give declarations of operator functions for each of the following ways to overload operator + You must state where the declaration goes,whether within the class in the public or private section or outside the class.The operator + may be overloaded
a)as friend function
b)as member function
c)as non-friend,non-member function
Unlock Deck
Unlock for access to all 31 flashcards in this deck.
Unlock Deck
k this deck
22
Why have we not needed to overload the assignment operator so far?
Unlock Deck
Unlock for access to all 31 flashcards in this deck.
Unlock Deck
k this deck
23
Which operators cannot be overloaded?
Unlock Deck
Unlock for access to all 31 flashcards in this deck.
Unlock Deck
k this deck
24
If I need to build an object for return from a function,I can construct that function directly in the return statement.
Unlock Deck
Unlock for access to all 31 flashcards in this deck.
Unlock Deck
k this deck
25
Show how to overload the operators << and >> to create stream output for this class.
Make these functions friends of the class Pair.The expected form of a pair is (2,3)for both input and output.To make this problem manageable,you should only provide to accept and discard the parentheses and comma,but you should not check that these particular characters were typed.Output should be the expected form.
class IntPair
{
int first;
int second;
public:
IntPair(int firstValue,int secondValue);
int getFirst( )const;
int getSecond( )const;
};
Unlock Deck
Unlock for access to all 31 flashcards in this deck.
Unlock Deck
k this deck
26
Which operators can be overloaded only as non-static class members?
Unlock Deck
Unlock for access to all 31 flashcards in this deck.
Unlock Deck
k this deck
27
Explain the similarities and differences between (binary)operators such as +,*,-,/ and a function.
Unlock Deck
Unlock for access to all 31 flashcards in this deck.
Unlock Deck
k this deck
28
What do you need to add to the class definition to overload operator < so that it applies to the type Money from Display 8.1? Given this extract from the class Money from Display 8.1 of the text.
class Money
{
public:
Money( );
// other constructors
// other public members
int getCents( )const;
int getDollars( )const;
private:
int dollars;
int cents;
// other private members
};
Unlock Deck
Unlock for access to all 31 flashcards in this deck.
Unlock Deck
k this deck
29
What are some reasons for using friend operator overloading?
Unlock Deck
Unlock for access to all 31 flashcards in this deck.
Unlock Deck
k this deck
30
What objections to the use of friend functions and classes do some experts give?
Unlock Deck
Unlock for access to all 31 flashcards in this deck.
Unlock Deck
k this deck
31
Consider the class definition:
class IntPair
{
int first;
int second;
public:
IntPair(int firstValue,int secondValue);
// prefix operator++ here
// postfix operator ++ here
int getFirst( )const;
int getSecond( )const;
};
a)Give declarations for prefix and postfix versions of operator++
b)Give definitions for prefix and postfix versions of operator++
Unlock Deck
Unlock for access to all 31 flashcards in this deck.
Unlock Deck
k this deck
locked card icon
Unlock Deck
Unlock for access to all 31 flashcards in this deck.