MOD 2_ZYBOOKS 3_STRUCTS & CLASSES Flashcards

(116 cards)

1
Q

What is an object?

A

A grouping of data (variables) and operations that can be performed on that data (functions).

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

What is abstraction? (aka information hiding or encapsulation).

A

To have a user interact with an item at a high-level, with lower-level internal details hidden from the user

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

What is an abstract data type (ADT)? What can be used to implement it?

A

A data type whose creation and update are constrained to specific well-defined operations. A class can be used to implement an ADT.

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

What does a namespace do? Which operator does it use?

A

Defines a region (or scope) used to prevent name conflicts.

The scope resolution operator :: allows specifying in which namespace to find a name, as in: auditorium::Seat concertSeat; and airplane::Seat flightSeat;.

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

T/F: Are all items in the C++ standard library part of the std namespace?

A

True.

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

What are the 2 ways in which a programmer can use classes like string or predefined objects like cout?

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

T/F: For code clarity, most programming guidelines discourage using namespace directives except perhaps for std.

A

True.

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

What is an inline member function?

A

A member function’s definition that appears within the class definition.

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

What does an inline member function look like?

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

Normally, items like variables must be declared before being used, but this rule does not apply within a ________?

A

Class definition.

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

T/F: A public inline member function can thus access a private data member even though that private data member is declared after the function.

A

True.

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

Should a very short inline member’s function statements be put below the function’s name or on one line?

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

What is an ostream (output stream)?

A

A class that supports output, available via #include <iostream> and in namespace "std".</iostream>

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

What operator does an ostream use?

A

The insertion operator («)

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

What does an insertion operator («) do?

A

Converts different types of data into a sequence of characters.

That sequence is normally placed into a buffer, and the system then outputs the buffer at various times.
(«) operators can appear in series.

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

What data types can the &laquo_space;operator support?

A

Standard data types, such as int, bool, float, etc.

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

What is cout?

A

A predefined ostream object (declared as ostream cout; in the iostream library) that is pre-associated with a system’s standard output, usually a computer screen.

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

What is an istream (input stream)?

A

A class that supports input. Available via #include <iostream></iostream>

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

What is the (») operator?

A

Extraction operator.

Extracts data from a data buffer and writes the data into different types of variables.

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

What is cin?

A

A predefined istream pre-associated with a system’s standard input, usually a computer keyboard.

The system automatically puts the standard input into a data buffer associated with cin.

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

The (») operator skips leading ________ and reads characters up to the next ________.

A

Whitespace, whitespace

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

What is a class?

A

The class definition creates a new type that can group data and functions to form an object.

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

What are a class’ public member functions?

A

They indicate all operations a class user can perform on the object.

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

Does a class user need not know how the class’ data and functions are implemented?

A

