Operator Overloading Flashcards

1
Q

What is an example of an overloaded operator built into c++?

A

&laquo_space;stream insertion/bitwise left shift operator

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

When do + and - perform differently?

A

Depending on arithmetic type (int, float, pointer)

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

What is an alternative to overloaded operators?

A

Explicit function calls

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

What namespace does string belong to?

A

std

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

What overloaded operators does string provide?

A

Equality, relational, assignment, addition assignment, and subscript

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

What is boolalpha?

A

Stream manipulator
Displays true or false instead of 1/0

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

How to indicate string object literal?

A

Place letter s immediately following closing “ of string literal

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

Member function of string that return substring?

A

substr

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

Which string overloaded operator does not perform bounds checking?

A

[]

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

How to bounds check with string?

A

Member function at returns reference if subscript is valid

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

How to overload an operator?

A

Function definition with keyword operator followed by operator being overloaded

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

Operators overloaded must be what type of member functions?

A

Non-static
Must be called on an object

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

Which operators can be used on class objects without being defined?

A

=, &, and ,

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

Can you change precedence or associativity by overloading?

A

No

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

The number of operands an operator takes can…..

A

Not be changed

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

Can you create new operators?

A

No

17
Q

Can you change the meaning of how an operator works on fundamental types?

A

No

18
Q

Which assignment operators must be declared as class members

A

(), [], ->

19
Q

Binary operator must be overloaded as a ___________ member function with one arg or as a ______ member function with two args (with one of the arguments a _______)

A

non-static
non-
Class object or reference to a class object

20
Q

Unary operators for a class can be overloaded as a __________ member function with no arguments or a _________ member function with one argument (which must be a _______)

A

non-static
non-
Object or reference to an object

21
Q

Why should member functions that implement overloaded operators be non-static?

A

So they can access the non-static data in each object of the class

22
Q

How to overload prefix and postfix operators?

A

Each must have a distinct signature.
Postfix must have second arg of type int for compiler to distinguish

23
Q

Dynamic memory management

A

Controls allocation and deallocation of memory for built in or user defined type

24
Q

Heap/free store

A

Memory assigned to a program to store dynamically allocated objects at execution time

25
Q

What does new operator do?

A

Allocates right amount of storage for object
Runs objects constructor
Returns pointer of correct type
Throws exception if not enough memory

26
Q

Delete operator

A

Destroys dynamically allocated object and frees space

27
Q

How can you improve performance of non-member operator functions?

A

Declare as friend

28
Q

How to obtain class arg of member functions explicitly?

A

The this pointer

29
Q

How to enable an operator to be commutative?

A

Overload as non-member function

30
Q

Conversion constructors

A

Turn objects of other types into objects of particular class
Ex: overloaded cast-operator functions

31
Q

What can be used as a conversion constructor?

A

Constructor called with a single argument

32
Q

Can a compiler call cast operators and conversion constructors ?

A

Implicitly when necessary

33
Q

Explicit constructors

A

Can’t be used in an implicit conversion

34
Q

Can you overload the function call operator? ()

A

Yes, allows arbitrary number of parameters