7 - Constructors and Other Tools Flashcards

1
Q

What is a constructor?

A

A constructor is a member function of a class that is automatically called when an object of that class is declared.

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

How are constructors named?

A

They must have the same name as the class.

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

What do constructors return?

A

Nothing. They cannot return a value. Moreover, no type, not even void, can be given at the start of the function declaration or in the function header.

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

Constructors are placed in the public or private section of the class definition?

A

Public. If you were to make all your constructors private members, then you would not be able to declare any objects of that class type, which would make the class completely useless.

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

What is the scope resolution operator?

A

::
As in

DayOfYear :: DayOfYear (int monthValue, int dayValue)

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

There’s a pitfall associated with calling a default constructor. Which?

A

DayOf Year date3 ( ); IS WRONG
DayOfYear date3; IS RIGHT

It is tempting to think that empty parentheses should be used when declaring a variable for which you want the constructor with no arguments invoked, but the problem is that although you may mean it as a constructor invocation, the compiler sees it as a declaration of a function.

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

What are vectors?

A

Vectors can be thought of as arrays that can grow (and shrink) in length while your program is running.

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

Declare a variable v, for a vector with base type int.

A

vector v;

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

How do you add an element to a vector for the first time?

A

You use push_back:

vector sample;
sample.push_back(0.0).

This adds an element in the next available position.

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

How do you make sure you can use vectors in your program?

A

include

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

How do you know how many elements are in the vector?

A

v.size().

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

A common error associated with vector size is ..

A

Trying to set v[i] for i greater than or equal to v.size().

You may or may not get an error message, but your program will undoubtedly misbehave at some point.

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

What is the capacity of a vector?

A

Capacity is the number of elements for which it currently has memory allocated. This is larger than v.size ( ). The member function capacity ( ) can be used to find out the capacity of a vector.

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

How do you set the capacity of a vector, and why would you?

A

For performance purposes, you might want to make sure that the implementation of C++ doesn’t give your vector a capacity much larger than needed. This can be done using the member function reserve ( ) .

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

How do you cut away the last elements of a vector?

A

You can change the size of a vector using the member function resize.

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