SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Walter Savitch ,julia Lobur
Chapter:
Defining Classes
Exercise:
Self-test Exercises
Question:22 | ISBN:9780321531346 | Edition: 7

Question

Suppose your program contains the following class definition (along with

definitions of the member functions):


class YourClass

{

public:

YourClass(int new_info, char more_new_info);

YourClass( );

void do_stuff( );

private:

int information;

char more_information;

};

Which of the following are legal?

YourClass an_object(42, 'A');

YourClass another_object;

YourClass yet_another_object( );

an_object = YourClass(99, 'B');

an_object = YourClass( );

an_object = YourClass;

TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

Let's analyze each statement to determine if it is legal:

  1. YourClass an_object(42, 'A');

    • Legal: This statement creates an object an_object of type YourClass and calls the constructor YourClass(int new_info, char more_new_info) with arguments 42 and 'A' to initialize its member variables information and more_information.
  2. YourClass another_object;

    • Legal: This statement creates an object another_object of type YourClass and calls the default constructor YourClass() to initialize its member variables information and more_information.
  3. YourClass yet_another_object( );

    • Legal: This statement also creates an object yet_another_object of type YourClass and calls the default constructor YourClass() to initialize its member variables information and more_information. Note that the parentheses () are optional here.
  4. an_object = YourClass(99, 'B');

    • Legal: This statement creates a temporary object of type YourClass using the constructor YourClass(int new_info, char more_new_info) with arguments 99 and 'B', and then uses the assignment operator = to assign the temporary object to the an_object.
  5. an_object = YourClass( );

    • Legal: This statement creates a temporary object of type YourClass using the default constructor YourClass() and then uses the assignment operator = to assign the temporary object to the an_object.
  6. an_object = YourClass;

    • Not Legal: This statement attempts to assign the class type YourClass to the object an_object, which is not allowed. It should provide arguments to call a constructor or use the default constructor if no arguments are needed.

In summary, the legal statements are:

  • YourClass an_object(42, 'A');
  • YourClass another_object;
  • YourClass yet_another_object();
  • an_object = YourClass(99, 'B');
  • an_object = YourClass();

The last statement (an_object = YourClass;) is not legal and should be avoided.

0 0

Discussions

Post the discussion to improve the above solution.