Chapter 13 Flashcards

1
Q
  1. What is the difference between a class and an instance of the class?
A
  1. A class describes a data type. An instance of a class is an object of the data type that exists in memory.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
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
  1. All members of a struct are public by default. The members of a class, however, are private by default.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q
  1. What is the default access specification of class members?
A

private

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q
  1. 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
  1. The function’s name is getRadius. It is a member of the Circle class.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q
  1. A contractor uses a blueprint to build a set of identical houses. Are classes analogous to the blueprint or the houses?
A
  1. A class is analogous to the blueprint.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q
  1. What is a mutator function? What is an accessor function?
A
  1. 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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q
  1. Is it a good idea to make member variables private? Why or why not?
A
  1. Yes it is. This protects the variables from being directly manipulated by code outside the class, and prevents them from receiving invalid data.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q
  1. Can you think of a good reason to avoid writing statements in a class member function that use cout or cin ?
A
  1. 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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q
  1. Under what circumstances should a member function be private?
A
  1. 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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q
  1. What is a constructor? What is a destructor?
A
  1. 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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q
  1. What is a default constructor? Is it possible to have more than one default constructor?
A
  1. A default constructor is a constructor that is called without any arguments. It is not possible to have more than one default constructor.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q
  1. Is it possible to have more than one constructor? Is it possible to have more than one destructor?
A
  1. Yes, it is possible to have more than one constructor. It is not possible to have more than one destructor.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q
  1. If a class object is dynamically allocated in memory, does its constructor execute? If so, when?
A
  1. Yes, the constructor executes when the object is created.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q
  1. When defining an array of class objects, how do you pass arguments to the constructor for each object in the array?
A
  1. You specify the arguments for each object individually in an initialization list.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q
  1. What are a class’s responsibilities?
A
  1. A class’s responsibilities are the things that the class is responsible for knowing and the actions that the class is responsible for doing.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q
  1. How do you identify the classes in a problem domain description?
A
  1. 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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q
  1. The two common programming methods in practice today are _________ and
    _________.
A
  1. procedural programming, object-oriented programming
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q
  1. ________ programming is centered around functions or procedures.
A

procedural

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q
  1. ________ programming is centered around objects.
A
  1. object-oriented
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q
  1. ________ is an object’s ability to contain and manipulate its own data.
A

encapsulation

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q
  1. In C++ the _________ is the construct primarily used to create objects.
A

class

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q
  1. A class is very similar to a(n) _________.
A

structure

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q
  1. A(n) _________ is a key word inside a class declaration that establishes a member’s accessibility.
A
  1. access specifier
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q
  1. The default access specification of class members is _________.
A

private

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
Q
  1. Defining a class object is often called the _________ of a class.
A

instantiation

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
26
Q
  1. The default access specification of a struct in C++ is _________.
A

public

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
27
Q
  1. Members of a class object may be accessed through a pointer to the object by using the _________ operator.
A
  1. ->
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
28
Q
  1. If you were writing the declaration of a class named Canine , what would you name the file it was stored in? _________
A

canine.h

29
Q
  1. If you were writing the external definitions of the Canine class’s member functions, you would save them in a file named _________.
A
  1. canine.cpp
30
Q
  1. When a member function’s body is written inside a class declaration, the function is
A

inline

31
Q
  1. A(n) _________ is automatically called when an object is created.
A

constructor

32
Q
  1. A(n) _________ is a member function with the same name as the class.
A

constructor

33
Q
  1. ________ are useful for performing initialization or setup routines in a class object.
A

constructors

34
Q
  1. Constructors cannot have a(n) _________ type.
A

return

35
Q
  1. A(n) _________ constructor is one that requires no arguments.
A

default

36
Q
  1. A(n) _________ is a member function that is automatically called when an object is destroyed.
A

destructor

37
Q
  1. A destructor has the same name as the class, but is preceded by a(n) _________ character.
A

~

38
Q
  1. Like constructors, destructors cannot have a(n) _________ type.
A

return

39
Q
  1. A constructor whose arguments all have default values is a(n) _________ constructor.
A

default

40
Q
  1. A class may have more than one constructor, as long as each has a different _________.
A

parameter list

41
Q
  1. A class may only have one default _________ and one _________.
A
  1. constructor, destructor
42
Q
  1. A(n) _________ may be used to pass arguments to the constructors of elements in an object array.
