• Home
  • Textbooks
  • Starting out with C++: From Control Structures through Objects
  • Introduction to Classes

Starting out with C++: From Control Structures through Objects

Tony Gaddis

Chapter 13

Introduction to Classes - all with Video Answers

Educators


Chapter Questions

Problem 1

What is the difference between a class and an instance of the class?

Check back soon!

Problem 2

What is the difference between the following Person structure and Person class?
struct Person
{
string name;
int age;
};
class Person
{
string name;
int age;
};

Check back soon!

Problem 3

What is the default access specification of class members?

Check back soon!

Problem 4

Look at the following function header for a member function:
void Circle::getRadius()
What is the name of the function?
Of what class is the function a member?

Check back soon!

Problem 5

A contractor uses a blueprint to build a set of identical houses. Are classes analogous to the blueprint or the houses?

Check back soon!

Problem 6

What is a mutator function? What is an accessor function?

Check back soon!

Problem 7

Is it a good idea to make member variables private? Why or why not?

Check back soon!

Problem 8

Can you think of a good reason to avoid writing statements in a class member function that use cout or cin?

Check back soon!

Problem 9

Under what circumstances should a member function be private?

Check back soon!

Problem 10

What is a constructor? What is a destructor?

Check back soon!

Problem 11

What is a default constructor? Is it possible to have more than one default constructor?

Check back soon!

Problem 12

Is it possible to have more than one constructor? Is it possible to have more than one destructor?

Check back soon!

Problem 13

If a class object is dynamically allocated in memory, does its constructor execute? If so, when?

Check back soon!

Problem 14

When defining an array of class objects, how do you pass arguments to the constructor for each object in the array?

Check back soon!

Problem 15

What are a class’s responsibilities?

Check back soon!

Problem 16

How do you identify the classes in a problem domain description?

Check back soon!

Problem 17

The two common programming methods in practice today are _________ and _________.

Check back soon!

Problem 18

_________ programming is centered around functions or procedures.

Check back soon!

Problem 19

_________ programming is centered around objects.

Check back soon!
00:44

Problem 20

_________ is an object’s ability to contain and manipulate its own data.

Aditya Sood
Aditya Sood
Numerade Educator

Problem 21

In C++, the _________ is the construct primarily used to create objects.

Check back soon!

Problem 22

A class is very similar to a(n) _________.

Check back soon!

Problem 23

A(n) _________ is a key word inside a class declaration that establishes a member’s accessibility.

Check back soon!

Problem 24

The default access specification of class members is _________.

Check back soon!

Problem 25

The default access specification of a struct in C++ is _________.

Check back soon!

Problem 26

Defining a class object is often called the _________ of a class.

Check back soon!

Problem 27

Members of a class object may be accessed through a pointer to the object by using the _________ operator.

Check back soon!

Problem 28

If you were writing the declaration of a class named Canine, what would you name the file it was stored in? _________

Check back soon!

Problem 29

If you were writing the external definitions of the Canine class’s member functions, you would save them in a file named _________.

Check back soon!

Problem 30

When a member function’s body is written inside a class declaration, the function is _________.

Check back soon!

Problem 31

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

Check back soon!

Problem 32

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

Check back soon!

Problem 33

_________ are useful for performing initialization or setup routines in a class object.

Check back soon!

Problem 34

Constructors cannot have a(n) _________ type.

Check back soon!

Problem 35

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

Check back soon!

Problem 36

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

Check back soon!

Problem 37

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

Check back soon!

Problem 38

Like constructors, destructors cannot have a(n) _________ type.

Check back soon!

Problem 39

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

Check back soon!

Problem 40

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

Check back soon!

Problem 41

A class may only have one default _________ and one _________.

Check back soon!

Problem 42

A(n) _________ may be used to pass arguments to the constructors of elements in an object array.

Check back soon!
00:12

Problem 43

Write set and get functions to access the radius variable, and a function named getArea that returns the area of the circle. The area is calculated as
3.14159 * radius * radius

Amy Jiang
Amy Jiang
Numerade Educator

Problem 44

Add a default constructor to the Circle class in Question 43. The constructor should initialize the radius member to 0 .

