1/67
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced | Call with Kai |
|---|
No analytics yet
Send a link to your students to track their progress
What is the difference between a class and an instance of the class?
A class describes a data type.
An instance of a class is an object of the data type
that exists in memory.
What is the difference between the following Person structure and Person class?
struct Person
{
string name;
int age;
};
class Person
{
string name;
int age;
};
All members of a struct are public by default.
The members of a class are private by default.
What is the default access specification of class members?
private
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?
The function's name is getRadius.
It is a member of the Circle class.
A contractor uses a blueprint to build a set of identical houses. Are classes analogous to the
blueprint or the houses?
A class is analogous to the blueprint
What is a mutator function? What is an accessor function?
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.
Is it a good idea to make member variables private? Why or why not?
Yes. protects the variables from being manipulated by code outside the class, and prevents them from receiving invalid data
Can you think of a good reason to avoid writing statements in a class member function that use cout or cin ?
Classes should avoid handling input and output directly, instead using member functions to store and retrieve data so programmers can choose their own I/O methods.
Under what circumstances should a member function be private?
Functions meant only for a class’s internal operations, such as initializing or destroying data, should remain inaccessible outside the class to prevent them from being used at the wrong time.
What is a constructor? What is a destructor?.
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
What is a default constructor? Is it possible to have more than one default constructor?
A default constructor is a constructor that is called without any arguments.
It is not possible to have more than one default constructor
Is it possible to have more than one constructor? Is it possible to have more than one
destructor?
Yes, it is possible to have more than one constructor. It is not possible to have more than one destructor
If a class object is dynamically allocated in memory, does its constructor execute? If so,when?
Yes, the constructor executes when the object is created.
When defining an array of class objects, how do you pass arguments to the constructor for each object in the array?
You specify the arguments for each object individually in an initialization list.
What are a class’s responsibilities?
the things that the class is responsible for knowing and the actions that the class is responsible for doing.
How do you identify the classes in a problem domain description?
Identify all the nouns 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.
The two common programming methods in practice today are _________ and
_________.
procedural programming, object-oriented programming
________ programming is centered around functions or procedures.
procedural
________ programming is centered around objects
object oriented
________ is an object’s ability to contain and manipulate its own data.
encapsulation
In C++ the _________ is the construct primarily used to create objects.
class
A class is very similar to a(n) _________.
structure
A(n) _________ is a key word inside a class declaration that establishes a member’s
accessibility.
access specifier
The default access specification of class members is _________.
private
The default access specification of a struct in C++ is _________.
public
Defining a class object is often called the _________ of a class.
instantiation
Members of a class object may be accessed through a pointer to the object by using the
_________ operator.
->
If you were writing the declaration of a class named Canine , what would you name the file it
was stored in? _________
canine.h
If you were writing the external definitions of the Canine class’s member functions, you
would save them in a file named _________.
canine.cpp
When a member function’s body is written inside a class declaration, the function is
_________.
inline
A(n) _________ is automatically called when an object is created
constructor
A(n) _________ is a member function with the same name as the class.
constructor
________ are useful for performing initialization or setup routines in a class object.
constructors
Constructors cannot have a(n) _________ type.
return
A(n) _________ constructor is one that requires no arguments.
default
A(n) _________ is a member function that is automatically called when an object is
destroyed.
destructor
A destructor has the same name as the class, but is preceded by a(n) _________ character.
~
Like constructors, destructors cannot have a(n) _________ type.
return
A constructor whose arguments all have default values is a(n) _________ constructor.
default
A class may have more than one constructor, as long as each has a different _________.
parameter list
A class may only have one default _________ and one _________.
constructor, destructor
A(n) _________ may be used to pass arguments to the constructors of elements in an object
array.
initialization list
T F Private members must be declared before public members.
false
T F Class members are private by default.
true
T F Members of a struct are private by default
false
T F Classes and structures in C++ are very similar.
true
T F All private members of a class must be declared together.
False
T F All public members of a class must be declared together.
false
T F It is legal to define a pointer to a class object.
true
T F You can use the new operator to dynamically allocate an instance of a class.
true
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.
false
T F Constructors do not have to have the same name as the class.
false
T F Constructors may not have a return type.
true
T F Constructors cannot take arguments
false
T F Destructors cannot take arguments.
true
T F Destructors may return a value.
false
T F Constructors may have default arguments.
true
T F Member functions may be overloaded
true
T F A class may not have a constructor with no parameter list, and a constructor whose
arguments all have default values.
true
T F A class may only have one destructor.
true
T F When an array of objects is defined, the constructor is only called for the first element.
false
T F When an array of objects is defined, the constructor is only called for the first element.
false
T F A class’s responsibilities are the things the class is responsible for knowing, and actions
the class must perform.
true
73. class Circle:
{
private
double centerX;
double centerY;
double radius;
public
setCenter(double, double);
setRadius(double);
}
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.
#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;
}
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.
#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;
}
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.
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;
}
Both constructors are considered the default constructor.