Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C++ Object-Oriented Programming - So I'm having a problem with private within this context in my accrue function and my non-member function called createAccount .

C++ Object-Oriented Programming - So I'm having a problem with "private within this context" in my accrue function and my non-member function called createAccount. I've posted all the code for the two classes, I just need help with the two functions named. I'm not sure how I can directly manipulate private data using the functions I was given. I am not allowed to add any other functions to my classes. The two functions should behave in the way that I have tried to represent, if any information is lacking to fix those two functions, please let me know thank you.

class Money

{

public:

Money();

Money(int dol, int cen);

Money(int dol);

Money(double amt);

int getPennies() const;

bool isNegative() const;

void add(const Money &m);

void subtract(const Money &m);

bool isEqual(const Money &m) const;

void print() const;

private:

int dollars, cents;

};

// Place Account Class Definition Here

class Account

{

public:

Account();

Account(std::string accountName, double intRate, Money money);

Account(std::string accountName, double intRate, int balance);

Account(std::string accountName, double intRate, double balance);

std::string getName() const;

double getRate() const;

const Money getBalance() const;

void setName(std::string newName);

void deposit(const Money &m);

void deposit(int d, int c);

void deposit(int d);

void deposit(double d);

const Money withdraw(const Money &m);

const Money withdraw(int d, int c);

const Money withdraw(int d);

const Money withdraw(double d);

void accrue();

void print() const;

void transferTo(Account &dest, const Money &amount);

private:

Money money;

double intRate;

std::string accountName;

};

Money::Money() : dollars(0), cents(0)

{

}

Money::Money(int dol, int cen)

{

dollars = dol;

cents = cen;

if(dollars < 0 && cents > 0){

cents = -cents;

}

if(cents < 0 && dollars > 0){

dollars = -dollars;

}

if(cents > 100){

dollars += cents / 100;

cents %= 100;

}

}

Money::Money(int dol)

{

dollars = dol;

cents = 0;

}

Money::Money(double amt)

{

dollars = amt;

cents = (amt * 100 - dollars * 100);

}

int Money::getPennies() const

{

return dollars * 100 + cents;

}

bool Money::isNegative() const

{

if(dollars < 0 || cents < 0){

return true;

}

else {

return false;

}

}

void Money::add(const Money &m)

{

int pennies = getPennies() + m.getPennies();

dollars = pennies / 100;

cents = pennies % 100;

}

void Money::subtract(const Money &m)

{

int pennies = getPennies() - m.getPennies();

dollars = pennies / 100;

cents = pennies % 100;

}

bool Money::isEqual(const Money &m) const

{

if(getPennies() == m.getPennies()){

return true;

}

else {

return false;

}

}

void Money::print() const

{

if(isNegative()) {

std::cout << "$(" << -dollars << "." << -cents << ")" << std::endl;

}

else {

std::cout << "$" << dollars << "." << cents << std::endl;

}

}

// Implement Account Class Functions Here

Account::Account() : money(0,00), intRate(0), accountName("Savings")

{

}

Account::Account(std::string accountName, double intRate, Money balance)

{

this->accountName = accountName;

this->intRate = intRate;

this->money = balance;

}

Account::Account(std::string accountName, double intRate, int balance)

{

this->accountName = accountName;

this->intRate = intRate;

this->money = balance;

}

Account::Account(std::string accountName, double intRate, double balance)

{

this->accountName = accountName;

this->intRate = intRate;

this->money = balance;

}

std::string Account::getName() const

{

return accountName;

}

double Account::getRate() const

{

return intRate;

}

const Money Account::getBalance() const

{

return (money.getPennies());

}

void Account::setName(std::string newName)

{

accountName = newName;

}

void Account::deposit(const Money &m)

{

if(m.isNegative() == true){

std::cout << "Cannot deposit negative amount." << std::endl;

return;

}

money.add(m);

return;

}

void Account::deposit(int d, int c)

{

Money m(d,c);

if(m.isNegative() == true){

std::cout << "Cannot deposit negative amount." << std::endl;

return;

}

money.add(m);

return;

}

void Account::deposit(int d)

{

Money m(d);

if(m.isNegative() == true){

std::cout << "Cannot deposit negative amount." << std:: endl;

return;

}

money.add(m);

return;

}

void Account::deposit(double d)

{

Money m(d);

if(m.isNegative() == true){

std::cout << "Cannot deposit negative amount." << std::endl;

return;

}

money.add(m);

return;

}

const Money Account::withdraw(const Money &m)

{

if(m.isNegative() == true){

std::cout << "Cannot withdraw negative amount." << std::endl;

return -1;

}

if((m.getPennies()/100) > 50){

std::cout << "Cannot overdraw more than $50." << std::endl;

return -1;

}

money.subtract(m);

return money;

}

const Money Account::withdraw(int d, int c)

{

Money m(d,c);

if(m.isNegative() == true){

std::cout << "Cannot withdraw negative amount." << std::endl;

return -1;

}

if((m.getPennies()/100) > 50){

std::cout << "Cannot overdraw more than $50." << std::endl;

return -1;

}

money.subtract(m);

return money;

}

const Money Account::withdraw(int d)

{

Money m(d);

if(m.isNegative() == true){

std::cout << "Cannot withdraw negative amount." << std::endl;

return -1;

}

if((m.getPennies()/100) > 50){

std::cout << "Cannot overdraw more than $50." << std::endl;

return -1;

}

money.subtract(m);

return money;

}

const Money Account::withdraw(double d)

{

Money m(d);

if(m.isNegative() == true){

std::cout << "Cannot withdraw negative amount." << std::endl;

return -1;

}

if((m.getPennies()/100) > 50){

std::cout << "Cannot overdraw more than $50." << std::endl;

return -1;

}

money.subtract(m);

return money;

}

void Account::accrue()

{

Money m;

double interest = getRate() * (double(m.getPennies()));

//0.005 added to get nearest precision value in decimal

m.dollars = (int)interest;

m.cents = (interest - (int)interest)*100;

double amount = interest + account.amount.dollars + (double)account.amount.cents/100;

Money money;

money.dollars = (int)amount;

money.cents = (amount - (int)amount)*100;

account.amount = money;

}

void Account::print() const

{

money.print();

}

void Account::transferTo(Account &dest, const Money &amount)

{

if(amount.isNegative() == true){

std::cout << "Cannot transfer negative amount of money" << std::endl;

return;

}

dest.money.add(amount);

return;

}

// Implement Non-Member Function Here

const Account createAccount(){

std::cout << "Let's set up your account." << std::endl;

std::cout << "First, what's the name of your account? ";

std::string acc_name;

std::getline(std::cin, acc_name);

std::cout << "Whats the interest rate of your " << acc_name << " account? ";

double int_rate;

std::cin >> int_rate;

std::cout << "Finally, what is the starting balance of your " << acc_name << " account? $";

double amount;

std::cin >> amount;

Money money;

money.dollars = (int)amount;

money.cents = round((amount - (int)amount)*100);

Account acc;

accountName = acc_name;

intRate = int_rate;

money = money;

return acc;

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

Find the derivative of y= cos cos (x + 2x)

Answered: 1 week ago

Question

Do you think physicians should have unions? Why or why not?

Answered: 1 week ago