Ch.10 Flashcards
A member function that allows the user of the class to change the value in a data member is known as
a mutation
Why do you want to usually make data members private in a class?
A) so that no one can use the class
B) provide information hiding
C) ensure data integrity
D) provide data abstraction
E) B, C, and D
ensure data integrity
provide data abstraction.
provide information hiding.
You specify an individual member of a struct by using
the dot operator
Given the following class definition and the following member function header, which is the correct way to output the private data?
class Person { public: void outputPerson(ostream& out); private: int age; float weight; int id; };
void outputPerson(ostream& out) { //what goes here? }
out << age << weight << id;
What is wrong with the following structure definition?
struct MyStruct
{
int size;
float weight;
}
missing semicolon
Member functions of a class
may be in either section
To assign values to a structure variable, you use the
assignment operator
A class member function that automatically initializes the data members of a class is called
a constructor
Data members or member functions of a class that are declared to be private
may only be accessed by members of the class
Given the following class definition, how could you use the constructor to assign values to an object of this class?
class CDAccount { public: CDAccount( ); CDAccount(float interest, float newBalance); float getBalance( ); float getRate( ); void setRate(float interest); void setBalance(float newBalance); private: float balance, rate; };
and the following object declaration
CDAccount myAccount;
myAccount = CDAccount(myRate, myBalance)
If you design a class with private data members, and do not provide mutators and accessors, then
A) the class cannot be used. B) the data cannot be changed or viewed by anyone.
Given the following class definition, how would you declare an object of the class, so that the object automatically called the default constructor? class ItemClass { public: ItemClass( ); ItemClass(int newSize, float newCost); int getSize( ); float getCost( ); void setSize(int newSize); void setCost(float newCost); private: int size; float cost; };
ItemClass myItem;
A data type consisting of data members and operations on those members which can be used by a programmer without knowing the implementation details of the data type is called
an abstract data type
Given the following structure definition, what is the correct way to initialize a variable called today?
struct DateType
{
int day;
int month;
int year;
}
DateType today = {1,1,2000,0);
If you have a class named myPersonClass, which of the following correctly declare a constructor in the class definition?
myPersonClass( );
Given the following structure definitions, what is the correct way to print the person’s birth year?
struct DateType
{
int day;
int month;
int year;
}
struct PersonType
{
int age;
float weight;
DateType birthday;
}
PersonType person;
cout << person.birthday.year;
Given the following class, what would be the best declaration for a mutator function that allows the user of the class to change the age?
class Wine
{
public:
Wine( );
int getAge( );
float getCost( );
private:
int age;
float cost;
}
void setAge(int newAge);
Given the following class and object declaration, how would you print out the age and cost of a bottle of wine?
class Wine
{
public:
Wine( );
int getAge( );
float getCost( );
private:
int age;
float cost;
}
Wine bottle;
cout << bottle.getAge( ) << bottle.getCost( );
Which part of the ADT tells the programmer using it how to use it?
the interface
In a structure definition, the identifiers declared in the braces are called
member names
If you have a class with a member function called display(ostream& out), that will send the values in the class to the parameter stream, and you need to call that function from within another member function, how would you call it to print the data to the screen?
display(cout);
A member function that allows the user of the class to find out the value of a private data type is called a(n) ________.
accessor function
A structure variable is a collection of smaller values called ________ values
member
A member function that allows the user of the class to change the value of a private data type is called a ________.
mutator function
The constructor of a class that does not have any parameters is called a ________ constructor
default
The keyword ________ defines a structure type definition
struct
When a structure contains another structure variable as one of its members, it is known as a
hierarchical structure
What can a constructor return?
nothing
In the following class constructor definition, the part of the header starting with a single colon is called the ________.
BankAccount::BankAccount( ): balance(0), interest(0.0)
initialization section
The name of a constructor is
the name of the class
The double colon (::) is known as the ________ operator
scope resolution operator
A struct variable is declared differently from a predefined type such as an int
FALSE
Two different structure definitions may have the same member names
TRUE
The assignment operator may not be used with objects of a class
False
All constructors for a class must be private
FALSE
A structure can only be passed to a function as a call-by-value parameter
FALSE
A function may return a structure
True
A class member function may be private
TRUE