Write a base class TaxFiler and its derived classes SingleFiler, MarriedFiler, H
ID: 3548012 • Letter: W
Question
Write a base class TaxFiler and its derived classes SingleFiler, MarriedFiler, HeadOfHousehold & MarriedSeparateFiler. Define name as string and taxableIncome as double in the base class. Implement getName() method and make computeTax() an abstract method in TaxFiler class. You need to implement computeTax() in each derived class. Utilize tax tables listed in http://en.wikipedia.org/wiki/Rate_schedule_%28federal_income_tax%29 as guidance.
You can use the following main() to test them out. Feel free to add additional lines for full testing.
TaxFiler *filers[] = { new SingleFiler("Jack", 100050),
new MarriedFiler("Minnie", 150000.50),
new HeadOfHousehold("Boxer", 75000),
new MarriedSeparateFiler("Mr.X", 300000.5),
new SingleFiler("Nita", 9000.50) };
for( int i=0 ; i<5 ; i++ )
cout << filer[i]->getName() << ": " << filer[i]->computeTax() << endl;
PARTIAL CODE :
class TaxFiler {
private:
string name;
double taxableIncome;
public:
...
virtual double computeTax() = 0;
double getTaxableIncome() {
return taxableIncome;
}
}
class SingleFiler : public TaxFiler {
...
public:
double computeTax() {
double income = getTaxableIncome();
if (income < ...) //as per tax table
return ...
}
}
Explanation / Answer
//private: // redundant, class opens as 'private'.
std::string m_name; // m_ prefix to denote a member variable.
double m_taxableIncome;
public:
TaxFiler(const std::string& name, double income)
: m_name(name) // initializer list
, m_taxableIncome(income)
{
}
const std::string& getName() const { return m_name; }
virtual double computeTax() const = 0; // must be implemented by derived class.
double taxableIncome() const { return m_taxableIncome; }
};
class SingleFiler : public TaxFiler
{
public:
SingleFiler(const std::string& name, double income)
: TaxFiler(name, income)
{
}
double computeTax() const override
{
// taxableIncome() could become non-trivial, so do it once
// and store the value.
auto taxable = taxableIncome();
if (taxable <= 0.0)
return 0.0;
if (taxable <= 8925)
return (taxable*.10);
if (taxable <= 36250)
return (892.50 + (taxable* .15));
if (taxable <= 87850)
return (4991.25 + (taxable* .25));
if (taxable <=183250)
return (17891.25 + (taxable * .28));
if (taxable <= 398350)
return (44603.25 + (taxable * .22));
if (taxable <= 40000)
return (115586.25 + (taxable * .35));
return (116163.75 + (taxable * .396));
}
};
int main()
{
std::vector<std::unique_ptr<TaxFiler>> filers;
filers.emplace_back(new SingleFiler("Jack", 100050));
filers.emplace_back(new SingleFiler("Joe", 42000));
for (const auto& filer : filers) {
std::cout << filer->getName() << ": " << filer->computeTax() << " ";
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.