jjzjj

c++ - 帮助处理类和派生类

coder 2024-02-23 原文

我正在处理一个使用基类“bankAccount”和两个派生类“checkingAccount”和“savingsAccount”的作业。我目前对我得到的输出感到困惑。所有期末余额都以负数结束。谁能看看我的代码,看看他们是否发现了为什么会这样?我假设我在派生类“checkingAccount”的处理函数中做错了什么。 “savingsAccount”流程功能将是相似的我只是还没有做到,因为第一个不工作。谢谢!

标题:

#ifndef HEADER_H_INCLUDED
#define HEADER_H_INCLUDED

#include <iostream>
#include <fstream>


using namespace std;

class bankAccount
{
    public:
    bankAccount();
    void setAccountInfo(int accountNumTemp, double balanceTemp);
    void prePrint(char accountType);
    void process(char accountType, char transactionTypeTemp, int amountTemp, int j);
    void postPrint();

    private:
    int accountNumber;
    double balance;
};

class checkingAccount: public bankAccount
{
    public:
    void prePrint(int accountNumber, char accountType, double checkingBalance);
    checkingAccount();
    double checkingAccount:: process(char transactionTypeTemp, int amountTemp, int j, double checkingBalance);
    /*applyTansaction();
    applyInterest();*/

    private:
    float interestRate;
    int minimumBalance;
    float serviceCharge;

};

class savingsAccount: public bankAccount
{
    public:
    void prePrint(int savingsAccountNumber, char accountType, double savingsBalance);
    savingsAccount();
   /* applyTansaction();
    applyInterest();*/

    private:
    float interestRate;
};


#endif // HEADER_H_INCLUDED

类实现:

#include "header.h"

bankAccount:: bankAccount()
{
    accountNumber = 0;
    balance = 0;
}

void bankAccount:: setAccountInfo(int accountNumTemp, double balanceTemp)
{
    accountNumber = accountNumTemp;
    balance = balanceTemp;
}

void bankAccount:: prePrint(char accountType)
{
    if(accountType == 'C')
    {
        int checkingAccountNumber = accountNumber;
        double checkingBalance = balance;
        checkingAccount ca;
        ca.prePrint(checkingAccountNumber, accountType, checkingBalance);
    }
    else if (accountType == 'S')
    {
        int savingsAccountNumber = accountNumber;
        double savingsBalance = balance;
        savingsAccount sa;
        sa.prePrint(savingsAccountNumber, accountType, savingsBalance);
    }


}

void bankAccount:: process(char accountType, char transactionTypeTemp, int amountTemp, int j)
{
        double checkingBalance;
        checkingAccount ca;
        //savingsAccount sa;

        if (accountType == 'C')
        {
            checkingBalance = balance;
            balance = ca.process(transactionTypeTemp, amountTemp, j, checkingBalance);
        }
        /*else if (accountType == 'S')
        {
            savingsBalance = balance;
            sa.process(transactionTypeTemp, amountTemp, j, savingsBalance)
        }*/

}

void bankAccount:: postPrint()
{
   cout << "Balance after processing: " << balance << endl;
}

checkingAccount:: checkingAccount()
{
    interestRate = .02;
    minimumBalance = 500;
    serviceCharge = 20;
}

void checkingAccount:: prePrint(int checkingAccountNumber, char accountType, double checkingBalance)
{
    cout << "Account Number:" << checkingAccountNumber << " account type:" << accountType << " Starting Balance:" << checkingBalance << endl;
}

double checkingAccount:: process(char transactionTypeTemp, int amountTemp, int j, double checkingBalance)
{
  if (transactionTypeTemp == 'D')
        {
            checkingBalance = checkingBalance + amountTemp;
            checkingBalance = (checkingBalance * interestRate);
        }
  else if (transactionTypeTemp == 'W')
        {
            if ((checkingBalance = checkingBalance - amountTemp) < 0)
            {
            cout << "error: transaction number" << j + 1 << " never occured due to insufficent funds." << endl;
            }
            else
            {
                checkingBalance = checkingBalance - amountTemp;
                if(checkingBalance < minimumBalance) //if last transaction brought the balance below minimum balance
                {
                    checkingBalance = (checkingBalance - serviceCharge); //apply service charge
                    checkingBalance = (checkingBalance * interestRate);  //apply interest

                }
                else // if last transaction did not bring the balance below minimum balance
                {
                    checkingBalance = (checkingBalance * interestRate); //apply interest without service charge
                }
            }
        }

        return checkingBalance;
}

