168k views
4 votes
In the class definition, initialize the data members, string name and integer rating, with the default values "Unknown" and 1, respectively.

Ex: If the input is Dan 4, then the output is:

Name: Unknown's Bistro's Buffet, Rating: 1 Name: Dan's Bistro's Buffet, Rating: 4

Note: The class's print function is called first after the default constructor, then again after the inputs are passed to the setters.

GIven code:

#include
#include
using namespace std;

User Polmonroig
by
7.7k points

1 Answer

6 votes

Final answer:

The question asks about defining a class in C++ with default values for its data members. The solution is to use a constructor to initialize string 'name' to 'Unknown' and integer 'rating' to 1. Two methods, setName and setRating, are used to update the values, and a print function displays them.

Step-by-step explanation:

The student is asking how to initialize data members in a class with default values in C++ programming. Specifically, they want to set a string called name to "Unknown" and an integer called rating to 1 by default.

To achieve this, you will need to use a constructor in your class definition to set the default values upon the object's creation. Here is how you could write your class:

class Restaurant {
private:
string name;
int rating;
public:
Restaurant() : name("Unknown"), rating(1) {}
void setName(string newName) { name = newName; }
void setRating(int newRating) { rating = newRating; }
void print() const {
cout << "Name: " << name << "'s Bistro's Buffet, Rating: " << rating << endl;
}
};

After defining the class, you create an object and call the print method twice, as per the instructions: first to print the default values, and then again after using the setters to update the object with the provided input.

User WiredIn
by
7.6k points