patterncppMinor
Design a Tips class that calculates the gratuity on a restaurant meal
Viewed 0 times
themealtipsrestaurantgratuitydesignthatclasscalculates
Problem
Design a Tips class that calculates the gratuity on a restaurant meal.
Its only class member variable,
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 what the cost of
the meal was before the 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, and then allows the program user to retrieve the correct tip
amount using various bill totals and desired tip rates.
What can I do better?
Its only class member variable,
taxRate, should be set by aone-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 what the cost of
the meal was before the 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, and then allows the program user to retrieve the correct tip
amount using various bill totals and desired tip rates.
What can I do better?
#include
#include
using namespace std;
class Tips
{
private :
double taxRate;
public:
Tips()
{
taxRate = .65;
cout << "Tax rate is "<<taxRate<<endl;
}
Tips(double tax)
{
taxRate = tax;
cout << "Tax rate is"<<taxRate<<endl;
}
double computeTip(double billAmount,double tipRate)
{
double mealCost = billAmount - billAmount*(taxRate);
cout << "Meal cost is with no tax "<<mealCost<<endl;
return mealCost+tipRate;
}
};
int main()
{
Tips tip ;
cout << "Meal cost + tip rate is "<<tip.computeTip(100,20)<<endl;
return 0;
}Solution
Inconsistant layout:
Be consistent in your code. I prefer the version with the space.
Using namespace std
Arrrrr. Don't do that. see: Why is “using namespace std;” considered bad practice?.
Definately never do it in a header file. You are pulliting the namesapce for anybody that includes your code (which can change behavior) and break other peoples code. Don't do it in source files because of the reasons you read in the linked article.
Default argument
When you see code like this you can usually replace it with a single constructor that has a parameter with a default argument (as they are basically doing the same thing).
Be consistent with spacing (white space is your friend)
Prefer '\n' over std::endl
std::end forces a flush of the stream. This is never what you actually want. The automated flushing of the stream will work much better than manual flushing.
Prefer to use the initializer list.
Arithmetic issue
If the billAmount is
#include // No space
#include // SpaceBe consistent in your code. I prefer the version with the space.
Using namespace std
Arrrrr. Don't do that. see: Why is “using namespace std;” considered bad practice?.
Definately never do it in a header file. You are pulliting the namesapce for anybody that includes your code (which can change behavior) and break other peoples code. Don't do it in source files because of the reasons you read in the linked article.
Default argument
Tips()
{
taxRate = .65;
cout << "Tax rate is "<<taxRate<<endl;
}
Tips(double tax)
{
taxRate = tax;
cout << "Tax rate is"<<taxRate<<endl;
}When you see code like this you can usually replace it with a single constructor that has a parameter with a default argument (as they are basically doing the same thing).
Tips(double tax = 0.065) // Corrected you constant.
{
taxRate = tax;
cout << "Tax rate is"<<taxRate<<endl;
}Be consistent with spacing (white space is your friend)
cout << "Tax rate is"<<taxRate<<endl;
/// ^^^^^^ ^^^^^ Looking very tight here.
cout << "Meal cost + tip rate is "<<tip.computeTip(100,20)<<endl;
//// ^^^^^ ^^^^Prefer '\n' over std::endl
std::end forces a flush of the stream. This is never what you actually want. The automated flushing of the stream will work much better than manual flushing.
Prefer to use the initializer list.
Tips(double tax = 0.065) // Corrected you constant.
: taxRate(tax)
{
cout << "Tax rate is"<< taxRate << endl;
}Arithmetic issue
If the billAmount is
mealCost * (1 + taxRate) then this is not the formula to calculate meal cost.double mealCost = billAmount - billAmount*(taxRate);Code Snippets
#include<iostream> // No space
#include <string> // SpaceTips()
{
taxRate = .65;
cout << "Tax rate is "<<taxRate<<endl;
}
Tips(double tax)
{
taxRate = tax;
cout << "Tax rate is"<<taxRate<<endl;
}Tips(double tax = 0.065) // Corrected you constant.
{
taxRate = tax;
cout << "Tax rate is"<<taxRate<<endl;
}cout << "Tax rate is"<<taxRate<<endl;
/// ^^^^^^ ^^^^^ Looking very tight here.
cout << "Meal cost + tip rate is "<<tip.computeTip(100,20)<<endl;
//// ^^^^^ ^^^^Tips(double tax = 0.065) // Corrected you constant.
: taxRate(tax)
{
cout << "Tax rate is"<< taxRate << endl;
}Context
StackExchange Code Review Q#70188, answer score: 6
Revisions (0)
No revisions yet.