Deck 7: Constructors and Other Tools

Full screen (f)
exit full mode
Question
A constructor is like a function.It can return any type value needed.
Use Space or
up arrow
down arrow
to flip the card.
Question
A constructor is a special kind of member function.It is automatically called when an object of that class is declared.
Question
A constructor usually terminates by falling off the end of its block,but a return statement is permitted if there is no argument add following the word return..
Question
Assignment behaves essentially the same for vectors as for arrays.
Question
A local class and a nested class are the same thing.
Question
If v is a vector and i is an int variable,then in the following the value of i can be any nonnegative int value:
v[i] = i;
Question
The set of integers ranging from -MAXINT to MAXINT make up the int data type.
Question
A class may not have another class type object as a member.
Question
A constructor is always named construct with class name attached.If the class is Foo,then the constructor name is constructFoo.
Question
It is valid to initialize member variables in a class on the same line in which the variable is declared.For example,the following sets xx to 1000:
class A
{
public:
A(){}
private:
int xx = 1000;
};
Question
A static variable of a class cannot be changed.
Question
The keyword static is used in a static function declaration in a class but not in the function definition.
Question
size and capacity of a vector are two names for the same thing.
Question
The functions or data members declared in the private: section of a class can be accessed only in the definition those functions declared in that class.Note that friend functions are also declared (but not defined)in the class to which they are friends,so they also have access to private as well as public members.
Question
Any use of the keyword const is a promise to the compiler,and a request to the compiler to enforce the promise.What promises?
Question
Vector assignment is well behaved.
Question
You can write a class that is useful with all its constructors in the private section.
Question
A constructor can be called implicitly or explicitly.(Implicitly means the compiler did it for you. )In the interest of uniformity in answers,please use class A;for your examples.
Question
Inline functions are always more efficent than noninline functions.
Question
It is legal to call a constructor as a member function of an object of a class,as in
class A
{
public:
A(){}
A(int x,int y):xx(x),yy(y){}
// other members
private:
int xx;
int yy;
};
int main()
{
A w;
w.A(2,3);// Is this legal?
}
Question
Which of the following are legal access to the class or struct members? Assume each is outside of the class member definitions,
struct S class C class D
{ { {
int x;int x;public:
int y;int y;int x;
} private: int y;
S s;int z;private:
};int z;
C c;};
D d;
a)s.x
b)c.x
c)d.x
d)c.z
e)d.z
Question
Describe the differences between a call to an inline function member and a function that is not declared inline..What advantages are there to inline? What disadvantages?
Question
A constructor

A)can only be used to initialize
B)must initialize all member variables
C)can do anything any other method can do.
D)usually initializes all,or most,member variables
Question
Describe in terms of who needs access to class members why the public members should come first in a class definition.
Question
Given the definitions below.Rewrite the definition of this class so that functions f()const and g(const A& x)are inline.
const int x = 17;
class A
{
public:
A( );
A(int n);
int f( )const;
int g(const A& x);
private:
int i;
};
Question
Suppose you have a class whose objects are very,very large.Briefly,describe the advantages and drawbacks of call-by-value and call-by-reference for large objects.Describe a parameter passing mechanism that will allow the safety of call-by-value and the efficiency of call-by-reference.
Question
Explain why data members,in particular should be placed in the private section of a class.
Question
Which of the following are accurate comparisons between call-by-value and const call-by-reference?

A)Both protect against changing the caller's argument.
B)Both are very fast for all sizes of objects.
C)Call-by-value copies the argument whereas const call-by-reference does not
D)Call by value uses more memory than const call-by-reference in making the copy.
Question
A class member that is to be shared among all objects of a class is called