savingsAccount:: savingsAccount()
{
    interestRate = .04;
}

void savingsAccount:: prePrint(int savingsAccountNumber, char accountType, double savingsBalance)
{
    cout << "Account Number:" << savingsAccountNumber << " account type:" << accountType << " Starting Balance:" << savingsBalance << endl;
}

主要内容:

#include "header.h"

int main()
{
    ifstream inFile;
    int numberOfAccounts, accountNumTemp, transactionNum, amountTemp;
    double balanceTemp;
    char discard, accountType, transactionTypeTemp;
    bankAccount ba;

    cout << "Processing account data..." << endl;

    inFile.open("Bank.txt");

    if (!inFile)
    {
        for  (int a = 0; a < 20; a++)
            cout  << endl;
        cout << "Cannot open the input file."
             << endl;
            return 1;
    }

    inFile >> numberOfAccounts;
    inFile.get(discard);

    for (int i = 0; i < numberOfAccounts; i++)
    {
            inFile >> accountNumTemp >> accountType >> balanceTemp >> transactionNum;
            inFile.get(discard);
            ba.setAccountInfo(accountNumTemp, balanceTemp);
            ba.prePrint(accountType);

            for (int j = 0; j < transactionNum; j++)
            {
                inFile >> transactionTypeTemp >> amountTemp;
                inFile.get(discard);
                ba.process(accountType, transactionTypeTemp, amountTemp, j);
            }

            ba.postPrint();

    }


    inFile.close();

    return 0;
}

最佳答案

我实际上在一家银行工作,所以我不能离开这个。 :-)

增加你的问题:

if (transactionTypeTemp == 'D')
{
     checkingBalance = checkingBalance + amountTemp;
     checkingBalance = (checkingBalance * interestRate);
}

这实际上给账户留下了利息!

此外,真正的银行不会在您存款时计算利息,而是在固定日期(例如每月一次或每年一次)计算利息。您获得(或支付)的利息还取决于账户达到一定余额的天数。

if ((checkingBalance = checkingBalance - amountTemp) < 0)
{
    cout << "error: transaction number" << j + 1 << " never occured due to insufficent funds." << endl;
}

尽管将文本写入 cout,但交易确实已经发生,因为 = 为 Balance 分配了一个新值!也许您应该只比较余额和金额?

然后您在 else 部分再次重复无效利息计算。

关于c++ - 帮助处理类和派生类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5768270/

