- 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?
- 0
- 1
- 2
- 3
- 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?
- 0
- 1
- 2
- 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?
- 0
- 1
- 2
- 3
- 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?
- 0
- 1
- 2
- 3
- How many implicit arguments are required for an overloaded operator implemented as a member function?
- 0
- 1
- 2
- 3
- How many implicit arguments are required for an overloaded operator implemented as a friend function?
- 0
- 1
- 2
- 3
- 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)
- take two explicit arguments
- return a value
- create a named temporary object
- have one explicit argument of type C
- To convert from a user-defined class to a basic type, you would most likely use
- a built-in conversion operator.
- a one-argument constructor (aka a conversion constructor).
- an overloaded = operator.
- a conversion operator that's a member of the class.
- To convert from a basic type to a user-defined class, you would most likely use
- a built-in conversion operator.
- a conversion constructor.
- an overloaded = operator.
- a conversion operator that's a member of the class.
- The compiler won't object if you overload the * operator to perform division for a new class.
- True
- False
- A friend function can access a class's private data without being a member of the class.
- True
- false
- 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.
- foo foo::operator+(foo f) { . . . . }
- foo operator+(foo f1, foo f2) { . . . . }
- friend foo operator+(foo f1, foo f2) { . . . . }
- foo operator+(foo f) { . . . . }
- member defined in the class
- member defined out of the class
- non-member defined in the class
- non-member defined out of the class
- 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.
- Which of the following is NOT true?
- Operator functions should not contain more than three to five statements.
- Operator overloading cannot change precedence
- Operator overloading cannot create new operators
- Operator overloading cannot change the number of operands
- The functions that implement operator overloading must have unique argument lists.
- friend functions are members of their befriending classes.
- true
- false
- 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?
- 0
- 1
- 2
- Cannot be implemented as a member function.
- 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?
- 0
- 1
- 2
- Cannot be implemented as a friend function.
- 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);
- True
- False
- 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);
- True
- False
- 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
- operator+ is a member function
- operator+ is a friend function (i.e., a non-member function)
- operator+ may be a member or a friend function
- 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.
- Fraction();
- Fraction(int n, int d) : numerator(n), denominator(d) {}
- Fraction(int n, int d = 1) : numerator(n), denominator(d) {}
- Fraction(int n = 0, int d = 1) : numerator(n), denominator(d) {}
- Fraction(int n) : numerator(n), denominator(1) {}
- 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.
- Fraction();
- Fraction(int n, int d) : numerator(n), denominator(d) {}
- Fraction(int n, int d = 1) : numerator(n), denominator(d) {}
- Fraction(int n = 0, int d = 1) : numerator(n), denominator(d) {}
- Fraction(int n) : numerator(n), denominator(1) {}
- 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?
- int i = F;
- int i = int(F);
- int i = F.int();
- All three are valid
- a and b are valid
- 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;
- friend bar operator*(bar b1, bar b2);
- bar(int i);
friend bar operator*(bar b1, bar b2);
- bar(int i);
friend bar operator*(bar b2);
- bar(int i);
bar operator*(bar b1, bar b2);
- 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&);
};
- foo F1(100);
- foo F2 = F3;
- F3 = F4 + F5;
- F3 = F4.operator+(F5);
- F3 = F4 * F5;
- F3 = F4.operator*(F5);
- 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.
|
- Given the class specification in the previous question, define an appropriate overloaded extractor operator (i.e., operator>>) function.
- How would the answers to questions 26 and 27 change if the functions were prototyped in the class but defined outside the class?
-
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.)
- 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.
- Complete the Bar initializer list.
- Complete the Foo initializer list.
- Print the name, and the inherited count and balance.
- 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.
- Print the name
- Print the count and balance
- 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.
- Print the name
- Make sure it's safe to use aggregation
- Print the count and balance