Chapter 13 Flashcards
1
Q
- What is the difference between a class and an instance of the class?
A
- A class describes a data type. An instance of a class is an object of the data type that exists in memory.
2
Q
. What is the difference between the following Person structure and Person class?
struct Person
{
string name;
int age;
};
class Person
{
string name;
int age;
};
A
- All members of a struct are public by default. The members of a class, however, are private by default.
3
Q
- What is the default access specification of class members?
A
private
4
Q
- Look at the following function header for a member function.
void Circle::getRadius()
What is the name of the function?
What class is the function a member of?
A
- The function’s name is getRadius. It is a member of the Circle class.
5
Q
- A contractor uses a blueprint to build a set of identical houses. Are classes analogous to the blueprint or the houses?
A
- A class is analogous to the blueprint.
6
Q
- What is a mutator function? What is an accessor function?
A
- A mutator is a member function that stores a value in a private member variable, or in some way changes an attribute. An accessor is a member function that retrieves the value stored in a private member variable.
7
Q
- Is it a good idea to make member variables private? Why or why not?
A
- Yes it is. This protects the variables from being directly manipulated by code outside the class, and prevents them from receiving invalid data.
8
Q
- Can you think of a good reason to avoid writing statements in a class member function that use cout or cin ?
A
- Unless a class is specifically designed to perform I/O, operations like user input and output are best left to the person designing the application. Classes should provide member functions for retrieving any important data without displaying them on the screen. Likewise, they should provide member functions that store data into private member variables without using cin. This allows a programmer to use the class without being locked into a particular method of performing I/O.
9
Q
- Under what circumstances should a member function be private?
A
- When the function is necessary for internal processing, but not useful to the program outside the class. In some cases a class may contain member functions that initialize member variables or destroy their contents. Those functions should not be accessible by an external part of program because they may be called at the wrong time.
10
Q
- What is a constructor? What is a destructor?
A
- A constructor is a member function that is automatically called when a class object is created. A destructor is a member function that is automatically called when a class object is destroyed.
11
Q
- What is a default constructor? Is it possible to have more than one default constructor?
A
- A default constructor is a constructor that is called without any arguments. It is not possible to have more than one default constructor.
12
Q
- Is it possible to have more than one constructor? Is it possible to have more than one destructor?
A
- Yes, it is possible to have more than one constructor. It is not possible to have more than one destructor.
13
Q
- If a class object is dynamically allocated in memory, does its constructor execute? If so, when?
A
- Yes, the constructor executes when the object is created.
14
Q
- When defining an array of class objects, how do you pass arguments to the constructor for each object in the array?
A
- You specify the arguments for each object individually in an initialization list.
15
Q
- What are a class’s responsibilities?
A
- A class’s responsibilities are the things that the class is responsible for knowing and the actions that the class is responsible for doing.
16
Q
- How do you identify the classes in a problem domain description?
A
- Identify all the nouns (including pronouns and noun phrases) in the problem domain description. Each of these is a potential class. Then, refine the list to include only the classes that are relevant to the problem.
17
Q
- The two common programming methods in practice today are _________ and
_________.
A
- procedural programming, object-oriented programming
18
Q
- ________ programming is centered around functions or procedures.
A
procedural
19
Q
- ________ programming is centered around objects.
A
- object-oriented
20
Q
- ________ is an object’s ability to contain and manipulate its own data.
A
encapsulation
21
Q
- In C++ the _________ is the construct primarily used to create objects.
A
class
22
Q
- A class is very similar to a(n) _________.
A
structure
23
Q
- A(n) _________ is a key word inside a class declaration that establishes a member’s accessibility.
A
- access specifier
24
Q
- The default access specification of class members is _________.
A
private
25
Q
- Defining a class object is often called the _________ of a class.
A
instantiation
26
Q
- The default access specification of a struct in C++ is _________.
A
public
27
Q
- Members of a class object may be accessed through a pointer to the object by using the _________ operator.
A
- ->