Check back soon!

Problem 45

Add an overloaded constructor to the Circle class in Question 44. The constructor should accept an argument and assign its value to the radius member variable.

Check back soon!

Problem 46

Write a statement that defines an array of five objects of the Circle class in Question 45. Let the default constructor execute for each element of the array.

Check back soon!

Problem 47

Write a statement that defines an array of five objects of the Circle class in Question 45. Pass the following arguments to the elements' constructor: $12,7,9,14$, and 8 .

Check back soon!

Problem 48

Write a for loop that displays the radius and area of the circles represented by the array you defined in Question 47.

Check back soon!

Problem 49

If the items on the following list appeared in a problem domain description, which would be potential classes?
$$
\begin{array}{lll}
\text { Animal } & \text { Medication } & \text { Nurse } \\
\text { Inoculate } & \text { Operate } & \text { Advertise } \\
\text { Doctor } & \text { Invoice } & \text { Measure } \\
\text { Patient } & \text { Client } & \text { Customer }
\end{array}
$$

Check back soon!

Problem 50

Look at the following description of a problem domain:
The bank offers the following types of accounts to its customers: savings accounts, checking accounts, and money market accounts. Customers are allowed to deposit money into an account (thereby increasing its balance), withdraw money from an account (thereby decreasing its balance), and earn interest on the account. Each account has an interest rate.
Assume you are writing an application that will calculate the amount of interest earned for a bank account.
A) Identify the potential classes in this problem domain.
B) Refine the list to include only the necessary class or classes for this problem.
C) Identify the responsibilities of the class or classes.

Check back soon!

Problem 51

True or False
T F Private members must be declared before public members.

Check back soon!

Problem 52

True or False
T F Class members are private by default.

Check back soon!

Problem 53

True or False
T F Members of a struct are private by default.

Check back soon!

Problem 54

True or False
T F Classes and structures in C++ are very similar.

Check back soon!

Problem 55

True or False
T F All private members of a class must be declared together.

Check back soon!

Problem 56

True or False
T F All public members of a class must be declared together.

Check back soon!

Problem 57

True or False
T F It is legal to define a pointer to a class object.

Check back soon!

Problem 58

True or False
T F You can use the new operator to dynamically allocate an instance of a class.

Check back soon!

Problem 59

True or False
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.

Check back soon!

Problem 60

True or False
T F Constructors do not have to have the same name as the class.

Check back soon!

Problem 61

True or False
T F Constructors may not have a return type.

Check back soon!

Problem 62

True or False
T F Constructors cannot take arguments.

Check back soon!

Problem 63

True or False
T F Destructors cannot take arguments.

Check back soon!

Problem 64

True or False
T F Destructors may return a value.

Check back soon!

Problem 65

True or False
T F Constructors may have default arguments.

Check back soon!

Problem 66

True or False
T F Member functions may be overloaded.

Check back soon!

Problem 67

True or False
T F Constructors may not be overloaded.

Check back soon!

Problem 68

True or False
T F A class may not have a constructor with no parameter list, and a constructor whose arguments all have default values.

Check back soon!

Problem 69

True or False
T F A class may only have one destructor.

Check back soon!

Problem 70

True or False
T F When an array of objects is defined, the constructor is only called for the first element.

Check back soon!

Problem 71

True or False
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.

Check back soon!

Problem 72

True or False
T F A class’s responsibilities are the things the class is responsible for knowing, and actions the class must perform.

Check back soon!

Problem 73

Each of the following class declarations or programs contain errors. Find as many as possible.

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

Check back soon!

Problem 74

Each of the following class declarations or programs contain errors. Find as many as possible.

#include <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 >> "What is your weight? ";
cin << earth;
Moon lunar(earth);
cout << "On the moon you would weigh "
<<lunar.getMoonWeight() << endl;
return 0;
}

Check back soon!

Problem 75

Each of the following class declarations or programs contain errors. Find as many as possible.

#include <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 << "The weight is " << bar.weight << endl;
return 0;
}

Check back soon!

Problem 76

Each of the following class declarations or programs contain errors. Find as many as possible.

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;
}

Check back soon!