Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Short Answers Short Answers Section 2.1 - 2.2 Introduction to Classes Here is part of the throttle declaration: class throttle { public: void shut_off(); double

Short Answers

Short Answers Section 2.1 - 2.2 Introduction to Classes

Here is part of the throttle declaration:

class throttle

{

public:

void shut_off();

double flow();

...

private:

int position;

};

Write several lines of C++ code to declare a throttle called quiz, activate the member function that shuts off quiz's flow, and then print the current flow from quiz.

The public part of the throttle declaration from class notes is shown below. Mark each function member header as follows: Mark C for any constructor; mark X for any function that is forbidden from changing the throttle's data fields.

class throttle

{

public:

throttle( );

throttle(int size);

void shut_off( );

void shift(int amount);

double flow( ) const;

bool is_on( ) const;

...

What is an automatic default constructor, and what does it do?

What is an inline member function, and when might one be used? Give a small example as part of your answer.

Short Answers Section 2.3 The Header File and the Implementation File

What is a macro guard in a header file, and what is its purpose? Give a small example as part of your answer.

Suppose that the throttle class is implemented with a header file (throttle.h) and an implementation file (throttle.cxx), and that it is part of the namespace main_savitch_2A. Write the include directive and the using directive that are needed for a program to use the throttle class.

Short Answers Section 2.4 Classes and Parameters

When is it appropriate to use a const reference parameter? Give a small example as part of your answer.

Suppose a function has a parameter named x, and the body of the function changes the value of x. When should x be a value parameter? When should it be a reference parameter? With this function, could x ever be a const reference parameter?

Write one clear sentence telling me when it would be appropriate to use a const reference parameter.

Write one clear sentence telling me when it would be appropriate to use a reference parameter.

Here is a small class definition:

class small

{

public:

small( );

void k() const;

void h(int i);

friend f(Small z);

private:

int size;

};

Suppose that x and y are both small objects. Write the word "legal" or "illegal" in each location of this table to indicate whether the indicated statement is legal or illegal in these locations:

Statement

In a main program

In the const member function k

In the friend function f

x = y;

.

.

.

x.size = y.size;

.

.

.

x.size = 3;

.

.

.

x.h(42);

.

.

.

Suppose that you define a new class called foo. For two foo objects x and y, you would like the expression x+y to be a new foo object. What is the prototype of the function that you must write to enable expressions such as x+y?

I have written a class with an operator + which is not a member function, but the operator + implementation does access the private member variables of objects in the class. What did I need to do to gain this access?

Write one clear sentence telling me when it would be appropriate to declare a function as a friend of some class.

Multiple Choice

Here is the start of a class declaration:

class foo

{

public:

void x(foo f);

void y(const foo f);

void z(foo f) const;

...

Which of the three member functions can alter the PRIVATE member variables of the foo object that activates the function?

A. Only x can alter the private member variables of the object that activates the function.

B. Only y can alter the private member variables of the object that activates the function.

C. Only z can alter the private member variables of the object that activates the function.

D. Two of the functions can alter the private member variables of the object that activates the function.

E. All of the functions can alter the private member variables of the object that activates the function.

Is it possible for a member function of a class to activate another member function of the same class?

A. No.

B. Yes, but only public member functions.

C. Yes, but only private member functions.

D. Yes, both public and private member functions can be activated within another member function.

Can two classes contain member functions with the same name?

A. No.

B. Yes, but only if the two classes have the same name.

C. Yes, but only if the main program does not declare both kinds

D. Yes, this is always allowed.

What is the common pattern of class definitions that is used in Chapter 2?

A. Member functions and member variables are both private.

B. Member functions are private, and member variables are public.

C. Member functions are public, and member variables are private.

D. Member functions and member variables are both public.

Consider this class definition:

class quiz

{

public:

quiz( );

int f( );

int g( ) const;

private:

double score;

};

Which functions can carry out an assignment score=1.0; to the private member variable score?

A. Both f and g can carry out the assignment.

B. f can carry out the assignment, but not g.

C. g can carry out the assignment, but not f.

D. Neither f nor g can carry out the assignment.

What is the primary purpose of a default constructor?

A. To allow multiple classes to be used in a single program.

B. To copy an actual argument to a function's parameter.

C. To initialize each object as it is declared.

D. To maintain a count of how many objects of a class have been created.

Suppose that the foo class does not have an overloaded assignment operator. What happens when an assignment a=b; is given for two foo objects?

A. The automatic assignment operator is used

B. The copy constructor is used

C. Compiler error

D. Run-time error

When should you use a const reference parameter?

A. Whenever the data type might be many bytes.

B. Whenever the data type might be many bytes, the function changes the parameter within its body, and you do NOT want these changes to alter the actual argument.

C. Whenever the data type might be many bytes, the function changes the parameter within its body, and you DO want these changes to alter the actual argument.

D. Whenever the data type might be many bytes, and the function does not change the parameter within its body.

Here is a small function definition:

void f(int i, int &k)

{

i = 1;

k = 2;

}

Suppose that a main program has two integer variables x and y, which are given the value 0. Then the main program calls f(x,y); What are the values of x and y after the function f finishes?

A. Both x and y are still 0.

B. x is now 1, but y is still 0.

C. x is still 0, but y is now 2.

D. x is now 1, and y is now 2.

Here is a function prototype and some possible function calls:

int day_of_week(int year, int month = 1, int day = 1);

// Possible function calls:

cout << day_of_week( );

cout << day_of_week(1995);

cout << day_of_week(1995, 10);

cout << day_of_week(1995, 10, 4);

How many of the function calls are legal?

A. None of them are legal

B. 1 of them is legal

C. 2 of them are legal

D. 3 of them are legal

E. All of them are legal

Which kind of functions can access private member variables of a class?

A. Friend functions of the class

B. Private member functions of the class

C. Public member functions of the class

D. All of the above can access private member variables

E. None of the above

Short Answers Short Answers Section 2.1 - 2.2 Introduction to Classes 1. Here is part of the throttle declaration: 2. 3. class throttle 4. { 5. public: 6. void shut_off(); 7. double flow(); 8. ... 9. private: 10. int position; 11. }; Write several lines of C++ code to declare a throttle called quiz, activate the member function that shuts off quiz's flow, and then print the current flow from quiz. 12. The public part of the throttle declaration from class notes is shown below. Mark each function member header as follows: Mark C for any constructor; mark X for any function that is forbidden from changing the throttle's data fields. 13. 14. class throttle 15. { 16. public: 17. throttle( ); 18. throttle(int size); 19. void shut_off( ); 20. void shift(int amount); 21. double flow( ) const; 22. bool is_on( ) const; 23. ... 24. What is an automatic default constructor, and what does it do? 25. What is an inline member function, and when might one be used? Give a small example as part of your answer. Short Answers Section 2.3 The Header File and the Implementation File 26. What is a macro guard in a header file, and what is its purpose? Give a small example as part of your answer. 27. Suppose that the throttle class is implemented with a header file (throttle.h) and an implementation file (throttle.cxx), and that it is part of the namespace main_savitch_2A. Write the include directive and the using directive that are needed for a program to use the throttle class. Short Answers Section 2.4 Classes and Parameters 28. When is it appropriate to use a const reference parameter? Give a small example as part of your answer. 29. Suppose a function has a parameter named x, and the body of the function changes the value of x. When should x be a value parameter? When should it be a reference parameter? With this function, could x ever be a const reference parameter? 30. Write one clear sentence telling me when it would be appropriate to use a const reference parameter. 31. Write one clear sentence telling me when it would be appropriate to use a reference parameter. 32. Here is a small class definition: 33. 34. class small 35. { 36. public: 37. small( ); 38. void k() const; 39. void h(int i); 40. friend f(Small z); 41. private: 42. int size; 43. }; Suppose that x and y are both small objects. Write the word "legal" or "illegal" in each location of this table to indicate whether the indicated statement is legal or illegal in these locations: Statement In a main program In the const member function k In the friend function f x = y; . . . x.size = y.size; . . . x.size = 3; . . . x.h(42); . . . 44. Suppose that you define a new class called foo. For two foo objects x and y, you would like the expression x+y to be a new foo object. What is the prototype of the function that you must write to enable expressions such as x+y? 45. I have written a class with an operator + which is not a member function, but the operator + implementation does access the private member variables of objects in the class. What did I need to do to gain this access? 46. Write one clear sentence telling me when it would be appropriate to declare a function as a friend of some class. Multiple Choice 1. Here is the start of a class declaration: 2. 3. class foo 4. { 5. public: 6. void x(foo f); 7. void y(const foo f); 8. void z(foo f) const; 9. ... Which of the three member functions can alter the PRIVATE member variables of the foo object that activates the function? o A. Only x can alter the private member variables of the object that activates the function. o B. Only y can alter the private member variables of the object that activates the function. o C. Only z can alter the private member variables of the object that activates the function. o D. Two of the functions can alter the private member variables of the object that activates the function. o E. All of the functions can alter the private member variables of the object that activates the function. 10. Is it possible for a member function of a class to activate another member function of the same class? o A. No. o B. Yes, but only public member functions. o C. Yes, but only private member functions. o D. Yes, both public and private member functions can be activated within another member function. 11. Can two classes contain member functions with the same name? o A. No. o B. Yes, but only if the two classes have the same name. o C. Yes, but only if the main program does not declare both kinds o D. Yes, this is always allowed. 12. What is the common pattern of class definitions that is used in Chapter 2? o A. Member functions and member variables are both private. o B. Member functions are private, and member variables are public. o C. Member functions are public, and member variables are private. o D. Member functions and member variables are both public. 13. Consider this class definition: 14. 15. class quiz 16. { 17. public: 18. quiz( ); 19. int f( ); 20. int g( ) const; 21. private: 22. double score; 23. }; Which functions can carry out an assignment score=1.0; to the private member variable score? o A. Both f and g can carry out the assignment. o B. f can carry out the assignment, but not g. o C. g can carry out the assignment, but not f. o D. Neither f nor g can carry out the assignment. 24. What is the primary purpose of a default constructor? o A. To allow multiple classes to be used in a single program. o B. To copy an actual argument to a function's parameter. o C. To initialize each object as it is declared. o D. To maintain a count of how many objects of a class have been created. 25. Suppose that the foo class does not have an overloaded assignment operator. What happens when an assignment a=b; is given for two foo objects? o A. The automatic assignment operator is used o B. The copy constructor is used o C. Compiler error o D. Run-time error 26. When should you use a const reference parameter? o A. Whenever the data type might be many bytes. o B. Whenever the data type might be many bytes, the function changes the parameter within its body, and you do NOT want these changes to alter the actual argument. o C. Whenever the data type might be many bytes, the function changes the parameter within its body, and you DO want these changes to alter the actual argument. o D. Whenever the data type might be many bytes, and the function does not change the parameter within its body. 27. Here is a small function definition: 28. 29. void f(int i, int &k) 30. { 31. i = 1; 32. k = 2; 33. } Suppose that a main program has two integer variables x and y, which are given the value 0. Then the main program calls f(x,y); What are the values of x and y after the function f finishes? o A. Both x and y are still 0. o B. x is now 1, but y is still 0. o C. x is still 0, but y is now 2. o D. x is now 1, and y is now 2. 34. Here is a function prototype and some possible function calls: 35. 36. int day_of_week(int year, int month = 1, int day = 1); 37. // Possible function calls: 38. cout << day_of_week( ); 39. cout << day_of_week(1995); 40. cout << day_of_week(1995, 10); 41. cout << day_of_week(1995, 10, 4); How many of the function calls are legal? o A. None of them are legal o B. 1 of them is legal o C. 2 of them are legal o D. 3 of them are legal o E. All of them are legal 42. Which kind of functions can access private member variables of a class? o A. Friend functions of the class o B. Private member functions of the class o C. Public member functions of the class o D. All of the above can access private member variables o E. None of the above

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Flash XML Applications Use AS2 And AS3 To Create Photo Galleries Menus And Databases

Authors: Joachim Schnier

1st Edition

0240809173, 978-0240809175

More Books

Students also viewed these Databases questions

Question

Discuss the role of change as part of organizational planning.

Answered: 1 week ago

Question

What do Dimensions represent in OLAP Cubes?

Answered: 1 week ago