Final II Flashcards

1
Q

An object consists of 3 things:

A
  1. Name
  2. Member Data
  3. Member Functions
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

An objects name is:

A

The variable name we give it.

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

Member data is:

A

The data that describes an object; Attributes, or the state of the object.

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

Member functions are:

A

Behavior aspects of an object (functions related to the object itself)

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

What is an object?

A

An encapsulation of data along with functions that act upon that data.

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

What is a class?

A

A blueprint for building objects; A user defined type.

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

A class description consists of:

A

A declaration and a definition. Usually split into separate files.

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

An object is a single instance of:

A

A class. You can create many objects from the same type of class.

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

What does DDU stand for?

A

Declare, Define, Use.

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

Declare (DDU)

A

Declaration of class (usually) in a header file; Gives an interface.

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

A variable declaration gives:

A

The type

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

A function declaration:

A

Tells how to use it, without bothering with how it works.

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

A class declaration:

A

Shows what an object will look like and what its available functions are, implementation details are not needed.

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

Define (DDU)

A

Definition of class members in implementation file.

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

T/F
The definition of a class doesn’t need to be seen by the user of the class (interface)?

A

True

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

A function definition:

A

The code that makes a function work (the function body)

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

A class definition:

A

Consists of definitions of its members.

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

Use (DDU)

A

Usage of class by building objects, through its interface (declaration).

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

The user of a function is:

A

A programmer, who makes calls to the function (without needing to know implementation details)

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

The user of a class is:

A

A programmer, who uses it by creating objects and calling the available functions for those objects.

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

3 Protection (access) levels in a class:

A

1) Public

2) Private

3) Protected

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

Public:

A

Can be accessed from inside or outside of the object.

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

Private:

A

Can only be used by the object itself.

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

Protected:

A

Protection label for use in base/derived classes; Members declared as protected can be accesses by the class itself and by derived classes only (not anywhere else)

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

The interface of an object is essentially comprised of:

A

The public section of the class.

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

The user of an objects is:

A

Some other portion of code (other classes, functions, main program)

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

Providing functions in the public area that handle all necessary actions on an object helps to:

A

Make the interface as simple as possible.

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

T/F
There are set rules on what gets made public and what gets made private?

A

False

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

Standard practice for member data of a class:

A

Make it private, to protect the data.

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

Functions that do not need to be part of the interface can be made:

A

Private

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

4 reasons for data hiding:

A

1) Makes interface simpler for user.

2) Principal of least privilege (need-to-know).

3) More secure. Less chance for misuse (accidental or malicious).

4) Class implementation easy to change without affecting other modules that use it.

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

Class declaration format:

A

class <classname></classname>

{

public:

(public data & functions)

private:

(private data & functions)

};

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

2 principles of good class design:

A

1) Decide upon semantic meaning of this type of object, along with what combinations of data values constitute valid meaning,

2) Guarantee that an object is always in a valid semantic state, regardless of what features user calls upon.

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

2 examples to guarantee valid semantic state of an object:

A

1) In a Circle class, don’t allow for a negative radius.

2) In a Temperature class, don’t allow a temperature to be below absolute zero.

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

2 ways to ensure an object is always in a valid semantic state:

A

1) In the public section, only put the members that the user needs to call upon directly.

2) In the private section, put things that the user does not need to use directly (this typically includes most member data)

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

By hiding member data in the private section:

A

The builder can control how the data items are updated, with validation/error-checking in the functions.

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

2 Primary categories of member functions:

A

1) Mutators

2) Accessors

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

Mutators:

A

Member functions that can change the state of an object.

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

Accessors:

A

Member functions that will not change the state of an object

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

Accessors should always be declared with:

A

const

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

Setter:

A

A common form of a mutator; A function that takes data in through parameters in order to set them into private variables on the inside.

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

Getter:

A

A common form of an accessor; A function that returns some internal data to the caller, usually by value (as a copy) or in some other read-only way.

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

Constructor:

A

A special member function of a class whose purpose is typically to initialize the member data of an object

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

Why have a constructor:

A

To ensure initialization that is not left up to the user; To set an object to a valid semantic state immediately upon creation.

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

3 properties of a Constructor

A

1) Has the same name as the class, no return type.

2) Automatically invoked upon creating an object.

3) Can have parameters but doesn’t have to;

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

