I'm creating the first class representing a bank account. The second class represents the registry, using a vector to store objects of the first class. As you can see, I'm calling a function in the main, that calls a function from the second class which calls a function from the first class.
using namespace std;
class Account {
private:
string name;
double balance;
char allowNegative;
public:
Account();
void setName(string holderName);
void setAllowNegative(char ynNegative);
void setBalance(double balance);
string getName() const;
double getBalance() const;
bool yesNegative() const;
};
Account::Account() {
balance = 0;
}
class Registry {
private:
vector<Account> accounts;
public:
bool exists(string holderName);
void addAcount(Account currAcount);
Account getAccount(string holderName) const;
};
void Account::setName(string holderName) {
name = holderName;
}
void Account::setAllowNegative(char ynNegative) {
allowNegative = ynNegative;
}
void Account::setBalance(double transaction) {
balance = balance + transaction;
}
string Account::getName() const {
return name;
}
double Account::getBalance() const {
return balance;
}
bool Account::yesNegative() const {
if (allowNegative == 'y') {
return true;
}
return false;
}
bool Registry::exists(string holderName) {
for (int i = 0; i < accounts.size(); i++) {
if (accounts.at(i).getName == holderName) {
return true;
}
}
return false;
}
void Registry::addAcount(Account currAcount) {
accounts.push_back(currAcount);
}
Account Registry::getAccount(string holderName) const {
Account tempAccount;
for (int i = 0; i < accounts.size(); i++) {
if (accounts.at(i).getName == holderName) {
tempAccount = accounts.at(i);
break;
}
}
return tempAccount;
}
And in the main I have this part:
if (!regCopy.exists(holderName)) {
throw runtime_error("account does not exist");
}
regCopy is an object of class Registry
I saw many links regarding this problem, most of them include using pointers, and I'm not there yet. So I'm avoiding using that.

Aucun commentaire:
Enregistrer un commentaire