【问题标题】:How do I structure classes in the header so that they work in the cpp file?如何在标头中构造类,以便它们在 cpp 文件中工作?
【发布时间】:2022-12-05 05:23:56
【问题描述】:

好的,所以我想这是一个多层问题。我正在尝试通过使用类和对象来简化我编写的这个程序程序并挑战自己。也只是为了练习。我已经在这个特定程序上工作了一个半月多了,我在头文件的结构和实际在 cpp 文件中创建对象方面遇到了问题。我将在下面包含代码。

头文件

`

#include <cmath>
#include <iomanip>
#include <iostream>
#include <string>
#pragma once
using namespace std;


class calculations {
    public:
        double rate, tax, taxable, income, mortgageCred, interest;
        int deduction, mortgage, age = 1, taxCredit;
        bool hasMortgage, blind, senior, single;
        string answer1, answer2, answer3, answer4, answer5;

        calculations(){};
        calculations(double i, int d) {
            income = i;
            deduction = d;
        };
        calculations(double in, string a5, int mor) {
            interest = in;
            answer5 = a5;
            mortgage = mor;
        };
        calculations(string a1, string a2, string a3, bool sin, bool sen, bool bld, int d) {
            answer1 = a1;
            answer2 = a2;
            answer3 = a3;
            single = sin;
            senior = sen;
            blind = bld;
            deduction = d;
        };
        calculations(string a4, int tC) {
            answer4 = a4;
            taxCredit = tC;
        };
        calculations
        calculations(int d, int tC, int mor, int a, double tb, double t, double r, double i,double mC, double in, string a1, string a2, string a3, string a4, string a5, bool h, bool bld, bool sin, bool sen) {
            deduction = d;
            taxable = tb;
            tax = t;
            rate = r;
            income = i;
            hasMortgage = h;
            answer1 = a1;
            answer2 = a2;
            answer3 = a3;
            answer4 = a4;
            answer5 = a5;
            mortgage = mor;
            mortgageCred = mC;
            interest = in;
            age = a;
            single = sin;
            senior = sen;
            blind = bld;
            taxCredit = tC;
        }

            void finalcalc(double i, int d) {
                deduction = d;
                income = i;

                do {
                    cout << "What was your income for the 2022 tax year?: ";
                    cin >> income;

                    taxable = income - deduction;

                    if (taxable < 1) {

                        tax = 0;
                        break;
                    }
                    if (taxable >= 1 && taxable <= 10000) {

                        rate = 0.10;
                        tax = rate * taxable;
                        break;
                    }
                    if (taxable > 10000 && taxable <= 40000) {

                        rate = 0.12;
                        tax = (rate * (taxable - 10000.00)) + 1000.00;
                        break;
                    }
                    if (taxable > 40000 && taxable <= 89000) {

                        rate = 0.22;
                        tax = (rate * (taxable - 40000.00)) + 4600.00;
                        break;
                    }
                    if (taxable > 80000 && taxable <= 170000) {

                        rate = 0.24;
                        tax = (rate * (taxable - 80000.00)) + 12400.00;
                        break;
                    }
                    if (taxable > 170000 && taxable <= 216000) {

                        rate = 0.32;
                        tax = (rate * (taxable - 170000.00)) + 34200.00;
                        break;
                    }
                    if (taxable > 216000 && taxable <= 540000) {

                        rate = 0.35;
                        tax = (rate * (taxable - 216000.00)) + 48920.00;
                        break;
                    }
                    if (taxable > 540000) {

                        rate = 0.37;
                        tax = (rate * (taxable - 540000.00)) + 162320.00;
                        break;
                    }

                } while (age == 3);
            cout << "Taxes due total $" << tax << "." << endl;
        }
        
            void mortgageCalc(double in, string a5, int mor) {
                interest = in;
                answer5 = a5;
                mortgage = mor;
            
                do {
                    cout << "Did you pay a mortgage on your home this year?: ";
                    cin >> answer5;

                    if (answer5 == "yes") {

                        hasMortgage = true;
                        cout << "Great! Youll get a deduction for that." << endl;
                        cout << "What was the principal of the mortgage (ie. 2000)?: ";
                        cin >> mortgage;
                        cout << endl;

                        cout << "Thanks! Now we need the interest rate on the mortgage." << endl;
                        cout << "You can enter it as you would a percentage ie 3.15. : ";
                        cin >> interest;

                        interest = interest * 0.01;
                        mortgageCred = mortgage * interest;
                        deduction = deduction + mortgageCred;
                        age = 1;
                        break;
                    }
                    else if (answer5 == "no") {

                        hasMortgage = false;
                        cout << "Thats ok! lets move on to some other stuff." << endl;
                        age = 1;
                        break;
                    }
                    else {

                        age = 3;


                    }

                } while (age == 3);
            }

            void deductionCalc(string a1, string a2, string a3, bool sin, bool sen, bool bld, int ded) {
                answer1 = a1;
                answer2 = a2;
                answer3 = a3;
                single = sin;
                senior = sen;
                blind = bld;
                deduction = ded;

                do {
                    cout << "Are you filing single?: ";
                    cin >> answer1;

                    if (answer1 == "yes") {

                        single = true;
                        cout << "Are you over 65 years old?: ";
                        cin >> answer2;

                        if (answer2 == "yes") {

                            senior = true;
                            cout << "Are you considered legally blind?: ";
                            cin >> answer3;

                            if (answer3 == "yes") {

                                blind = true;
                                deduction = 16450;
                                age = 1;
                                break;

                            }
                            else if (answer3 == "no") {

                                blind = false;
                                deduction = 14700;
                                age = 1;
                                break;
                            }
                            else {

                                age = 3;
                                break;
                            }
                        }
                        else if (answer2 == "no") {

                            senior = false;
                            cout << "Are you considered legally blind?: ";
                            cin >> answer3;

                            if (answer3 == "yes") {

                                blind = true;
                                deduction = 14700;
                                age = 1;
                                break;
                            }
                            else if (answer3 == "no") {

                                blind = false;
                                deduction = 12950;
                                age = 1;
                                break;
                            }
                            else {

                                age = 3;
                                break;
                            }
                        }
                    }
                    else if (answer1 == "no") {

                        single = false;
                        cout << "Are you over 65 years old?: ";
                        cin >> answer2;

                        if (answer2 == "yes") {

                            senior = true;
                            cout << "Are you considered legally blind?: ";
                            cin >> answer3;

                            if (answer3 == "yes") {

                                blind = true;
                                deduction = 16450;
                                age = 1;
                                break;

                            }
                            else if (answer3 == "no") {

                                blind = false;
                                deduction = 14700;
                                age = 1;
                                break;
                            }
                            else {

                                age = 3;
                                break;
                            }
                        }
                        else if (answer2 == "no") {

                            senior = false;
                            cout << "Are you considered legally blind?: ";
                            cin >> answer3;

                            if (answer3 == "yes") {

                                blind = true;
                                deduction = 14700;
                                age = 1;
                                break;

                            }
                            else if (answer3 == "no") {

                                blind = false;
                                deduction = 12950;
                                age = 1;
                                break;
                            }
                            else {

                                age = 3;
                                break;
                            }
                        }
                    }
                    else {

                        age = 3;
                        break;
                    }
                }
                while (age == 3);
            }

            void charityCalc(string a4, int tC) {
                answer4 = a4;
                taxCredit = tC;
                
                do {
                    cout << "Did you make any contributions to a charitable organization in 2022?: " << endl;
                    cin >> answer4;

                    if (answer4 == "yes") {
                        cout << "Great! That will help your deduction" << endl;
                        cout << "Please enter how much money you gave to charity in 2022: ";
                        cin >> taxCredit;
                        deduction = deduction + taxCredit;
                    }
                    else if (answer4 == "no") {
                        cout << "OK! Next question!" << endl;
                    }
                    else {
                        age = 3;
                    }
                } while (age == 3);
            }

            int getStats() {
                
                cout << "Marital Status: " << answer1 << endl;
                cout << "Over 65: " << answer2 << endl;
                cout << "Legally Blind: " << answer3 << endl;
                
                if (answer4 == "yes") {

                    cout << "Charitable Donation: " << answer4 << endl;
                    cout << "Charitable Donation Amount: $" << taxCredit << endl;
                    
                }
                else {

                    cout << "Charitable Donation: " << answer4 << endl;

                }

            }


};


`

