【问题标题】:Regarding calling inherited functions关于调用继承函数
【发布时间】:2013-04-16 23:40:11
【问题描述】:

我有一个名为 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;

};

知道我做错了什么吗??

【问题讨论】:

  • 您遇到的具体问题是什么/您没有得到预期输出的示例输入(以及您实际得到的输出)是什么?

标签: c++ class inheritance methods


【解决方案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 ";
}

【讨论】:

  • 谢谢显而易见的船长。完美运行。
【解决方案2】:

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

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

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

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

    sa.makeDeposit(amtD);

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

    double justMadeDeposit = sa.makeDeposit(amtD);

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

【讨论】:

  • 实际上连编译都不会。
  • @CaptainObvlious:哦,现在我明白你的意思了。我会解决的。
猜你喜欢
  • 1970-01-01
  • 2016-06-06
  • 1970-01-01
  • 1970-01-01
  • 2019-03-04
  • 2017-04-05
  • 2011-04-14
  • 2017-09-19
  • 2020-09-10
相关资源
最近更新 更多