A)A const member
B)A reference member
C)A static member.
D)A value member
E)A function member
Question
Why is it an error to add a const modifier,as shown to the declaration for the member function input given here?
class BankAccount
{
public:
void input( )const;
// other members
};
Question
What is a default constructor? When does a class not have a default constructor?
Question
If we use an out of range index with a vector,there be an error message from the compiler.
Unlock Deck
Sign up to unlock the cards in this deck!
Unlock Deck
Unlock Deck
1/32
auto play flashcards
Play
simple tutorial
Full screen (f)
exit full mode
Deck 7: Constructors and Other Tools
1
A constructor is like a function.It can return any type value needed.
False
2
A constructor is a special kind of member function.It is automatically called when an object of that class is declared.
True
3
A constructor usually terminates by falling off the end of its block,but a return statement is permitted if there is no argument add following the word return..
True
4
Assignment behaves essentially the same for vectors as for arrays.
Unlock Deck
Unlock for access to all 32 flashcards in this deck.
Unlock Deck
k this deck
5
A local class and a nested class are the same thing.
Unlock Deck
Unlock for access to all 32 flashcards in this deck.
Unlock Deck
k this deck
6
If v is a vector and i is an int variable,then in the following the value of i can be any nonnegative int value:
v[i] = i;
Unlock Deck
Unlock for access to all 32 flashcards in this deck.
Unlock Deck
k this deck
7
The set of integers ranging from -MAXINT to MAXINT make up the int data type.
Unlock Deck
Unlock for access to all 32 flashcards in this deck.
Unlock Deck
k this deck
8
A class may not have another class type object as a member.
Unlock Deck
Unlock for access to all 32 flashcards in this deck.
Unlock Deck
k this deck
9
A constructor is always named construct with class name attached.If the class is Foo,then the constructor name is constructFoo.
Unlock Deck
Unlock for access to all 32 flashcards in this deck.
Unlock Deck
k this deck
10
It is valid to initialize member variables in a class on the same line in which the variable is declared.For example,the following sets xx to 1000:
class A
{
public:
A(){}
private:
int xx = 1000;
};
Unlock Deck
Unlock for access to all 32 flashcards in this deck.
Unlock Deck
k this deck
11
A static variable of a class cannot be changed.
Unlock Deck
Unlock for access to all 32 flashcards in this deck.
Unlock Deck
k this deck
12
The keyword static is used in a static function declaration in a class but not in the function definition.
Unlock Deck
Unlock for access to all 32 flashcards in this deck.
Unlock Deck
k this deck
13
size and capacity of a vector are two names for the same thing.
Unlock Deck
Unlock for access to all 32 flashcards in this deck.
Unlock Deck
k this deck
14
The functions or data members declared in the private: section of a class can be accessed only in the definition those functions declared in that class.Note that friend functions are also declared (but not defined)in the class to which they are friends,so they also have access to private as well as public members.
Unlock Deck
Unlock for access to all 32 flashcards in this deck.
Unlock Deck
k this deck
15
Any use of the keyword const is a promise to the compiler,and a request to the compiler to enforce the promise.What promises?
Unlock Deck
Unlock for access to all 32 flashcards in this deck.
Unlock Deck
k this deck
16
Vector assignment is well behaved.
Unlock Deck
Unlock for access to all 32 flashcards in this deck.
Unlock Deck
k this deck
17
You can write a class that is useful with all its constructors in the private section.
Unlock Deck
Unlock for access to all 32 flashcards in this deck.
Unlock Deck
k this deck
18
A constructor can be called implicitly or explicitly.(Implicitly means the compiler did it for you. )In the interest of uniformity in answers,please use class A;for your examples.
Unlock Deck
Unlock for access to all 32 flashcards in this deck.
Unlock Deck
k this deck
19
Inline functions are always more efficent than noninline functions.
Unlock Deck
Unlock for access to all 32 flashcards in this deck.
Unlock Deck
k this deck
20
It is legal to call a constructor as a member function of an object of a class,as in
class A
{
public:
A(){}
A(int x,int y):xx(x),yy(y){}
// other members
private:
int xx;
int yy;
};
int main()
{
A w;
w.A(2,3);// Is this legal?
}
Unlock Deck
Unlock for access to all 32 flashcards in this deck.
Unlock Deck
k this deck
21
Which of the following are legal access to the class or struct members? Assume each is outside of the class member definitions,
struct S class C class D
{ { {
int x;int x;public:
int y;int y;int x;
} private: int y;
S s;int z;private:
};int z;
C c;};
D d;
a)s.x
b)c.x
c)d.x
d)c.z
e)d.z
Unlock Deck
Unlock for access to all 32 flashcards in this deck.
Unlock Deck
k this deck
22
Describe the differences between a call to an inline function member and a function that is not declared inline..What advantages are there to inline? What disadvantages?
Unlock Deck
Unlock for access to all 32 flashcards in this deck.
Unlock Deck
k this deck
23
A constructor

A)can only be used to initialize
B)must initialize all member variables
C)can do anything any other method can do.
D)usually initializes all,or most,member variables
Unlock Deck
Unlock for access to all 32 flashcards in this deck.
Unlock Deck
k this deck
24
Describe in terms of who needs access to class members why the public members should come first in a class definition.
Unlock Deck
Unlock for access to all 32 flashcards in this deck.
Unlock Deck
k this deck
25
Given the definitions below.Rewrite the definition of this class so that functions f()const and g(const A& x)are inline.
const int x = 17;
class A
{
public:
A( );
A(int n);
int f( )const;
int g(const A& x);
private:
int i;
};
Unlock Deck
Unlock for access to all 32 flashcards in this deck.
Unlock Deck
k this deck
26
Suppose you have a class whose objects are very,very large.Briefly,describe the advantages and drawbacks of call-by-value and call-by-reference for large objects.Describe a parameter passing mechanism that will allow the safety of call-by-value and the efficiency of call-by-reference.
Unlock Deck
Unlock for access to all 32 flashcards in this deck.
Unlock Deck
k this deck
27
Explain why data members,in particular should be placed in the private section of a class.
Unlock Deck
Unlock for access to all 32 flashcards in this deck.
Unlock Deck
k this deck
28
Which of the following are accurate comparisons between call-by-value and const call-by-reference?

A)Both protect against changing the caller's argument.
B)Both are very fast for all sizes of objects.
C)Call-by-value copies the argument whereas const call-by-reference does not
D)Call by value uses more memory than const call-by-reference in making the copy.
Unlock Deck
Unlock for access to all 32 flashcards in this deck.
Unlock Deck
k this deck
29
A class member that is to be shared among all objects of a class is called

A)A const member
B)A reference member
C)A static member.
D)A value member
E)A function member
Unlock Deck
Unlock for access to all 32 flashcards in this deck.
Unlock Deck
k this deck
30
Why is it an error to add a const modifier,as shown to the declaration for the member function input given here?
class BankAccount
{
public:
void input( )const;
// other members
};
Unlock Deck
Unlock for access to all 32 flashcards in this deck.
Unlock Deck
k this deck
31
What is a default constructor? When does a class not have a default constructor?
Unlock Deck
Unlock for access to all 32 flashcards in this deck.
Unlock Deck
k this deck
32
If we use an out of range index with a vector,there be an error message from the compiler.
Unlock Deck
Unlock for access to all 32 flashcards in this deck.
Unlock Deck
k this deck
locked card icon
Unlock Deck
Unlock for access to all 32 flashcards in this deck.