Design a Tips class that calculates the gratuity on a restaurant meal. Its only
ID: 3906768 • Letter: D
Question
Design a Tips class that calculates the gratuity on a restaurant meal. Its only class member variable, taxRate, should be set by a one-parameter constructor to whatever rate is passed to it when a Tips object is created. If no argument is passed, a default tax rate of .065 should be used. The class should have just one public function, computeTip. This function needs to accept two arguments, the total bill amount and the tip rate. It should use this information to compute the meal cost before any tax was added. It should then apply the tip rate to just the meal cost portion of the bill to compute and return the tip amount. Demonstrate the class by creating a program that creates a single Tips object, then loops multiple times to allow the program user to retrieve the correct tip amount using various bill totals and desired tip rates. The program asks the user if they have another meal to compute. Program terminates if the user does not enter y/Y to the previous question.
Explanation / Answer
Program code :
#include <iostream>
#include <iomanip>
using namespace std;
//Create the class
class Tips
{
public:
double tar;
Tips()
{if (tar < 0)
tar = 6.5;
}
double compute tip(double, double, double);
};
double Tips::compute tip(double t, double tir, double tar)
{
double total = t;
double tipRate = tir;
double taxRate = tar;
double meal;
double tip;
while (t != 999)
{
cout << "Enter the total cost of the meal: ";
cin >> t;
if (t < 0)
{cout << "Invalid entry. Please enter a number greater than 0: ";
cin >> t;
}
cout << "Enter the amount of tax: ";
cin >> tar;
Tips::Tips();
meal = (t / (tar + 100)) * 100;
cout << "The total amount of the meal before tax is: ";
cout << setprecision(4) << meal;
cout << endl;
cout << "Enter the tip percentage you would like to leave: ";
cin >> tir;
if (tir < 0)
{cout << "Invalid entry. Please enter a number greater than 0: ";
cin >> tir;
}
tip = meal * (tir / 100);
cout << "The tip rate you would like to leave is: " << tir / 100 << endl;
cout << "The amount of the tip you should leave is: " << setprecision(3) << tip << endl;
cout << endl;
}
return tip;
}
int main()
{
Tips bill;
double billRate, billMeal, billTir, billTip;
cout << "This program computes the gratuity at a restaurant. Enter 999 to quit.";
cout << endl;
cout << endl;
bill.computeTip(billTir, billTip, billRate);
system("pause");
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.