文件

`

#include <iostream>
#include <iomanip>
#include <string>
#include <cmath> 
#include "Taxes.h"
#include "C:\Users\samfi\Desktop\Taxes.h"
using namespace std;



int main() {

            

    double income, rate, tax, taxable, interest, mortgageCred;
    string more, answer1, answer2, answer3, answer4, answer5;
        bool senior, single, blind, hasMortgage;
        int deduction = 0, times, taxCredit = 0, mortgage = 0, age = 1;

        cout << "Welcome to the Simple Tax Calculator. Let's get started!" << endl;
        cout << endl;


        for (times = 0; times < 5; times++) {


                // standard deduction calculator
                calculations::deductionCalc(answer1, answer2, answer3, single, senior, blind, deduction);

                
                //charitible donation
                calculations::charityCalc(answer4, taxCredit);


                // nortgage calculation and deduction.
                calculations::mortgageCalc(interest, answer5, mortgage);


                // details

                cout << "Lets go over your details just to make sure we got them right." << endl;
                calculations::getStats();

                // tax calculator
                calculations::finalcalc(income, deduction);


            //restart??
                {cout << "Would you like to calculate another tax estimate?: ";
            cin >> more;

            if (more == "yes") {
                cout << "Restarting..." << endl;
                cout << endl;
            }
            else if (more == "no") {
                times = 5;
                break;
            }
            else {
                cout << "Exception Thrown: " << more << ", is not a valid answer." << endl;
            }
        }                           

    }
}

`