Default constructor:

A

A constructor with no parameters.

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

T/F
A constructor is a function and you can define it to do anything you want.

A

True

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

Example call to default constructor:

A

Circle c1;

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

Example call to constructor with parameters:

A

Circle c1(51.4);

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

T/F
Circle c1(); calls the default constructor for the Circle class?

A

False, this declares a function named c3 that returns a Circle object.

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

How to make a constructor with optional parameters:

A

Use a default value on the parameter you want to make optional.

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

Example of a constructor with an optional parameter:

A

Fraction (int n, int d = 1);

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

Destructor:

A

A special function whose typical job is to do any clean-up tasks (usually involving memory allocation) that are needed before an object is deallocated.

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

3 properties of destructors:

A

1) Looks like the default constructor, but with a ~ in front.

2) Cannot have parameters, so there can only be one per class.

3) Called automatically, right before an object is deallocated by the system (usually when it goes out of scope)

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

T/F
Like the constructor, a destructor is a function and can do other things, if desired?

A

True.

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

Example declaration of a destructor:

A

~Fraction();

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

::

A

Scope resolution operator.

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

What is the scope resolution operator used for?

A

For specifying which class a member belongs to when we define it separately from its declaration inside the class.

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

To call a member function of an existing object use:

A

The dot operator (.)

-Syntax: f1.Show();

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

A class module normally consists of these two things:

A

1) A header file

2) An implementation file

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

Header file:

A

Contains the declaration of the class (without implementation details).

Usual form: filename.h

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

Implementation file:

A

Contains the implementations (definitions) of the class headers.

Usual form: filename.cpp

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

T/F
Filenames have to be the same as the class name:

A

False

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

Well-chosen filenames can:

A

Help identify the contents or purpose of a file.

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

2 major stages of compiling a program:

A

1) Compile stage

2) Linking stage

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

Compile stage consists of these 5 things

A

1) Checking syntax for correctness.

2) Variables and function calls checked to insure that correct declarations were made (“declare before use”).

3) Matching function calls to prototypes (doesn’t match calls to definitions yet).

4) Type checking on variable usage.

5) Translation into object code

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

Linking stage consists of these 2 things:

A

1) Linking object code files together, (usually) into an executable program.

2) Matches function calls to their definitions, checks to make sure it has only one definition for every function that is called).

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

T/F
The end result of the linking stage is usually an executable program?

A

True (but could be other target type like a DLL).

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

2 reasons for separate compiling and linking stages:

A

1) Changes to a file require only that file to be re-compiled, along with re-linking.

2) Libraries are often distributed in a pre-compiled format, so trying to #include a .cpp file would not be feasible.

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

CS account (g++) command for invoking compile stage only:

A

g++ -c filename.cpp

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

2 general rules for compilation and debugging:

A

1) Don’t #include .cpp files.

2) #include header files to satisfy “declare before use”.

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

What does it mean for a library to be distributed in a pre-compiled format?

A

The programmer only has header file and object code, not full implementation code)

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

3 Types of errors when compiling:

A

1) Compilation errors

2) Linker errors

3) Run-time errors

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

Compilation errors:

A

Usually syntax errors, undeclared variables and functions, improper function calls.

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

Linker errors:

A

Usually involve undefined functions or multiply-defined functions or symbols.

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

2 varieties of run-time errors:

A

1) Fatal: cause a program to crash during execution.

2) Non-fatal (logical): don’t crash the program, but produce erroneous results.

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

T/F
Compilation and linker errors result in a failed compilation of the program, while run-time errors occur while the program is running (after successful compilation)?

A

True

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

T/F
Compiler errors usually provides a filename and line number indicating where it ran into trouble, for each reported error?

A

True

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

4 tips for debugging compiler errors:

A

1) Always start at the top of the list of errors. Fix first error, then recompile and see what is left.

2) If a list of errors is too long, compile and debug one file at a time.

3) When searching for an error, start with the indicated line number, but also look in the vicinity (usually previous lines) for the possible error.

4) Compile portions of programs as you go, don’t wait until the program is fully written to do the first compile.

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

T/F
Linker errors are also reported by the compiler, but do not usually contain line numbers.

A

True, because the linker works on object code, not on the original source code.

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

3 tips for debugging linker errors:

A

1) Learn what kinds of problems cause linker errors (usually problems with agreement between definitions and calls).

