199k views
4 votes
Onvert the following constructor to use a constructor initializer list.

Restaurant:: Restaurant()
{
Pizza=10;
Burger =25;
}

a. Restaurant::Restaurant():Pizza(10), Burger(25){}
b. Restaurant:: Restaurant() :: Pizza(10), Burger(25) {}
c. Restaurant() : Pizza(10), Burger(25) {}
d. Restaurant:: Restaurant().Pizza(10), Burger(25) {}

User Yakunins
by
8.0k points

1 Answer

3 votes

Final answer:

The correct way to convert the constructor is by using a constructor initializer list.

Option a)), Restaurant::Restaurant():Pizza(10), Burger(25){ }, is the correct answer as it uses this list to initialize member variables before the constructor's body.

Step-by-step explanation:

Initializer List is used in initializing the data members of a class. The list of members to be initialized is indicated with constructor as a comma-separated list followed by a colon.

To convert the given constructor from a traditional method to one that uses an initializer list to set the values for Pizza and Burger variables, the correct format is to place a colon after the constructor's name, followed by the variable names with their respective values enclosed in parentheses. This is compact and efficient, and it initializes the member variables before the constructor's body is executed.

The right answer is: Restaurant::Restaurant() : Pizza(10), Burger(25) {}

Note that this line initializes the Pizza variable with a value of 10 and the Burger variable with a value of 25 directly using the constructor initializer list which is considered good practice in C++.

User Ivan Dimov
by
7.8k points