0

我有一个名为 account 的基类。三个类从帐户继承。这些类别是储蓄、支票和信用卡。在我的 main() 中,我试图创建一个切换菜单,以便当用户选择 1 时,它将调用 makeDeposit(),它是帐户的一部分,但通过储蓄来实现。这样当用户选择 3 时,它将调用 makeDeposit(),但通过检查来完成。这是我写的代码。我已声明对象保存 sa;当我调用 makeDeposit 时,我试图将其写为 sa.makeDeposit()。这是代码:

int main ()
{
    saving sa;
    creditCard cca;
    checking ca;

    string n;
    int option;
    int exit = 1;
    cout << endl;
    cout << "Checking Balance:" << " " << "          " << "Savings balance:" << " " << "          " << "Credit Card balance:" << " " << endl;
    cout << endl;
    cout << " (1) Savings Deposit " << endl;
    cout << " (2) Savings withdrawel " << endl;
    cout << " (3) Checking Deposit " << endl;
    cout << " (4) Write A Check " << endl;
    cout << " (5) Credit Card Payment " << endl;
    cout << " (6) Make A Charge " << endl;
    cout << " (7) Display Savings " << endl;
    cout << " (8) Display Checkings " << endl;
    cout << " (9) Display Credit Card " << endl;
    cout << " (0) Exit " << endl;
    cin >> option;

    do{

    switch ( option )

    {
        case 1 : double amtD;
                 cout << " Please enter how much you would like to deposit into savings " << endl;
                 cin >> amtD;
                 double sa.makeDeposit(double amtD);
                 break;
        case 2 : double makeWithdrawel();
                 break;
        case 3 : double makeDeposit();
                 break;
        case 4 : 
                 break;
        case 5 : 
                 break;
        case 6 : double makeWithdrawel();
                 break;
        case 7 : int display();
                 break;
        case 8 : int display();
                 break;
        case 9 : int display();
                 break;
        case 0 : exit = 0;
                 break;
        default : exit = 0;
                 cout << " ERROR ";
    }
    }
    while(exit==1);
    return 0;
}

这是我的班级储蓄:

#include "stdafx.h"
#include "iostream"
#include "Account.h"
#include <string>
#include <sstream>
using namespace std;

class saving: public account
{
public :

    double doWithdraw(double amount);
    saving();
    saving(string itsName, long itsTaxID, double itsBalance);
}

和我的帐户课程:

#include "stdafx.h"
#include <string>
#include <sstream>
using namespace std;
class account {

public :
    void setName(string name); void setTaxID(long taxID); void setBalance(double balance);
    string getName(); long getTaxID(); double getBalance();
    double makeDeposit( double amount );
    account();
    account(string itsName, long itsTaxID, double itsBalance);
    int display();

private :
    string itsName;
    long itsTaxID;
    double itsBalance;

protected :
    double last10withdraws[10];
    double last10deposits[10];
    int numdeposits;
    int numwithdraws;

};

知道我做错了什么吗?

4

2 回答 2

1

您错误地调用了函数。您不需要包含返回值和参数的类型。

case 10: int function(); break;

实际上是在声明一个本地函数——没有function像你期望的那样调用。

您的 switch 语句应如下所示。注意函数调用中没有类型

switch ( option )
{
case 1 : double amtD;
    cout << " Please enter how much you would like to deposit into savings " << endl;
    cin >> amtD;
    sa.makeDeposit(amtD);
    break;
case 2 : makeWithdrawel();
    break;
case 3 : makeDeposit();
    break;
case 4 : 
    break;
case 5 : 
    break;
case 6 : makeWithdrawel();
    break;
case 7 : display();
    break;
case 8 : display();
    break;
case 9 : display();
    break;
case 0 : exit = 0;
    break;
default : exit = 0;
    cout << " ERROR ";
}
于 2013-04-17T00:07:52.803 回答
0

下面的代码看起来有点奇怪。

cin >> amtD;
double sa.makeDeposit(double amtD);
break;

您正在尝试使用double关键字声明一个变量,但我认为您想做以下两件事之一:

  1. 无需使用 makeDeposit() 返回的 double 进行存款即可。在这种情况下,只需编写这样的行(使用返回的双精度是可选的):

    sa.makeDeposit(amtD);

  2. 您想进行存款,但也要保存一些退货信息。然后你可能会做这样的事情(并在以后使用新变量)。

    双 justMadeDeposit = sa.makeDeposit(amtD);

这是一些猜测。也许您根本不希望 makeDeposit() 函数返回任何内容,然后您可以将其声明为void

于 2013-04-17T00:08:34.613 回答