A
  1. initialization list
43
Q
  1. T F Private members must be declared before public members.
A

false

44
Q
  1. T F Class members are private by default.
A

true

45
Q
  1. T F Members of a struct are private by default.
A

false

46
Q
  1. T F Classes and structures in C++ are very similar.
A

true

47
Q
  1. T F All private members of a class must be declared together.
A

false

48
Q
  1. T F All public members of a class must be declared together.
A

false

49
Q
  1. T F It is legal to define a pointer to a class object.
A

true

50
Q
  1. T F You can use the new operator to dynamically allocate an instance of a class.
A

true

51
Q
  1. T F A private member function may be called from a statement outside the class, as long as the statement is in the same program as the class declaration.
A

false

52
Q
  1. T F Constructors do not have to have the same name as the class.
A

false

53
Q
  1. T F Constructors may not have a return type.
A

true

54
Q
  1. T F Constructors cannot take arguments.
A

false

55
Q
  1. T F Destructors cannot take arguments.
A

true

56
Q
  1. T F Destructors may return a value.
A
  1. false
57
Q
  1. T F Constructors may have default arguments.
A

true

58
Q
  1. T F Member functions may be overloaded.
A

true

59
Q
  1. T F Constructors may not be overloaded.
A

false

60
Q
  1. T F A class may not have a constructor with no parameter list, and a constructor whose arguments all have default values.
A

true

61
Q
  1. T F A class may only have one destructor.
A

true

62
Q
  1. T F When an array of objects is defined, the constructor is only called for the first element.
A

false

63
Q
  1. T F To find the classes needed for an object-oriented application, you identify all of the verbs in a description of the problem domain.
A

false

64
Q
  1. T F A class’s responsibilities are the things the class is responsible for knowing, and actions the class must perform.
A

true

65
Q
  1. class Circle:
    {
    private
    double centerX;
    double centerY;
    double radius;
    public
    setCenter(double, double);
    setRadius(double);
    }

Find the Errors

A

There should not be a colon after the word Circle.
Colons should appear after the words private and public.
A semicolon should appear after the closing brace.

66
Q
  1. # include <iostream></iostream>using namespace std;
    Class Moon;
    {
    Private;
    double earthWeight;
    double moonWeight;
    Public;
    moonWeight(double ew);
    { earthWeight = ew; moonWeight = earthWeight / 6; }
    double getMoonWeight();
    { return moonWeight; }
    }
    int main()
    {
    double earth;
    cout&raquo_space; “What is your weight? “;
    cin &laquo_space;earth;
    Moon lunar(earth);
    cout &laquo_space;“On the moon you would weigh “
    «lunar.getMoonWeight() &laquo_space;endl;
    return 0;
    }

Find the Errors

A

The semicolon should not appear after the word Moon.
The first character of the words private and public should not be capitalized.
There should be a colon, not a semicolon, following the words private and public.
Semicolons should not appear in the member function headers.
In function main an argument is passed to a constructor that does not exist in the
Moon class.
The moonWeight member function should have been called prior to getMoonWeight.

67
Q
  1. # include <iostream></iostream>using namespace std;
    class DumbBell;
    {
    int weight;
    public:
    void setWeight(int);
    };
    void setWeight(int w)
    {
    weight = w;
    }
    int main()
    {
    DumbBell bar;
    DumbBell(200);
    cout &laquo_space;“The weight is “ &laquo_space;bar.weight &laquo_space;endl;
    return 0;
    }

Find the Errors

A

The semicolon should not appear after the word DumbBell.
The function header for setWeight should appear as:
void DumbBell::setWeight(int w)
The line that reads:
DumbBell(200);
should read:
bar.setWeight(200);

bar.weight cannot be accessed outside of the class because no access specifier appeared before it in the class, making the variable private to the class by default. This means the cout statement will not work.

68
Q
  1. class Change
    {
    public:
    int pennies;
    int nickels;
    int dimes;
    int quarters;
    Change()
    { pennies = nickels = dimes = quarters = 0; }
    Change(int p = 100, int n = 50, d = 50, q = 25);
    };
    void Change::Change(int p, int n, d, q)
    {
    pennies = p;
    nickels = n;
    dimes = d;
    quarters = q;
    }

Find the Errors

A
  1. Both constructors are considered the default constructor.