Implement 2 destructors.

Signed-off-by: Abdulkadir Furkan Şanlı <me@abdulocra.cy>
This commit is contained in:
Abdulkadir Furkan Şanlı
2022-01-12 14:26:17 +01:00
parent 26c5521eaa
commit be86d1338a
2 changed files with 46 additions and 0 deletions
+43
View File
@@ -68,6 +68,26 @@ Bank::closeAccount (Client &x)
return false; // Account doesn't exist. return false; // Account doesn't exist.
} }
// Only used in ~Client
void
Bank::closeAccountNoBail (Client &x)
{
for (auto i = clients.begin (); i != clients.end (); i++)
if (i->id == x.id)
{
clients.erase (i);
return;
}
}
Client::~Client ()
{
for (auto i : accounts)
{
i->closeAccountNoBail (*this);
}
}
bool bool
Bank::hire (Employee &x) Bank::hire (Employee &x)
{ {
@@ -263,6 +283,14 @@ Gov::bailout (Bank &x, const double amount)
return false; return false;
} }
Employee::~Employee ()
{
if (employer != nullptr)
{
employer->fire (*this);
}
}
int int
main () main ()
{ {
@@ -382,6 +410,21 @@ main ()
if (joe.getMattress () != 550) if (joe.getMattress () != 550)
exit (1); exit (1);
{ // Test ~Employee
Employee tempemp ("underwood");
boa.hire (tempemp);
}
if (boa.countEmployees () != 2)
exit (1);
{
Client tempclient ("guy");
boa.openAccount (tempclient);
}
if ((boa.countClients () != 2 && bailed == 1)
|| (boa.countClients () != 3 && bailed == 0))
exit (1);
// Run until both 0 and 1 are seen, in order to test both code paths for // Run until both 0 and 1 are seen, in order to test both code paths for
// bailout. // bailout.
cout << "Bailed out: " << bailed << ", all others successful." << endl; cout << "Bailed out: " << bailed << ", all others successful." << endl;
+3
View File
@@ -42,6 +42,7 @@ public:
bool fire (Employee &x); bool fire (Employee &x);
bool openAccount (Client &x); bool openAccount (Client &x);
bool closeAccount (Client &x); bool closeAccount (Client &x);
void closeAccountNoBail (Client &x);
// TODO: implement loans, for now there is placeholder loan() and unloan(), // TODO: implement loans, for now there is placeholder loan() and unloan(),
// in order to simulate loaning and test bailout mechanism. // in order to simulate loaning and test bailout mechanism.
@@ -85,6 +86,7 @@ class Employee
public: public:
std::string name; std::string name;
Employee (std::string x) : name (x){}; Employee (std::string x) : name (x){};
~Employee ();
private: private:
Bank *employer = nullptr; Bank *employer = nullptr;
@@ -97,6 +99,7 @@ class Client
public: public:
std::string id; std::string id;
Client (std::string x) : id (x), underMattress (0){}; Client (std::string x) : id (x), underMattress (0){};
~Client ();
double earn (const double amount); // Earn money. double earn (const double amount); // Earn money.
double getMattress (); double getMattress ();