有关c++ - 帮助处理类和派生类的更多相关文章

  1. ruby - 如何指定 Rack 处理程序 - 2

    Rackup通过Rack的默认处理程序成功运行任何Rack应用程序。例如:classRackAppdefcall(environment)['200',{'Content-Type'=>'text/html'},["Helloworld"]]endendrunRackApp.new但是当最后一行更改为使用Rack的内置CGI处理程序时,rackup给出“NoMethodErrorat/undefinedmethod`call'fornil:NilClass”:Rack::Handler::CGI.runRackApp.newRack的其他内置处理程序也提出了同样的反对意见。例如Rack

  2. ruby-on-rails - 如何优雅地重启 thin + nginx? - 2

    我的瘦服务器配置了nginx,我的ROR应用程序正在它们上运行。在我发布代码更新时运行thinrestart会给我的应用程序带来一些停机时间。我试图弄清楚如何优雅地重启正在运行的Thin实例,但找不到好的解决方案。有没有人能做到这一点? 最佳答案 #Restartjustthethinserverdescribedbythatconfigsudothin-C/etc/thin/mysite.ymlrestartNginx将继续运行并代理请求。如果您将Nginx设置为使用多个上游服务器,例如server{listen80;server

  3. ruby - 有人可以帮助解释类创建的 post_initialize 回调吗 (Sandi Metz) - 2

    我正在阅读SandiMetz的POODR,并且遇到了一个我不太了解的编码原则。这是代码:classBicycleattr_reader:size,:chain,:tire_sizedefinitialize(args={})@size=args[:size]||1@chain=args[:chain]||2@tire_size=args[:tire_size]||3post_initialize(args)endendclassMountainBike此代码将为其各自的属性输出1,2,3,4,5。我不明白的是查找方法。当一辆山地自行车被实例化时,因为它没有自己的initialize方法

  4. Ruby——嵌套类和子类是一回事吗? - 2

    下面例子中的Nested和Child有什么区别?是否只是同一事物的不同语法?classParentclassNested...endendclassChild 最佳答案 不,它们是不同的。嵌套:Computer之外的“Processor”类只能作为Computer::Processor访问。嵌套为内部类(namespace)提供上下文。对于ruby​​解释器Computer和Computer::Processor只是两个独立的类。classComputerclassProcessor#Tocreateanobjectforthisc

  5. ruby-on-rails - Cucumber 是否只是 rspec 的包装器以帮助将测试组织成功能? - 2

    只是想确保我理解了事情。据我目前收集到的信息,Cucumber只是一个“包装器”,或者是一种通过将事物分类为功能和步骤来组织测试的好方法,其中实际的单元测试处于步骤阶段。它允许您根据事物的工作方式组织您的测试。对吗? 最佳答案 有点。它是一种组织测试的方式,但不仅如此。它的行为就像最初的Rails集成测试一样,但更易于使用。这里最大的好处是您的session在整个Scenario中保持透明。关于Cucumber的另一件事是您(应该)从使用您的代码的浏览器或客户端的角度进行测试。如果您愿意,您可以使用步骤来构建对象和设置状态,但通常您

  6. ruby - 使用 `+=` 和 `send` 方法 - 2

    如何将send与+=一起使用?a=20;a.send"+=",10undefinedmethod`+='for20:Fixnuma=20;a+=10=>30 最佳答案 恐怕你不能。+=不是方法,而是语法糖。参见http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_expressions.html它说Incommonwithmanyotherlanguages,Rubyhasasyntacticshortcut:a=a+2maybewrittenasa+=2.你能做的最好的事情是:

  7. ruby - 如何计算 Liquid 中的变量 +1 - 2

    我对如何计算通过{%assignvar=0%}赋值的变量加一完全感到困惑。这应该是最简单的任务。到目前为止,这是我尝试过的:{%assignamount=0%}{%forvariantinproduct.variants%}{%assignamount=amount+1%}{%endfor%}Amount:{{amount}}结果总是0。也许我忽略了一些明显的东西。也许有更好的方法。我想要存档的只是获取运行的迭代次数。 最佳答案 因为{{incrementamount}}将输出您的变量值并且不会影响{%assign%}定义的变量,我

  8. ruby-on-rails - 需要帮助最大化多个相似对象中的 3 个因素并适当排序 - 2

    我需要用任何语言编写一个算法,根据3个因素对数组进行排序。我以度假村为例(如Hipmunk)。假设我想去度假。我想要最便宜的地方、最好的评论和最多的景点。但是,显然我找不到在所有3个中都排名第一的方法。Example(assumingthereare20importantattractions):ResortA:$150/night...98/100infavorablereviews...18of20attractionsResortB:$99/night...85/100infavorablereviews...12of20attractionsResortC:$120/night

  9. Ruby-vips 图像处理库。有什么好的使用示例吗? - 2

    我对图像处理完全陌生。我对JPEG内部是什么以及它是如何工作一无所知。我想知道,是否可以在某处找到执行以下简单操作的ruby​​代码:打开jpeg文件。遍历每个像素并将其颜色设置为fx绿色。将结果写入另一个文件。我对如何使用ruby​​-vips库实现这一点特别感兴趣https://github.com/ender672/ruby-vips我的目标-学习如何使用ruby​​-vips执行基本的图像处理操作(Gamma校正、亮度、色调……)任何指向比“helloworld”更复杂的工作示例的链接——比如ruby​​-vips的github页面上的链接,我们将不胜感激!如果有ruby​​-

  10. ruby - Faye WebSocket,关闭处理程序被触发后重新连接到套接字 - 2

    我有一个super简单的脚本,它几乎包含了FayeWebSocketGitHub页面上用于处理关闭连接的内容:ws=Faye::WebSocket::Client.new(url,nil,:headers=>headers)ws.on:opendo|event|p[:open]#sendpingcommand#sendtestcommand#ws.send({command:'test'}.to_json)endws.on:messagedo|event|#hereistheentrypointfordatacomingfromtheserver.pJSON.parse(event.d

随机推荐