1. When overloading operators, give reasons for choosing member functions or fri
ID: 3677436 • Letter: 1
Question
1. When overloading operators, give reasons for choosing member
functions or friend functions.
2. When the stream input and output operators are overloaded to work with user-defined classes, are these operator functions usually members or friends? Why?
3. Is it possible using operator overloading to change the behavior of + on integers? Why or why not?
4. What restrictions apply to overloading the following operators?
=, (), [], ->
5. Why can we not overload << or >> as member operators?
Explanation / Answer
1. When overloading operators, give reasons for choosing member functions or friend functions.
Answer:
When you define operator overloading functions you can use member functions or friend functions.
a) member functions are advised not to use when the function takes two arguments of the class and returns the object of the class.
b) friend functions are used when the function needs to access private data members of the class.
c) Frined functions are also used when you are overloading stream I/O operators because operands are not from the user defined class.
2. When the stream input and output operators are overloaded to work with user-defined classes, are these operator functions usually members or friends? Why?
Answer:
Usually friend functions are used.
Frined functions are used when you are overloading stream I/O operators because operands are not from the user defined class i.e. left operand is not an object of the class.
3. Is it possible using operator overloading to change the behavior of + on integers? Why or why not?
Answer:
why:
You can not change how integers are added because you can not redefine the meaning of the operator.
why not:
Because operands of the + operator are not user defined.
4. What restrictions apply to overloading the following operators?
=, (), [], ->
Answer:
= The copy assignment between arrays of the same size can avoid allocation and deallocation if the class owns a heap allocated array.
5. Why can we not overload << or >> as member operators?
Answer:
Friend functions are used to overload << and >> operators..
Frined functions are used when you are overloading << >> operators because operands are not from the user defined class i.e. left operand is not an object of the class.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.