C++ This week I want you to write a program that contains a custom class that mo
ID: 3838242 • Letter: C
Question
C++
This week I want you to write a program that contains a custom class that models a Taco profile for some Taco sharing app. The Taco class will have the following private member variables: 1. name: String 2. location: String 3. price: double 4. numOfRatings: int 5. rating: double You will need to implement public getters and setters (accessors and mutators) for the first three fields, (name, location, and price) In addition you will need to implement the following public member functions. 1. rateTaco(newRating: int): void 2. getRating(): double 3. displayTaco(): void The rateTaco(int) member function should take a rating between 1 and 10 and alter the rating field of that instance by an appropriate amount given the current rating and number of ratings. It should also increment the numOfRatings by one to reflect the new rating. To help with the math you can use this expression to update the rating of a Taco. rating = ((rating * numOfRatings) + newRating)/(numOfRatings + 1); The getRating() member function should just return the value of the rating member variable. {Basic getter/accessor.) I only listed it separately so that you would know you don't need to create a setter/mutator for the rating variable. A Taco's rating is a read-only variable. It gets adjusted as people rate tacos using the rateTaco() function. The displayTaco() member function is just a convenience method to print all of the information about an instance of the Taco class to the screen.Explanation / Answer
#include<iostream>
#include<string>
using namespace std;
class Taco
{
private:
string name;
string location;
double price;
int numOfRatings;
double rating;
public:
Taco()
{
name="";
location="";
price=0.0;
numOfRatings=0;
rating=0.0;
}
string getName()
{
return name;
}
string getLocation()
{
return location;
}
double getPrice()
{
return price;
}
void setName(string newName)
{
name=newName;
}
void setLocation(string newLocation)
{
location=newLocation;
}
void setPrice(double newPrice)
{
price=newPrice;
}
void rateTaco(int newRating)
{
rating=((getRating()*numOfRatings)+newRating)/(numOfRatings+1);
numOfRatings+=1;
}
double getRating()
{
return rating;
}
void displayTaco()
{
cout<<"Printing the memners "<<endl;
cout<<"name is "<<name<<endl;
cout<<"location is "<<location<<endl;
cout<<"price is "<<price<<endl;
cout<<"rating is "<<rating<<endl;
cout<<"numOfRating is"<<numOfRatings<<endl;
}
};
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.