2) Usually specify some kind of symbol (used by the compiler), which often resembles a function or variable name. This is usually a good clue.

3) Learn about good compilation techniques and pre-processor directives that help avoid linker errors.

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

T/F
Run-time errors must be tested while running a fully-compiled program?

A

True

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

2 tips for fixing run-time errors:

A

1) To catch fatal errors, try to wedge the mistake between extra printout statements to locate the cause.

2) To catch logic errors, place extra printout statements in code while testing (to be removed in final version). Especially, print out values of internal variables to locate computation problems.

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

Friend function:

A

Non-member functions that are given access to a class’ private section.

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

friend:

A

Keyword that allows a class to grant full access to an outside entity.
Full access means access to all the the class’ members, including private section.

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

Where are friend functions declared?

A

Inside the class definition block.

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

Is a friend function public or private?

A

Neither, by definition it is not a member of the class.

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

Does it matter where in the class definition block a friend function is placed?

A

No, because it is not a member of the class.

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

Example declaration of friend function:

A

friend bool Equals(Fraction x, Fraction y);

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

T/F
The keyword friend goes on the function definition in the implementation file?

A

False

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

T/F
The keyword friend goes on the function definition in the implementation file?

A

False

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

Is the scope resolution operator (::) used when defining a friend function?

A

No, it’s not needed since they are not members of a class.

93
Q

When using a member function instead of a friend, for a function that works on two or more objects:

A

One of the objects must be the calling object.

94
Q

When is it convenient to use a friend function instead of a member function.

A

When a function works on two or more objects.

95
Q

Example call to a member function that works on two or more objects:

A

if ( f1.Equals(f2) );

96
Q

How many parameters does a member function that works on two objects have?

A

2

97
Q

Example of member function that works on two objects:

A

bool Equals(Fraction f) const;

98
Q

In a member function working on two objects, to ensure the calling object isn’t changed by the function you should:

A

Declare the member version as a const member function.

99
Q

Conversion constructor:

A

A constructor with one parameter that allows for automatic type conversion from from parameter type to class type.

100
Q

T/F
The conversion constructor will be invoked automatically in all places where automatic type conversion applies?

A

True

101
Q

The conversion constructor will be invoked automatically in all places where automatic type conversion applies?

A

True

102
Q

T/F
If a constructor with two parameters has an optional parameter, it counts as constructor with one parameter, and is therefore a conversion constructor?

A

True

103
Q

Explicit call to constructor:

A

Fraction f1;
f1 = Fraction(4);

104
Q

Implicit call to conversion constructor:

A

Fraction f2;
f2 = 10;

105
Q

Example of a conversion constructor declaration:

A

Fraction(int n);

106
Q

In the event that a constructor with a single parameter should not be used for automatic type conversions, this feature can be suppressed by:

A

Using the keyword explicit on the constructor declaration.

107
Q

explicit means:

A

Only explicit calls to the constructor can be used.

108
Q

Everywhere the keyword const applies in code it does 3 things:

A

1) Expresses an intent on the part of the programmer, which the complier enforces (something is not allowed to change, within some scope)

2) Expresses the intent more clearly to a user (in this case, another portion of code–i.e. maybe another programmer)

3) Affects how certain items can be used. Sometimes this is the difference between using L-values and R-values in calls to functions. Sometimes this affects what other items can be used (whether they are also const or not)

109
Q

T/F
Pass-by-value vs. Pass-by-reference works the same on objects as it does with built-in types?

A

True

110
Q

If an object is passed by value:

A

A copy of the object is made. Any R-value can be sent on the call.

111
Q

If an object is passed by reference (without const):

A

No copy is made. Only an L-value can be sent on the call.

112
Q

When an object is passed by const reference:

A

No copy is made but the object cannot be changed through the reference.

113
Q

2 reasons to pass an object by const reference:

A

1) Less overhead

2) Since objects are sometimes large, it is often desirable to not have to make a copy.

114
Q

Passing an object by const reference (const reference parameter) protects against:

A

Changing the object through the reference.

115
Q

Example using pass by const reference parameters:

A

friend Fraction Add(const Fraction& f1, const Fraction& f2);

116
Q

4 uses of const:

A

1) const reference parameters in member and friend functions

2) const member functions

3) const objects

4) const member data

117
Q

