Study Guide 11: Overloaded Operators

  1. How many explicit arguments are required in the definition of an overloaded unary operator defined as a member (i.e., not as a friend) function?
    1. 0
    2. 1
    3. 2
    4. 3
  2. How many explicit arguments are required in the definition of an overloaded binary operator defined as a member (i.e., not as a friend) function?
    1. 0
    2. 1
    3. 2
    4. 3
  3. How many explicit arguments are required in the definition of an overloaded unary operator defined as a friend (i.e., not as a member) function?
    1. 0
    2. 1
    3. 2
    4. 3
  4. How many explicit arguments are required in the definition of an overloaded binary operator defined as a friend (i.e., not as a member) function?
    1. 0
    2. 1
    3. 2
    4. 3
  5. How many implicit arguments are required for an overloaded operator implemented as a member function?
    1. 0
    2. 1
    3. 2
    4. 3
  6. How many implicit arguments are required for an overloaded operator implemented as a friend function?
    1. 0
    2. 1
    3. 2
    4. 3
  7. Assume that obj1, obj2, and obj3 are instances of a class C. Also assume that the questions is about a member (i.e., a non-friend) function. For the statement
    obj3 = obj1 - obj2;
    to work correctly, the overloaded "-" operator must (choose all that apply)
    1. take two explicit arguments
    2. return a value
    3. create a named temporary object
    4. have one explicit argument of type C
  8. To convert from a user-defined class to a basic type, you would most likely use
    1. a built-in conversion operator.
    2. a one-argument constructor (aka a conversion constructor).
    3. an overloaded = operator.
    4. a conversion operator that's a member of the class.
  9. To convert from a basic type to a user-defined class, you would most likely use
    1. a built-in conversion operator.
    2. a conversion constructor.
    3. an overloaded = operator.
    4. a conversion operator that's a member of the class.
  10. The compiler won't object if you overload the * operator to perform division for a new class.
    1. True
    2. False
  11. A friend function can access a class's private data without being a member of the class.
    1. True
    2. false
  12. Match each function definition of the binary operator+ function to its correct description. Ask yourself, "Is the operator a member or non-member," and "is it defined inside or outside the class?" Assume that functions defined outside of the class are appropriately prototyped in the class.
    1. foo foo::operator+(foo f) { . . . . }
    2. foo operator+(foo f1, foo f2) { . . . . }
    3. friend foo operator+(foo f1, foo f2) { . . . . }
    4. foo operator+(foo f) { . . . . }

    1. member defined in the class
    2. member defined out of the class
    3. non-member defined in the class
    4. non-member defined out of the class
  13. Write the prototype for a friend function called harry that returns type void and takes one argument named g that is an instance class george.
  14. Which of the following is NOT true?
    1. Operator functions should not contain more than three to five statements.
    2. Operator overloading cannot change precedence
    3. Operator overloading cannot create new operators
    4. Operator overloading cannot change the number of operands
    5. The functions that implement operator overloading must have unique argument lists.
  15. friend functions are members of their befriending classes.
    1. true
    2. false
  16. A binary operator has two operands. The operator is included in a C++ class as an overloaded operator that operates on two instances of the class, how many explicit arguments (i.e., arguments appearing inside the parentheses) should the function have if it is implemented as a member function?
    1. 0
    2. 1
    3. 2
    4. Cannot be implemented as a member function.
  17. A binary operator has two operands. The operator is included in a C++ class as an overloaded operator that operates on two instances of the class, how many explicit arguments (i.e., arguments appearing inside the parentheses) should the function have if it is implemented as a friend function?
    1. 0
    2. 1
    3. 2
    4. Cannot be implemented as a friend function.
  18. If class foo defines operator+(foo x) as a member function, and A, B, and C are instances of foo, then both of the following function calls are legal:
    C = A + B;
    C = A.operator+(B);
    1. True
    2. False
  19. If class foo defines operator+(foo x) as a friend function, and A, B, and C are instances of foo, then both of the following function calls are legal:
    C = A + B;
    C = A.operator+(B);
    1. True
    2. False
  20. Class Foo defines an overloaded operator+ that adds two Foo objects. Class Foo also defines a conversion constructor that converts an int to a Foo. If f is an instance of Foo, which statement is true of the following operation?
    3 + f
    1. operator+ is a member function
    2. operator+ is a friend function (i.e., a non-member function)
    3. operator+ may be a member or a friend function
  21. Which of the following serves as a default constructor for class Fraction? (Note that it may not be possible to include all of the functions in the class at the same time.) Mark all that apply.
    1. Fraction();
    2. Fraction(int n, int d) : numerator(n), denominator(d) {}
    3. Fraction(int n, int d = 1) : numerator(n), denominator(d) {}
    4. Fraction(int n = 0, int d = 1) : numerator(n), denominator(d) {}
    5. Fraction(int n) : numerator(n), denominator(1) {}
  22. Which of the following serves as a conversion constructor for class Fraction? (Note that it may not be possible to include all of the functions in the class at the same time.) Mark all that apply.
    1. Fraction();
    2. Fraction(int n, int d) : numerator(n), denominator(d) {}
    3. Fraction(int n, int d = 1) : numerator(n), denominator(d) {}
    4. Fraction(int n = 0, int d = 1) : numerator(n), denominator(d) {}
    5. Fraction(int n) : numerator(n), denominator(1) {}
  23. Class foo defines a conversion operator: operator int(); If F is an instance of foo, which of the following are valid calls to the conversion operator?
    1. int i = F;
    2. int i = int(F);
    3. int i = F.int();
    4. All three are valid
    5. a and b are valid
  24. A class bar declares the following two overloaded operators:
    bar operator*(bar b);
    bar operator*(int i);
    These operators permit the following expressions (B's are bar objects and x is an int):
    B3 = B1 * B2;
    B4 = B3 * x;
    Choose the function or functions to replace the two operators above that will still permit the above statements in addition to
    B5 = x * B5;
    1. friend bar operator*(bar b1, bar b2);
    2. bar(int i);
      friend bar operator*(bar b1, bar b2);
    3. bar(int i);
      friend bar operator*(bar b2);
    4. bar(int i);
      bar operator*(bar b1, bar b2);
  25. Based on the following class specification, which statement is NOT correct? (Assume that F3 through F5 are existing foo objects.)
    class foo
    {
        public:
            foo(int);
            foo operator*(foo&);
            friend foo operator+(foo&, foo&);
    };
    1. foo F1(100);
    2. foo F2 = F3;
    3. F3 = F4 + F5;
    4. F3 = F4.operator+(F5);
    5. F3 = F4 * F5;
    6. F3 = F4.operator*(F5);
  26. Given the following class specification, define an appropriate overloaded inserter operator (i.e., operator<<) function.
    class room
    {
        private:
            int width;
            int length;
    
        public:
            room(int w, int l) : width(w), length(l) {}
    };
    • Define just the requested function - not the whole class.
    • Include syntax appropriate for defining the function inside the class.
    • Assume that all necessary header files have been included.
  27. Given the class specification in the previous question, define an appropriate overloaded extractor operator (i.e., operator>>) function.
  28. How would the answers to questions 26 and 27 change if the functions were prototyped in the class but defined outside the class?
  29. Two classes, Alpha and Beta, in an inheritance relationship, with Alpha as the superclass. Both classes have on inserter operator whose first parameter is 'in' and the second is 'me.' Given the UML diagram at the right, write the overloaded Beta << operator so that it prints all of a Beta object's member variables, including any that are inherited. (Hint, don't worry about details that are not shown on the diagram and focus on what is known.)
  30. The following code fragment denotes an inheritance relationship between classes Bar and Foo (note that the highlighted characters are labels and are not part of the code):
    class Bar
    {
    	private:
    		int	count;
    		int	balance;
    
    	public:
    		Bar(int a_count, int a_balance) : (a)_____________ {}
    
    		friend ostream& operator<<(ostream& out, Bar& b)
    		{
    			out << b.count << " " << b.balance << endl;
    			return out;
    		}
    };
    
    class Foo : public Bar
    {
    	private:
    		string	name;
    
    	public:
    		Foo(string a_name, int count, int balance):
    			(b)______________________________________ {}
    
    		friend ostream& operator<<(ostream& out, Foo& f)
    		{
    			out << (c)_______________ << endl;
    			return out;
    		}
    };
    Fill in the blanks to complete the program.
    1. Complete the Bar initializer list.
    2. Complete the Foo initializer list.
    3. Print the name, and the inherited count and balance.
  31. The following code fragment denotes a composition relationship between classes Bar and Foo (note that the highlighted characters are labels and are not part of the code):
    class Bar
    {
    	private:
    		int	count;
    		int	balance;
    
    	public:
    		friend ostream& operator<<(ostream& out, Bar& b)
    		{
    			out << b.count << " " << b.balance << endl;
    			return out;
    		}
    };
    
    class Foo
    {
    	private:
    		string	name;
    		Bar		my_bar;
    
    	public:
    		friend ostream& operator<<(ostream& out, Foo& f)
    		{
    			out << (a) _______________<< endl;
    			out << (b) ______________ << endl;
    
    			return out;
    		}
    };
    Fill in the blanks to complete the program.
    1. Print the name
    2. Print the count and balance
  32. The following code fragment denotes an aggregation relationship between classes Bar and Foo (note that the highlighted characters are labels and are not part of the code):
    class Bar
    {
    	private:
    		int count;
    		int balance;
    
    	public:
    		friend ostream& operator<<(ostream& out, Bar& b)
    		{
    			out << b.count << " " << b.balance << endl;
    			return out;
    		}
    };
    
    class Foo
    {
    	private:
    		string	name;
    		Bar*		my_bar = nullptr;
    
    	public:
    		friend ostream& operator<<(ostream& out, Foo& f)
    		{
    			out << (a) _______________<< endl;
    			if ( (b) _________________ )
    				out << (c) ______________ << endl;
    
    			return out;
    		}
    };
    Fill in the blanks to complete the program.
    1. Print the name
    2. Make sure it's safe to use aggregation
    3. Print the count and balance