关于调用继承函数

时间:2013-04-16 23:40:11

标签: c++ class inheritance methods

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

};

对我做错了什么?

2 个答案:

答案 0 :(得分: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 ";
}

答案 1 :(得分:0)

以下代码看起来有点奇怪。

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

您正在尝试使用 double 关键字声明变量,但我认为您希望执行以下两项操作之一:

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

    sa.makeDeposit(AMTD);

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

    double justMadeDeposit = sa.makeDeposit(amtD);

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