admin管理员组文章数量:1648390
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/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;
}
};
版权声明:本文标题:1352 [BA1002] The Account class 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://m.elefans.com/xitong/1729498289a1203173.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论