CaTx
CaTx

Reputation: 1481

C++ inherited class not initialized properly

I have a class (SavingAccount) inherited from another class (Account). Somehow, it does not initialize properly.

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

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;
    }
};

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

I intialize as follow:

    SavingAccount acctSaving(2000, 5);

acctSaving shows its balance as 2000 when I jump inside it. However, when it gets outside, the balance is back to 0.

If I change this line:

Account::Account(b);

into:

setBalance(b);

It returns the object properly with the balance value of 2000. What goes wrong? How is the first method not right?

Upvotes: 0

Views: 147

Answers (2)

3CxEZiVlQ
3CxEZiVlQ

Reputation: 38465

You should not call parent constructors directly. But you can specify which parent constructor should be used when a child is being constructed.

When you call a parent constructor directly like a function call, you create a temporary object.

// 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;
    }
}

Upvotes: 2

Igor Tandetnik
Igor Tandetnik

Reputation: 52471

Account::Account(b) doesn't do what you seem to think it does. It creates an unnamed temporary variable of type Account, initialized with b, and then immediately destroyed.

What you are likely looking for is something like this:

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

See also: member initializer list

Upvotes: 3

Related Questions