我尝试了很多不同的建议。我试过在 cpp 中创建对象,我试过创建多个构造函数,我试过将方法更改为非空值。似乎没有任何效果,所以我想我可以再看看它。理想情况下,该程序应该根据收入等级计算税收减免和欠税,然后您的减免明显增加某些参数。我不太关心它是否符合美国国税局的规定,我只是希望它能正常工作。我正在尝试学习多文件程序是如何工作的,这是一个很好的起点。这里的主要问题是: 类/标题的结构有什么问题? 如何使这两个文件协同工作以运行程序? 我将如何在 CPP 中创建对象以从标头调用方法? 您要添加的任何内容都可以使它更有效率/有效吗?

需要注意的一点:我曾经在学校学习过这个,但发现在其他人和谷歌的帮助下,我自己工作得更好。所以我想,如果我没有从中得到任何有价值的东西,为什么还要浪费我的钱,而且我可以更轻松地免费学习。这么说的目的是让大家知道这不是为了学校之类的,所以这不像是你在帮我做作业之类的。

【问题讨论】:

    标签: c++ class object oop header-files


    【解决方案1】:
    // It looks like your header file is missing the closing }; for the calculations class. The header file should look like this:
    
    #include <cmath>
    #include <iomanip>
    #include <iostream>
    #include <string>
    #pragma once
    using namespace std;
    
    
    class calculations {
        public:
            double rate, tax, taxable, income, mortgageCred, interest;
            int deduction, mortgage, age = 1, taxCredit;
            bool hasMortgage, blind, senior, single;
            string answer1, answer2, answer3, answer4, answer5;
    
            calculations(){};
            calculations(double i, int d) {
                income = i;
                deduction = d;
            };
            calculations(double in, string a5, int mor) {
                interest = in;
                answer5 = a5;
                mortgage = mor;
            };
            calculations(string a1, string a2, string a3, bool sin, bool sen, bool bld, int d) {
                answer1 = a1;
                answer2 = a2;
                answer3 = a3;
                single = sin;
                senior = sen;
                blind = bld;
                deduction = d;
            };
            calculations(string a4, int tC) {
                answer4 = a4;
                taxCredit = tC;
            };
            calculations(int d, int tC, int mor, int a, double tb, double t, double r, double i,double mC, double in, string a1, string a2, string a3, string a4, string a5, bool h, bool bld, bool sin, bool sen) {
                deduction = d;
                taxable = tb;
                tax = t;
                rate = r;
                income = i;
                hasMortgage = h;
                answer1 = a1;
                answer2 = a2;
                answer3 = a3;
                answer4 = a4;
                answer5 = a5;
                mortgage = mor;
                mortgageCred = mC;
                interest = in;
                age = a;
                single = sin;
                senior = sen;
                blind = bld;
                taxCredit = tC;
            }
    
                void finalcalc(double i, int d) {
                    deduction = d;
                    income = i;
    
                    do {
                        cout << "What was your income for the 2022 tax year?: ";
                        cin >> income;
    
                        taxable = income - deduction;
    
                        if (taxable < 1) {
    
                            tax = 0;
                            break;
                        }
                        if (taxable >= 1 && taxable <= 10000) {
    
                            rate = 0.10;
                            tax = rate * taxable;
                            break;
                        }
                        if (taxable > 10000 && taxable <= 40000) {
    
                            rate = 0.12;
                            tax = (rate * (taxable - 10000.00)) + 1000.00;
                            break;
                        }
                        if (taxable > 40000 && taxable <= 89000) {
    
                            rate = 0.22;
                            tax = (rate * (taxable - 40000.00)) + 4600.00;
                            break;
                        }
                        if (tax
    

    【讨论】:

      猜你喜欢
      • 2018-07-12
      • 1970-01-01
      • 2012-09-10
      • 2012-02-10
      • 1970-01-01
      • 1970-01-01
      • 2013-05-10
      • 2010-12-10
      相关资源
      最近更新 更多