Const member functions:

A

Means the function may not change the calling object itself.

118
Q

T/F
Using const on a member function can only be done to member functions of a class (not stand-alone functions)?

A

True

119
Q

Const member functions protect against:

A

Changing member data of the calling object. (Object remains in same state before and after call)

120
Q

How to use const member functions:

A

Place the keyword const at the end of the member function prototype and on the definition.

121
Q

T/F
Accessor member functions should always be const?

A

True

122
Q

Example of const member function declaration:

A

int GetNumerator() const;

123
Q

T/F
Primitive type variable declared as const must be initialized on the same line that they are declared.

A

True

124
Q

Const objects:

A

After the constructor runs for to initialize it, the object’s state (internal data) cannot be changed.

125
Q

Compiler rule to ensure that a const object cannot be changed:

A

A const object may only call const member functions.

126
Q

T/F
Attempting to call a non-const member function on a const object results in a compiler error?

A

True

127
Q

T/F
Const objects must also be initialized on the same line that they are declared?

A

True

128
Q

T/F
Member data of a class can also be declared const?

A

True

129
Q

T/F
You can initialize member data variables on their declaration lines in a class definition block?

A

False

130
Q

In order to declare and initialize member data variables in one step (like for const member data) use:

A

An initialization list.

131
Q

Operator Overloading:

A

The creation of new versions of built-in operators for use with user-defined types.

132
Q

5 rules of operator overloading

A

1) Cannot change its precedence

2) Cannot change its associativity

3) Cannot change its arity (number of operands)

4) It is not possible to create new operators, only new versions of existing ones

5) Operator meaning on the built-in types cannot be changed

133
Q

T/F
It is legal to mix and match types for operators (like a Fraction object + a Mixed object)?

A

True

134
Q

To declare an operator overload function:

A

Needs name, return type, and parameter list.

135
Q

The name of an operator overload function is:

A

A conjunction of the keyword operator and the operator symbol.

136
Q

T/F
Some operators can be written as member functions, some as outside functions, and some can be written as either?

A

True

137
Q

T/F
When written as stand-alone functions, it’s common to make operator overloads friends of a class:

A

True

138
Q

For binary operators:

A

1) Written as stand-alone function, both operands are passed as parameters.

2) Written as member function, first operand is calling object, second is received parameter.

139
Q

2 qualities of unary operators:

A

1) As stand-alone function, the operand is received as a parameter.

2) As a member function, the operand is the calling object. (With a few small exceptions, like post-increment)

140
Q

T/F
The overloaded insertion («) and extraction (») operators have to be done as stand-alone functions, if they are to work like the built-in versions:

A

True, if they were members, they would have to be inside the classes ostream and istream.

141
Q

For overloaded insertion («) and extraction (») operators:

A

Return type and first parameter are stream objects, which must be passed by reference.

142
Q

For overloaded extraction (») operator:

A

Pass the object (second parameter) by reference.

143
Q

For overloaded insertion («) operator:

A

Pass the object by value or by const reference (better)

144
Q

Aggregation/Composition:

A

“Has-a” relationship between objects

145
Q

How is Aggregation/Composition implemented?

A

By embedding an object of one class type (or a pointer or reference) inside as member data of another class type.

146
Q

T/F
Some developers use the term composition to refer to a stronger form of aggregation, where the embedded objects typically would not exist outside of the container object.

A

True

147
Q

“Tool building”

A

The idea of objects that are components of larger objects.

148
Q

Aggregation/Composition is useful for:

A

Embedding objects into a “manager class”, to manage communication between components.

149
Q

Declare an array of 20 Fraction objects:

A

Fraction rationals[20];

150
Q

T/F
Normal declaration of an array of objects uses the default constructor for each object in the array:

A

True
-Ex: Fraction nums[4];

151
Q

To specify different constructor for different array items, use:

A

An initializer set (must use explicit constructor calls)

Fraction nums[3] = { Fraction(2, 4), Fraction(5), Fraction()};

152
Q

Use a fraction object in an array named rationals to call the Show function for the object in index 2 of the array.

A

rationals[2].Show();

153
Q

T/F
Arrays can not be used as member data of a class:

A

False

154
Q

To keep track of how many items are in an array, use:

A

Tracking variable

155
Q

What needs to be added into member functions when using an array as member data of a class?

A

