Ich bin ein wenig verwirrt mit Syntax, denke ich. Ich benötige Hilfe beim Hinzufügen eines zusätzlichen Datenelements zum Klassenkonto mit der Nummer. Wie würde ich das tun? Ich habe versucht, ein zusätzliches Datenelement namens Nummer hinzuzufügen, das eine eindeutige Kontonummer darstellt. Der Datenmember wird verwendet, um das Konto zu finden, so dass es einen Konstruktor geben muss oder die Konstruktoren und die "Anzeige" -Methode bearbeiten müssen. HierWie füge ich der Account-Klasse eine zusätzliche Datenelementnummer hinzu?
ist der Code für Account.h:
#define START_BALANCE 500.0
#define START_INTEREST_RATE 0.0175
// define Account class
class Account
{
private:
double balance;
doube interestRate;
public:
// constructor with default values
// set to default values if value(s) is/are not valid
Account(double initialBalance, double initialInterestRate);
// observers
double getBalance() const; // return balance
double getInterestRate() const; // return interestRate
void displayAccountInformation() const; // display formatted account information through cout
// transformers
bool deposit(double amount);
// disposit amount into account
// return false if unsuccessful (amount invalid, balance not changed)
// true otherwise
bool withdraw(double amount);
// withdraw amount from account
// return false if unsuccessful (amount invalid, balance not changed)
// true otherwise
void applyInterest(); // apply interestRate to balance
bool setInterestRate(double newRate);
// change interestRate to newRate
// return false if unsuccessful (newRate invalid, interestRate not changed)
// true otherwise
bool setBalance(double newBalance);
// change balance to newBalance
// return false if unsuccessful (newBalance invalid, balance not changed)
// true otherwise
}; // end Account
#endif
und hier ist der Code für Account.cpp
#include <iostream>
#include <iomanip>
#include "Account.h"
#include "ListDA.h"
using namespace std;
// constructor with default values
// set to default values if value(s) is/are not valid
Account::Account(double initialBalance = START_BALANCE, double initialInterestRate = START_INTEREST_RATE)
{
if (initialBalance >= 0)
balance = initialBalance;
else // initialBalance < 0
balance = START_BALANCE;
if (initialInterestRate >= 0 && initialInterestRate < 1)
interestRate = initialInterestRate;
else // initialInterestRate >= 1 or initialInterestRate < 0
interestRate = START_INTEREST_RATE;
} // end Account
// observers
double Account::getBalance() const
// return balance
{
return balance;
} // end getBalance
double Account::getInterestRate() const
// return interestRate
{
return interestRate;
} // end getInterestRate
void Account::displayAccountInformation() const
// display formatted account information through cout
{
cout << "Account Information" << endl
<< "Current Balance: $" << fixed << setprecision(2) << balance << endl
<< "Interest Rate: " << interestRate * 100 << "%" << endl;
} // end displayAccountInformation
// transformers
bool Account::deposit(double amount)
// disposit amount into account
// return false if unsuccessful (amount invalid, balance not changed)
// true otherwise
{
if (amount < 0) // invalid amount
return false;
balance += amount;
return true;
} // end deposit
bool Account::withdraw(double amount)
// withdraw amount from account
// return false if unsuccessful (amount invalid, balance not changed)
// true otherwise
{
if (amount < 0 || amount > balance) // invalid amount
return false;
balance -= amount;
return true;
} // end withdraw
void Account::applyInterest()
// apply interestRate to balance
{
balance *= 1.0 + interestRate;
} // end applyInterest
bool Account::setInterestRate(double newRate)
// change interestRate to newRate
// return false if unsuccessful (newRate invalid, interestRate not changed)
// true otherwise
{
if (newRate < 0 || newRate >= 1) // invalid newRate
return false;
interestRate = newRate;
return true;
} // end setInterestRate
bool Account::setBalance(double newBalance)
// change balance to newBalance
// return false if unsuccessful (newBalance invalid, balance not changed)
// true otherwise
{
if (newBalance < 0) // invalid balance
return false;
balance = newBalance;
return true;
} // end setBalance
Jede hilfreich beraten. Vielen Dank.
Double, um Geld zu repräsentieren? In dieser Wirtschaft? – Pubby
Diese "Frage" ist nicht in der richtigen Form. Was fragst du? Wir brauchen eine klare Frage, die ein mehr oder weniger spezifisches Problem darstellt und sinnvoll beantwortet werden kann. Sind Sie auf der Suche nach Code? Dafür gibt es einen StackExchange. Sind das Hausaufgaben? –
Nein, ich frage, wie Sie ein zusätzliches Mitglied zum Klassenkonto hinzufügen können. Ich habe das oben erwähnt. – Lea