编写具有三个类的Bank应用程序:基类BankAccount和两个继承类SavingsAccount和CreditCard

时间:2012-04-22 02:34:17

标签: c++ inheritance

对于家庭作业,我必须编写一个BankAccount类,然后编写两个继承自该基类的Classes,CreditCard和Savings。我已经编写了应用程序,但输出与所需的输出不完全相同,后者与主Bank.cpp一起提供。

这是我的输出:

*Savings Page.
Setting Savings Account interest Rate at 12%
Opening my account on day 2 with $35.46
After 32 more days, I'm depositing an additional $245.64.
Current Balance(on day 34):   281.1
Withdrawl $285.05 on day 100
Couldn't make the withdrawl.  Not enough money! 281.1*

这是必需的输出:

*Savings Page.
Setting Savings Account interest Rate at 12%
Opening my account on day 2 with $35.46
After 32 more days, I'm depositing an additional $245.64.
Current Balance(on day 34):   281.473
Withdrawl $285.05 on day 100
Current Balance(after withdrawl): 2.53064*

这是我的申请:

//  BankAccount.h   
#pragma once

class BankAccount
{
public:
    BankAccount(void);
    ~BankAccount(void);
    float CalculateInterest(int Time);
    float GetInterestRate();
    float GetPrincipal();
    void SetInterestRate(float IR);
    void SetPrincipal(int Time, float Amount);
private:
    float mInterestRate;
    double mPrincipal;
    int mLastAccessTime;
}; 

//  BankAccount.cpp
#include "StdAfx.h"
#include "BankAccount.h"

 BankAccount::BankAccount(void)
:mInterestRate(0),mPrincipal(0),mLastAccessTime(0) {}


BankAccount::~BankAccount(void) {}

void BankAccount::SetInterestRate(float IR)
{
    mInterestRate = IR;
}

void BankAccount::SetPrincipal(int Time, float Amount)
{
    mLastAccessTime = Time;
    mPrincipal = Amount;
}

float BankAccount::CalculateInterest(int Time)
{
    float InterestEarned;
    Time = (Time - mLastAccessTime) / 360;
    float mInterestRate = ((mInterestRate * Time) / 100) * mPrincipal;
    return mInterestRate; 
}

float BankAccount::GetInterestRate()
{
    return mInterestRate;
}

float BankAccount::GetPrincipal()
{
    return mPrincipal;
}

//  Savings.H
// Inheritance class of BankAcount
#pragma once
#include "BankAccount.h"

class Savings : public BankAccount
{
public:
    Savings(void);
    ~Savings(void);
    void Deposit(int Time, float Amount);
    bool Withdrawal(int Time, float Amount);
    float GetBalance(int Time);
};

//  Savings.cpp
//  Inheritance class of BankAcount
#include "StdAfx.h"
#include "Savings.h"
#include <iostream>

Savings::Savings(void) {}
Savings::~Savings(void) {}

void Savings::Deposit(int Time, float Amount)
{ 
    if ( Time = 0 )
        SetPrincipal(Time, Amount);
    else
        SetPrincipal(Time, (Amount + GetPrincipal()));
}

bool Savings::Withdrawal(int Time, float Amount)
{
    if (GetPrincipal() >= Amount)
    {
        GetPrincipal() - Amount;
        return true;
    }
    else
        return false;
}

float Savings::GetBalance(int Time)
{
    return ( GetPrincipal() + CalculateInterest(Time));
}

//  CreditCard.h
#pragma once
#include "BankAccount.h"

class CreditCard : public BankAccount
{
public:
    CreditCard(void);
    ~CreditCard(void);
    void Payment(int Time, float Amount);
    void SetCreditLimit(float Amount);
    float getAvailableCredit(int Time);
    bool Charge(int Time, float Amount);
};

//  CreditCard.cpp

#include "StdAfx.h"
#include "CreditCard.h"

CreditCard::CreditCard(void) {}
CreditCard::~CreditCard(void) {}

void CreditCard::Payment(int Time, float Amount)
{
    GetPrincipal() + CalculateInterest(Time) - Amount;
}

void CreditCard::SetCreditLimit(float Amount) {}

float CreditCard::getAvailableCredit(int Time)
{
    return ( GetPrincipal() + CalculateInterest(Time));
}

bool CreditCard::Charge(int Time, float Amount)
{
    if (getAvailableCredit(Time) >= Amount)
    {
        (getAvailableCredit(Time) - Amount);
        return true;
    }
    else
        return false;
}

0 个答案:

没有答案