Error checking and boundary protection.

156
Q

What kind of relationship is an array of Card objects embedded inside Deck class?

A

“has-a” relationship

157
Q

Two types of memory allocation:

A

1) Static: Compile time. Size and types known in advance.

2) Dynamic: Run time. Sizes and amounts can be set while program is running.

158
Q

What is the ‘new’ keyword used for?

A

Used to allocate dynamic space; returns the address of the allocated item. Store in a pointer. (always use a type after new)

159
Q

Example of dynamically allocated integer variable:

A

int * ptr = new int;

160
Q

T/F
new can only be used to dynamically basic variable types:

A

False, can be used to allocate basic variables, objects, and arrays.

161
Q

Dynamically allocate an array of ints:

A

int * list = new int[size];

162
Q

T/F
You can pass in parameters to constructors on dynamically allocated objects:

A

True

163
Q

What is the ‘delete’ keyword used for?

A

Deallocates dynamically allocated memory. Apply to the pointer and it deallocates the target.

164
Q

Syntax for deallocating the dynamic data of a simple variable contained in a pointer called ‘ptr’:

A

delete ptr;

165
Q

Deallocation of a dynamic array pointed to by a pointer called ‘nums’:

A

delete [] nums;

166
Q

T/F
When dynamically allocating an object, the default constructor is invoked unless parameters are added:

A

True

167
Q

What operator is this and what does it do:
->

A

Arrow operator, like dot operator but for pointers.

p->Show(); equivalent to (*p).Show();

168
Q

4 steps to dynamically resizing an array:

A

1) Dynamically create a new array of the desired size (need another pointer)

2) Copy data from the old array to the new one (use for loop)

3) Deallocate the old array.

4) Change the pointer so the new array has the right name.

169
Q

4 steps to use dynamic memory allocation inside a class:

A

1) Declare pointer as member data

2) Always initialize pointers in the constructor (use null pointer if value is not needed yet)

3) Use new inside member functions to allocate space

4) Use correct cleanup in destructor (delete)

170
Q

For good class design when using dynamic memory allocation:

A

Separate memory management tasks from functionality/algorithmic tasks whenever possible.

171
Q

What are the four automatics:

A

1) Constructor

2) Destructor

3) Copy constructor

4) Assignment operator=

172
Q

What makes the automatics special?

A

If you do not explicitly define one, a default version will be automatically made by the compiler.

173
Q

3 qualities of automatics:

A

1) Default version of constructor and destructor are empty

2) Not every class has a default constructor (no parameters), only if no constructor is provided

3) Default version of copy constructor and assignment operator make a shallow copy

174
Q

What is a shallow copy:

A

Pointers and references are copied verbatim. They end up pointing to the same attached data

175
Q

Pass by address:

A

When & is used in regular statements (not reference declarations), in means “address of”

-Ex: int n;
cout &laquo_space;&n; (Prints the address of n)
Can be used to attach data to pointers
-Ex: int n;
int * p;
p = &n;

176
Q

Deep Copy:

A

Makes copies of attached data as well (which is external from the physical “object”)

177
Q

When is a deep copy needed?

A

When dynamic allocation is done from inside a class, if copies of objects are to be allowed.

178
Q

How and when is the copy constructor invoked (2 ways)?

A

Automatically, when a new copy of an object is created. Copies are made when:

1) An object is declare and initialized to another object’s value in the same statement

2) An object is passed into (or returned from) a function by value

179
Q

Parameter for copy constructor:

A

Always has one parameter, which is of the same type of the class. It is always passed by reference. (making the parameter const not required but usually a good idea)

180
Q

When is the assignment operator invoked?

A

When one object is assigned to another.

181
Q

T/F
The assignment operator is written as a stand-alone function:

A

False, written as a member function

182
Q

4 steps to defining the assignment operator:

A

1) Similar to copy constructor in the making of a deep copy

2) Needs to return *this (calling object), by reference. Return enables cascading of =

3) May have previously attached data to delete first

4) May need to protect from self-assignment

183
Q

2 qualities of ‘this’ (used from a member function):

A

1) Pointer to the current calling object

2) *this would represent a name for the calling object (the actual object)

184
Q

C-string:

A

Implemented as a null terminated character array (No built in string type in C)
-String literals in code are implemented as constant C-strings(“Hello World”)

185
Q

