Operator Overloading in a hierarchy Flashcards

1
Q

When do you use the “using” declaration?

A

If you want to bring down candidate values in the derived class from the base class with different arguments.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What are the absolute member operators within a hierarchy?

A
  1. [ ]
  2. =
  3. +=
  4. ++(prefix)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

When calling an overloaded assignment operator to kickstart the base class’s assignment operator, what do the following operations do?

  1. *this = source_obj;
  2. (base)*this = source_obj);
A
  1. Does compile. BUT this is infinitely recursive and will call the derived class’s assignment operator repeatedly.
  2. Does not compile. This will have a syntax error because it is trying to type convert without a cast or upcasting. The error is will be “rvalue when expected lvalue” because it will attempt to make a type converted copy with a temporary variable.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How do you kickstart a base class’s operator from a derived class that has the same operator implemented?

A
  1. static_cast(*this) = 2nd_operand;
  2. base::operator = (2nd_operand);
  3. 2nd_operand.operator = (2nd_operand);
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

With FUNCTION overloading in C++, what happens when we have an operator with different arguments in the derived and base class and we try to use the base class’s operator in the derived class?

A

Syntax error. The compiler will find the operator in the derived class scope and cause an error because the parent’s operator is hidden.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What happens when operators of a derived class have the same name operators of their parent’s class?

A

Operators of a derived class “hide” their parent operators as would be expected with any member functions.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How do you call a base class’s member operator in a derived class?

A
  1. base::operator += (2nd_operand);

2. static_cast (*this) = 2nd_operand;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What are the non_member operators?

A

Any operator whose first operand is not an object of the class.

bit shifting operators: <>
conditional operators: , <=, >=, !=, ==
binary arithmetic operators: +, -, ++(postfix), / , %

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Why is using “friends” for non-member operators okay for object-oriented programming?

A

Because it allows for direct access of the data members (which should be protected or private) without the use of setters and getters (which is not object-oriented).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is the absolute case to make an operator a non-member?

A

The first operand is not an object of the class.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How do you call a non-member (friend) operator from a base class in a derived class?

A

friends are not inherited so, to call a non-member friend operator (or use it in some way) of the base class in the derived class you can:

1. use static cast to type convert the operator; operand_1 + static_cast(operand_2);
OR
2. use the operator to act as a wrapper function to call a  member function in the derived class that does the important stuff. Then call the member function.
operand_2.function(operand_1);
How well did you know this?
1
Not at all
2
3
4
5
Perfectly