1352 [BA1002] The Account class

来源:互联网 发布:淘宝用什么刷流量 编辑:程序博客网 时间:2024/06/07 12:14

Description

Deisgn a class named Account that contains:
An int data field named id for the account (default 0).
A double data field named balance for the account (default 0).
A double data field named annualInterestRate that stores the current interest rate (default 0).
A no-arg constructor that creates a default account.
The accessor and mutator functions for id, balance, and annualInterestRate.
A function named getMonthlyInterestRate() that returns the monthly interest rate. (Hint: monthlyInterestRate = annualInterestRate / 12)
A function name withDraw that withdraws a specified amount from the account.
A function name deposit that deposits a specified amount to the account.

Provided Codes

framework.cpp

#include <iostream>#include <iomanip>#include "source.h"using namespace std;void print(Account& account){  cout << "Account ID " << account.getId()   << fixed << setprecision(2)  << ", Balance " << account.getBalance() << ", Monthly Interest Rate " << account.getMonthlyInterestRate() << endl;}int main(){  Account a, b;  print(a);  b.setId(1122);  b.setBalance(20000.0);  b.setAnnualInterestRate(4.5);  print(b);  return 0;}

Submission

source.h

 class Account{public:    Account();    void setId(int );    void setBalance(double =0);    void setAnnualInterestRate(double =0);    int getId();    double getBalance();     double getMonthlyInterestRate();    void withDraw(double );    void doposit(double );private:    int id;    double balance;    double annualInterestRate;};Account::Account(){    id=0;}void Account::setId(int Id){    id=Id;}void Account::setBalance(double Balance){    balance=Balance;}void Account::setAnnualInterestRate(double Annual){    annualInterestRate=Annual;}int Account::getId(){    return id;}double Account::getBalance(){    return balance;}double Account::getMonthlyInterestRate(){    return annualInterestRate/12;}void Account::withDraw(double payout){    balance-=payout;}void Account::doposit(double income){    balance+=income;}

Standard Answer

source.h

// Problem#: 17645// Submission#: 4642239// The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License// URI: http://creativecommons.org/licenses/by-nc-sa/3.0/// All Copyright reserved by Informatic Lab of Sun Yat-sen University#include<iostream>#include<string>using namespace std;class Account {private:    int id;    double balance, annualInterestRate;public:    Account(int ID = 0, double BALANCE = 0, double AnnualInterestRate = 0) {        id = ID;        balance = BALANCE;        annualInterestRate = AnnualInterestRate;    }    int getId() {        return id;    }    double getBalance() {        return balance;    }    double getAnnualInterestRate() {        return annualInterestRate;    }    void setId(int ID) {        id = ID;    }    void setBalance(double Balance) {        balance = Balance;    }    void setAnnualInterestRate(double AnnualInterestRate) {        annualInterestRate = AnnualInterestRate;    }    double getMonthlyInterestRate() {        return annualInterestRate/12;    }    void withDraw(double amount) {        balance -= amount;        if ( balance < 0 ) balance = 0;    }    void deposit(double amount) {        balance += amount;    }};                                 
原创粉丝点击