T/F
Every character array is a c-string:

A

False, only when terminated with the null character

186
Q

4 downsides of C-strings

A

1) Fixed length, when declared as normal c-style array

2) C-string name really just acts as a pointer, since it’s the name of an array

3) Overflow of c-string boundary not automatically detected, including in library functions

4) Less intuitive function calls for common operations

187
Q

One of the larger pitfalls of C-strings

A

Library functions for dealing with C-strings are usually based on the expectation of a null-character to stop the loop processing the c-sting

188
Q

When using a class to build a string type, use these 5 things:

A

1) Internal implementation with character arrays

2) Dynamic memory allocation for variable length strings

3) Destructor, copy constructor, and assignment operator (in the event of internal dynamic allocation)

4) Conversion constructor for converting other types to strings

5) Operator overloads for more intuitive notations (insertion & extraction, comparison, operator+ for concatenation)

189
Q

The inheritance relationship:

A

1) “is-a” relationship

2) Base classes and derived classes (derived class inherits from base class)

3) Represents idea of supertypes and subtypes (categories and subcategories)

190
Q

Format to declare a derived class

A

class derivedName : public baseName

(the word “public” causes derivedClassName to be publicly derived from base class; protection levels in derived class are same as in base class)

191
Q

3 features of a constructor for inheritance relationship

A

1) When a derived object is created, the base class constructor runs too

2) Constructor definitions run in top-to-bottom order. Base class constructor first

3) Destructor runs in reverse order (derived class first, base last)

192
Q

For default constructor in inheritance relationship:

A

The derived class constructor automatically calls the parent constructor

193
Q

3 features of a constructor with parameters in inheritance relationship:

A

1) Parameters can be passed in when declaring a derived object

2) Parameters are distributed up to the base constructors through initialization list (with explicit calls to parent constructor)

3) Constructor bodies still run in normal order (base class first)

193 / 229

194
Q

T/F
Derived classes only inherit public data and functions from base class:

A

False, they inherit all data and functions

195
Q

Can you still write a new version of an existing base class function for the derived class?

A

Yes, using function overriding
1) Both base and derived class can have their own version of a function with the same prototype

2) Can distinguish between them with class name and scope resolution operator

196
Q

Multiple inheritance

A

A class can inherit properties from more than one base class

197
Q

Example of multiple inheritance

A

Old versions of ifstream and ofstream
1) ifstream derived from istream and fstreambase
2) ofstream derived from ostream and fstreambase

198
Q

Important pointer property:

A

Pointers to base class types can point at derived objects (Similarly, a base class reference variable can refer to a derived object)

199
Q

2 benefits of pointer properties to user:

A

1) Simplifies storage, putting many base pointers into one container (heterogenous list)

2) Ability to write more versatile functions, with base pointer (or reference) parameters. User can send pointers to various derived objects on the call

200
Q

Virtual function:

A

Base class function
declare with virtual keyword

201
Q

What does declaring a function as virtual do?

A

Changes the binding of the call to the function definition from static (compile time) to dynamic (run time)
- Function is bound dynamically

202
Q

A virtual function is called through:

A

A base class pointer, then the derived version of the function can run (the target of the pointer)

203
Q

2 reasons why virtual functions are needed:

A

1) Compiler can only make decisions based on the type of the calling object or pointer

2) Because of the pointer property (base pointer pointing at derived object)

204
Q

Placing =0 on a function declaration means:

A

The function will not be defined (makes a virtual function a pure virtual function)

205
Q

Abstract class:

A

A class with at least one pure virtual function; Abstract classes cannot be instantiated (cannot build an object of this type)

206
Q

Polymorphism:

A

With OOP, refers to the use of the base pointer to child object property, along with virtual functions and function overriding to make the appropriate calls

207
Q

3 properties of bitwise operators:

A

1) Built-in operators that allow accessing and manipulating of individual bits

2) Necessary since smallest variables that can be created are at least one byte

3) Accessing individual bits can be useful for making more efficient algorithms or using less storage space

208
Q

4 properties of function templates:

A

1) Functions that can work with multiple parameter types

2) Compiler builds a separate function for each needed type, based on the calls used

3) Easy way to create overloaded functions without writing individual versions

4) Operations performed inside the function need to be valid for types used in the calls

209
Q

4 properties of class templates:

A

