In: Computer Science
4. Create a new project AccountPolymorphism and copy and paste the code from the attached AccountPolymorphism.cpp file. Compile and run. You will see that it is not showing output for Base class pointer to base class object data, not showing derived class output completely(missing word "Saving" in the output) and not showing any output for Base class pointer to derived class object(Saving). Fix these issue and submit the output.
// AccountPolymorphism.cpp : This file contains the 'main'
function. Program execution begins and ends there.
//
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
class Account
{
private:
int id;
double balance;
double annualInterestRate;
public:
Account()
{
id = 0;
balance = 0;
annualInterestRate = 0;
}
Account(int id, double balance, double annualInterestRate)
{
this->id = id;
this->balance = balance;
this->annualInterestRate = annualInterestRate;
}
double getMonthlyInterest() const
{
return balance * (annualInterestRate / 1200);
}
void withdraw(double amount)
{
balance -= amount;
}
void deposit(double amount)
{
balance += amount;
}
virtual string toString() const
{
stringstream ss;
ss << "Account id: " << id << " balance: "
<< balance;
return ss.str();
}
};
class Checkings : public Account
{
protected:
int overdraftLimit;
public:
Checkings(int id, double balance, double annualInterestRate)
:Account(id, balance, annualInterestRate)
{
overdraftLimit = 5000;
}
string toString(Account Acct) const
{
stringstream ss;
cout << Acct.toString() << endl;
ss << "Account Type : " << "Checking" <<
endl;
return ss.str();
}
};
class Saving : public Account
{
protected:
int overdraftLimit;
public:
Saving(int id, double balance, double annualInterestRate)
:Account(id, balance, annualInterestRate)
{
overdraftLimit = 5000;
}
string toString(Account Acct) const
{
stringstream ss;
cout << Acct.toString() << endl;
ss << "Account Type : " << "Saving" <<
endl;
return ss.str();
}
};
int main()
{
// Saving saving;
// Checkings checking;
cout << "Testing Account Class object " << endl
<< endl;
Account TestAccount{ 121,2000.00,0.5 };
cout << TestAccount.toString() << endl;
cout << "Depositing $1000 to Account object" << endl
<< endl;
TestAccount.deposit(1000.00);
cout << "Amount after depositing $1000 to Account object "
<< endl << endl;
cout << TestAccount.toString() << endl;
cout << "Withdrawing $2000 from the Account object " <<
endl << endl;
TestAccount.withdraw(2000.00);
cout << "Amount after witdrawing $2000 from the Account
object " << endl << endl;
cout << TestAccount.toString() << endl <<
endl;
cout << "Creating Savings object " << endl <<
endl;
Saving saving{ 122, 44000, 0.5 };
cout << saving.toString(saving) << endl;
cout << "Creating Checking object " << endl <<
endl;
Checkings checking{ 444, 88000, 0.8 };
cout << checking.toString(checking) << endl <<
endl;
// Using Base Class pointer at Base Class object
const Account* AccountPtr{ &TestAccount };
cout << "Base Class pointer at Base Class object" <<
endl << endl;
AccountPtr->toString();
cout << endl << endl;
// Using Derived Class pointer at Derived Class object
const Saving* SavingPtr{ &saving };
cout << "Derived Class pointer at Derived Class object"
<< endl << endl;
SavingPtr->toString(saving);
cout << endl << endl;
// Using Base Class pointer at Derived Class object
cout << "Base Class pointer at Derived Class object"
<< endl << endl;
AccountPtr = &saving;
AccountPtr->toString();
cout << endl << endl;
cout << "" << endl;
system("pause");
return 0;
}
Program needs to be corrected :
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
class Account
{
private:
int id;
double balance;
double annualInterestRate;
public:
Account()
{
id = 0;
balance = 0;
annualInterestRate = 0;
}
Account(int id, double balance, double annualInterestRate)
{
this->id = id;
this->balance = balance;
this->annualInterestRate = annualInterestRate;
}
double getMonthlyInterest() const
{
return balance * (annualInterestRate / 1200);
}
void withdraw(double amount)
{
balance -= amount;
}
void deposit(double amount)
{
balance += amount;
}
string toString() const // no need of virtual keyword
since no functions with same prototype exist in child
class
{
stringstream ss;
ss << "Account id: " << id << " balance: "
<< balance;
return ss.str();
}
};
class Checkings : public Account
{
protected:
int overdraftLimit;
public:
Checkings(int id, double balance, double annualInterestRate)
:Account(id, balance, annualInterestRate)
{
overdraftLimit = 5000;
}
string toString(Account Acct) const
{
stringstream ss;
cout << Acct.toString() << endl;
ss << "Account Type : " << "Checking" <<
endl;
return ss.str();
}
};
class Saving : public Account
{
protected:
int overdraftLimit;
public:
Saving(int id, double balance, double annualInterestRate)
:Account(id, balance, annualInterestRate)
{
overdraftLimit = 5000;
}
string toString(Account Acct) const
{
stringstream ss;
cout << Acct.toString() << endl;
ss << "Account Type : " << "Saving" <<
endl;
return ss.str();
}
};
int main()
{
// Saving saving;
// Checkings checking;
cout << "Testing Account Class object " << endl
<< endl;
Account TestAccount{ 121,2000.00,0.5 };
cout << TestAccount.toString() << endl;
cout << "Depositing $1000 to Account object" << endl
<< endl;
TestAccount.deposit(1000.00);
cout << "Amount after depositing $1000 to Account object "
<< endl << endl;
cout << TestAccount.toString() << endl;
cout << "Withdrawing $2000 from the Account object " <<
endl << endl;
TestAccount.withdraw(2000.00);
cout << "Amount after witdrawing $2000 from the Account
object " << endl << endl;
cout << TestAccount.toString() << endl <<
endl;
cout << "Creating Savings object " << endl <<
endl;
Saving saving{ 122, 44000, 0.5 };
cout << saving.toString(saving) << endl;
cout << "Creating Checking object " << endl <<
endl;
Checkings checking{ 444, 88000, 0.8 };
cout << checking.toString(checking) << endl <<
endl;
// Using Base Class pointer at Base Class object
const Account *AccountPtr =&TestAccount ;
cout << "Base Class pointer at Base Class object" <<
endl << endl;
cout<<AccountPtr->toString(); //cout is used
because here toString() is returning a string object with a copy of
the current contents of the stream
cout << endl << endl;
// Using Derived Class pointer at Derived Class object
const Saving* SavingPtr{ &saving };
cout << "Derived Class pointer at Derived Class object"
<< endl << endl;
SavingPtr->toString(saving);
cout << endl << endl;
// Using Base Class pointer at Derived Class object
cout << "Base Class pointer at Derived Class object"
<< endl << endl;
AccountPtr = &saving;
cout<<AccountPtr->toString(); //cout
is used because here toString() is returning a string object with a
copy of the current contents of the stream
cout << endl << endl;
cout << "" << endl;
system("pause");
return 0;
}
Output :
Testing Account Class object
Account id: 121 balance: 2000
Depositing $1000 to Account object
Amount after depositing $1000 to Account object
Account id: 121 balance: 3000
Withdrawing $2000 from the Account object
Amount after witdrawing $2000 from the Account object
Account id: 121 balance: 1000
Creating Savings object
Account id: 122 balance: 44000
Account Type : Saving
Creating Checking object
Account id: 444 balance: 88000
Account Type : Checking
Base Class pointer at Base Class object
Account id: 121 balance: 1000
Derived Class pointer at Derived Class object
Account id: 122 balance: 44000
Base Class pointer at Derived Class object
Account id: 122 balance: 44000
Explanation :