C ++继承的类未正确初始化

时间:2018-02-18 04:35:22

标签: c++ constructor

我有一个继承自另一个类(Account)的类(SavingAccount)。不知何故,它没有正确初始化。

=============================================== =========================

class Account
{
private:
    double balance;

public:
    // constructor
    Account()
    {
        balance = 0;
    }

    Account(double b)
    {
        if (b >= 0)
            balance = b;
        else
        {
            balance = 0;
            cout << "Balance cannot be negative." << endl;
        }
    }
    // destructor
    ~Account()
    {
        // nothing
    }

    // required functions
    bool credit(double a)
    {
        if (a > 0)
        {
            balance += a;
            return true;
        }
        else
        {
            cout << "Credit amount must be greater than 0." << endl;
            return false;
        }
    }

    bool debit(double a)
    {
        if (a > 0 && a <= balance)
        {
            balance -= a;
            return true;
        }
        else if (a > balance)
        {
            cout << "Debit amount exceeded account balance." << endl;
            return false;
        }
    }

    double getBalance()
    {
        return balance;
    }

    void setBalance(double b)
    {
        balance = b;
    }   
};

=============================================== =========================

class SavingAccount : public Account
{
private:
    double interestRate;    // percentage

public:
    // constructor
    SavingAccount()
    {
        Account::Account();
        interestRate = 0;
    }

    SavingAccount(double b, double r)
    {
        if (b >= 0 && r >= 0)
        {
            Account::Account(b);
            interestRate = r;
        }
        else
        {
            Account::Account();
            interestRate = 0;
            cout << "Balance or rate cannot be negative." << endl;
        }
    }
    // destructor
    ~SavingAccount()
    {
        // nothing
    }

    // required functions
    double calculateInterest()
    {
        return Account::getBalance() * interestRate / 100;
    }
};

=============================================== =========================

我初衷如下:

    SavingAccount acctSaving(2000, 5);
当我跳进它时,

acctSaving显示它的平衡为2000。但是,当它到达外面时,余额会回到0。

如果我更改此行:

Account::Account(b);

成:

setBalance(b);

它正确返回对象,余额值为2000.出了什么问题?第一种方法怎么不对?

2 个答案:

答案 0 :(得分:3)

Account::Account(b)并没有做你认为它做的事情。它创建一个Account类型的未命名临时变量,用b初始化,然后立即销毁。

你可能正在寻找的是这样的:

SavingAccount(double b, double r)
  : Account(b >= 0 && r >= 0 ? b : 0) {
    /* rest of code here */
}

另请参阅:member initializer list

答案 1 :(得分:2)

您不应直接调用父构造函数。但是,您可以指定在构造子项时应使用哪个父构造函数。

当您像函数调用一样直接调用父构造函数时,您将创建一个临时对象。

// constructor
SavingAccount() // Default parent constructor is called here implicitly
{
    interestRate = 0;
}

SavingAccount(double b, double r): Account(b) // Specify here parent constructor if default one is useless here
{
    if (b >= 0 && r >= 0)
    {
        interestRate = r;
    }
    else
    {
        interestRate = 0;
        cout << "Balance or rate cannot be negative." << endl;
    }
}