1) Extension of function template idea

2) Allows creation of a “generic” class, where the type of item stored can vary

3) Again, operations used inside the class need to be valid for any type used to instantiate the class

4) Each member function is written as a function template

210
Q

To declare a template:

A

Use template keyword along with parameters in angle brackets <>

template < class T >

template < typename T >

211
Q

Sample declaration of template objects of type Stack.

A

Stack< double > myStack;

Stack< int > stack2(10);

212
Q

What are Data Structures used for?

A

Created to store data in useful ways, often beyond the built-in mechanisms (like arrays)

213
Q

5 properties of a Stack:

A

1) First In Last Out (FILO) structure

2) push: function to insert data into stack

3) pop: function to delete data item from stack

4) Inserts and deletes always from the same end

5) Application areas include compilers, operating systems, handling of program memory (nested function calls)

214
Q

5 properties of a Queue:

A

1) First In First Out (FIFO) structure, similar to waiting in line

2) enqueue: function to insert data into queue

3) dequeue: function to delete data item from queue

4) Inserts and deletes always from opposite ends

5) Application areas include print job scheduling, operating systems (process scheduling)

215
Q

3 properties of a Vector:

A

1) Storage of a list using array based storage internally; stores data items of the same type (Use dynamic allocation and handle boundary issues of array with error checking for out of bounds indices)

2) Advantages: random access - i.e. quick locating of data if index is unknown

3) Disadvantages: Inserts and deletes are slower, since they require shifting of elements to consecutive array slots

216
Q

Sequence (or list):

A

Idea of a container that stores a sequence of data, in a specific order, possibly with duplicates (Ex: list of test grades)

217
Q

What is a Set:

A

A collection of data that does not have any duplicates, and order may or may not matter (Ex: Venn diagram)

218
Q

4 properties of a Linked List:

A

1) Storage of a list in a linear format using self-referential objects

2) Each node of a linked list stores a piece of data, and points to the next node in the list (nodes can be anywhere in memory, are generally allocated dynamically)

3) Advantages: Inserts and deletes are fast. Require only creation of a new node and changing a few pointers

4) Disadvantages: No random access. Possible to build indexing, but locating an element requires walking through the list.

219
Q

Self-referential object:

A

Contains data, along with one or more pointers to the next node in the list (the nodes used for Linked Lists qualify as self-referential objects)

220
Q

The advantages of the array (vector) are generally the disadvantages of:

A

The linked list, and vice versa.

221
Q

4 common features of implementing basic data structures:

A

1) Encapsulation of data structures inside an object means details can be handled internally, outside access through simple interface.

2) Often involves pointers and dynamic memory allocation (inside the data structure)

3) Templates are commonly used, to make data structures more general, useful with more than one type of data

4) Some structures can be implemented with others, through inheritance or composition (A stack can be implemented with a linked list or a vector, for example)

222
Q

3 properties of a Recursive function:

A

1) A function that calls itself

2) Involves extra activation records on function call stack
3) Infinite recursion will cause activation records to overrun the stack (and crash the program), so a way out needs to be provided to a recursive function to prevent it infinitely calling itself.

223
Q

5 example recursive algorithms:

A

Factorial, Fibonacci, GCD, sorting, binary search

224
Q

3 properties of Exception handling:

A

1) A method of error handling

2) Good for processing errors that must be handled in places other than where they occurred

3) Good for handling errors from widely used libraries or other components

4) Should not be used for general program control (confusing to the reader)

225
Q

Basic exception handling syntax:

A

try: label used for a block that encloses an area where exceptions might be thrown

  • try { }

throw: used to throw an exception. Can “throw” an item like a variable or an object (like special exception objects)

  • throw DivideByZeroException(): (Also used to build a throw list)

catch: the catch blocks immediately follow the try blocks. Can have more than one. Each catch can take one parameter, indicating the type of exception to be caught.

Special catch Block: (Fill in later???)

226
Q

4 directives of Conditional compilation

A

define SYMBOL: simply brings the symbol into existence

227
Q

2 things Conditional compilation statements can do:

A

1) Be used like if statements, at the compiler level

2) Used to check if symbols are defined, and compiles sections of code based on the result

228
Q

Example of conditional compilation:

A

ifndef _FRACTION_H

#define _FRACTION_H

// conditionally compiled code goes here

#endif