No. They need only understand how each public member function behaves.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
A class user can declare a variable of the class type to create a new ________. Then, the class user can call the ________ to operate on the object.
Object, functions
26
Can a programmer create one or more objects of the same class?
Yes.
27
Declaring a variable of a class type creates an ________ of that type.
Object.
28
Restaurant favLunchPlace; declares a Restaurant ________ named favLunchPlace.
Object.
29
The member access operator (.), is used to invoke a ________ on an object.
Function Ex: favLunchPlace.SetRating(4) calls the SetRating() function on the favLunchPlace object, which sets the object's rating to 4.
30
What does a typical class look like? (With objects and functions)
31
Are strings a type of class?
Yes. The string class stores a string's characters in memory, along with variables indicating the length and other things.
32
Give 3 string public member functions.
33
What are private data members? How do they appear in a class definition?
Variables that member functions can access but class users cannot. Private data members appear after the word "private:" in a class definition.
34
The scope resolution operator (::) is used when defining or declaring functions?
Defining, not declaring.
35
How do you declare a class' public member functions?
1. DECLARE member functions after the word "public:" in the class definition. 2. DEFINE each member function AFTER class definition.
36
What are the 3 components of a class' function DECLARATION? Does it consist of the function's statements?
1. Function's name 2. Return type 3. Parameter types It does NOT contain the function's statements. (Contained in .cpp)
37
What are the 5 components of a class' function DEFINITION?
1. Class name 2. Return type 3. Parameter types 4. Parameter names 5. Function's statements
38
How is the function definition written?
ClassName::
39
Can the class function definition access private data members?
Yes.
40
Give an example of a complete class definition.
41
How is the Print() member function declared?
void Print();
42
How does the Print() member function's definition begin?
void Restaurant::Print()
43
Could the Print() function's definition begin as follows?
44
Which private data members of class Restaurant do the Print() function definition's statements access?
Name and rating.
45
What enables a user to utilize the string class?
#include
46
Which 2 files typically use classes? What must they include?
Main file and ClassName.cpp They must include ClassName.h
47
What is good practice when using classes?
Create unique .cpp and .h files for each class.
48
Give an example of using two separate files for a class.
49
Programmers typically put all code for a class into which 2 files? (Separate from other code).
ClassName.h contains the class definition, including data members and member function declarations. ClassName.cpp contains member function definitions.
50
How is a class declared in the main file?
51
T/F: The .cpp file for a class should #include the associated .h file
True. Otherwise, the .cpp file cannot be compiled on its own. The class definition is needed by the functions.
52
Give an example for the class 'Review'.
53
Give an example for the class 'Restaurant'.
54
Give an example of how to sketch classes.
Private items are listed with (-) Public items are listed with (+)
55
How can this sketch for a class be written in code? (Without Main.cpp)
56
When can a programmer initialize data members of a class?
When writing the class definition. Any variable of that class type will initially have those values.
57
Give an example of a class definition with initialized data members?
58
Consider this example. When favLunchPlace is initially declared, what is the value of favLunchPlace's rating? After the call to SetRating(), what is the value of favLunchPlace's rating?
-1 4
59
What is a constructor?
A special class member function which is called automatically when a variable of that class type is declared, and which can initialize data members.
60
How is a constructor named? Does it have a return type? or "void"?
A constructor has the same name as the class. It has no return type, not even void.
61
Define a constructor for the class 'Restaurant'.
Restaurant::Restaurant() {...}
62
How can you overload a constructor?
By defining multiple constructors differing in parameter types. A constructor declaration can have arguments. The constructor with matching parameters will be called.
63
T/F: If a programmer defines any constructor, the compiler does not implicitly define a default constructor, so good practice is for the programmer to also explicitly define a default constructor so that a declaration like MyClass x; remains supported.
64
Can a constructor's parameters be assigned default values?
Yes.
65
If those default values allow the constructor to be called without arguments, then that constructor can serve as the ____ ____.
Default constructor
66
The default values could be in the function ____, but are clearer to class users in the ____.
Definition, declaration
67
Give an example of a constructor with default parameter values which makes it the default constructor.
68
What is a constructor initializer list?
An alternative approach for initializing data members in a constructor, coming after a colon and consisting of a comma-separated list of variableName(initValue) items.
69
Give an example of a constructor initializer list.
70
When is it important to use a constructor initializer list?
The approach is important when a data member is a class type that must be explicitly constructed. Otherwise, that data member is by default constructed. Ex: If you have studied vectors, consider a data member consisting of a vector of size 2.
71
Sometimes a program has two functions with the same name but differing in the number or types of parameters, known as function ____. The following two functions print a date given the day, month, and year. The first function has parameters of type int, int, and int, while the second has parameters of type int, string, and int.
Function name overloading
72
More than two same-named functions is allowed as long as each has distinct parameter types. Thus, in the above program:
PrintDate(int month, int day, int year, int style) can be added because the types int, int, int, int differ from int, int, int, and from int, string, int. PrintDate(int month, int day, int year) yields a compiler error, because two functions have types int, int, int (the parameter names are irrelevant).
73
Does a function's return type influence overloading?
A function's return type does NOT influence overloading. Thus, having two same-named function definitions with the same parameter types but different return types still yield a compiler error.
74
Would adding the function void PrintDate(int month, int day, int year, int style = 0) above would generate a compiler error?
The use of overloading and of default parameter values may be combined as long as no ambiguity is introduced. Adding the function void PrintDate(int month, int day, int year, int style = 0) above would generate a compiler error because the compiler cannot determine if the function call PrintDate(7, 30, 2012) should go to the "int, int, int" function or to that new "int, int, int, int" function with a default value for the last parameter.
75
What are the 2 most commonly classified public functions of a class?
76
What are the setter and getter functions?
77
Accessor functions usually are defined as ____ to make clear that data members won't be changed.
const
78
The keyword ____ after a member function's name and parameters causes a compiler error if the function modifies a data member. If a __same__ member function calls another member function, that function must also be __same__.
const
79
How is const written with members?
80
A programmer commonly creates private functions, known as "____ ____ functions", to help public functions carry out tasks.
Private helper functions
81
OBSERVE: Private helper member functions.
82
C++ allows a programmer to redefine the functionality of built-in operators like +, -, and *, to operate on programmer-defined objects, a process known as ____ ____.
operator overloading.
83
OBSERVE: Operator overloading
84
How do you overload TimeHrMn's + operator?
To overload +, the programmer creates a member function named operator+. Although + requires left and right operands as in time1 + time2, the member function only requires the right operand (rhs: right-hand-side) as the parameter, because the left operand is the calling object. In other words, time1 + time2 is equivalent to the function call time1.operator+(time2), which is valid syntax but almost never used.
85
How do you overload the + operator multiple times?
When an operator like + has been overloaded, the compiler determines which + operation to invoke based on the operand types. In 4 + 9, the compiler sees two integer operands and thus applies the built-in + operation. In time1 + time2, where time1 and time2 are TimeHrMn objects, the compiler sees two TimeHrMn operands and thus invokes the programmer-defined function. A programmer can define several functions that overload the same operator, as long as each involves different types so that the compiler can determine which to invoke. The code below overloads the + operator twice in the TimeHrMn class. main() uses the + operator in 4 statements. The first + involves two TimeHrMn operands, so the compiler invokes the first operator+ function ("A"). The second + involves TimeHrMn and int operands, so the compiler invokes the second operator+ function ("B"). The third + involves two int operands, so the compiler invokes the built-in + operation. The fourth +, commented out, involves an int and TimeHrMn operands. Because no function has those operands ("B" has TimeHrMn and int, not int and TimeHrMn; order matters), that statement would generate a compiler error.
86
A programmer can overload the equality operator (==) to allow?
comparing objects of a programmer-defined class for equality.
87
How do you overload the == operator?
To overload ==, the programmer creates a function named operator== that returns bool and takes two const reference arguments of the class type for the left-hand-side and right-hand-side operands.
88
Give an example of how you overload the == operator.
Ex: To overload the == operator for a Review class, the programmer defines a function bool operator==(const Review& lhs, const Review& rhs).
89
OBSERVE: Overloading the == operator.
The programmer must also determine when two objects are considered equal. In the Review class below, two Review objects are equal if the objects have the same rating and comment.
90
Describe overloading the < operator.
A programmer can also overload relational operators like the less than operator (<). The < operator should return true if the object on the left side of the < operator is less than the object on the right side of the operator. In the Review class below, the operator< function returns true if the left Review operand has a lower rating than the right Review operand.
91
OBSERVE: Overloading the Reviews class' < operator.
92
A common approach is to first overload the ____ and ____ operators and then overload other comparison operators using ____ and ____.
93
The ____ function, defined in the C++ Standard Template Library's (STL) algorithms library, can sort vectors containing objects of programmer-defined classes.
sort()
94
To use sort(), a programmer must: (3 steps)
95
OBSERVE: Sorting a vector of Review objects.
96
An object's member function is called using the syntax object.Function(). The object variable before the function name is known as an ____ ____ of the member function because the compiler converts the call syntax object.Function(...) into a function call with a pointer to the object implicitly passed as a parameter. Ex: Function(object, ...).
Implicit parameter
97
Describe how within a member function, the implicitly-passed object pointer is accessible using the name "this".
98
OBSERVE: Using 'this' to refer to an object's member.
99
How do you use 'this' in class member functions and constructors?
100
OBSERVE: How a member function works
101
The ____ operator allocates memory for the given type and returns a pointer to the allocated memory.
New
102
If the type is a class, the new operator calls the class's ____ after allocating memory for the class's member variables.
Constructor
103
OBSERVE: The new operator allocates space for an object, then calls the constructor.
104
The new operator can pass arguments to the ____. The arguments must be in ____ following the class ____.
constructor, parenthesis, name
105
OBSERVE: Constructor arguments
106
When using a pointer to an object, the ____ ____ operator (->) allows access to the object's members with the syntax a->b instead of (*a).b.
member access operator
107
Give an example of using the member access operator (->) to point to an object.
Ex: If myPoint is a pointer to a Point object, myPoint->Print() calls the Print() member function.
108
OBSERVE: Using the member access operator.
109
The ____ operator deallocates (or frees) a block of memory that was allocated with the new operator.
Delete
110
The statement delete pointerVariable; ____ a memory block pointed to by pointerVariable
Deallocates
111
If pointerVariable is null, does the delete operator have an effect?
No.
112
READ: Delete operator
113
OBSERVE: Delete operator
114
The new operator creates a ____________________ of objects if the class name is followed by square brackets containing the array's length. How does the new operator do this?
Dynamically allocated array A single, contiguous chunk of memory is allocated for the array, then the default constructor is called for each object in the array. A compiler error occurs if the class does not have a constructor that can take 0 arguments.
115
The ____ operator is used to free an array allocated with the new operator.
delete[]
116
OBSERVE: Allocating and deleting an array of Point objects.