Question
C++ (friend functions & overloaded operators) Any help is greatly appreciated. I feel very lost in this assignment, not sure how to print the correct
C++ (friend functions & overloaded operators)
Any help is greatly appreciated.
I feel very lost in this assignment, not sure how to print the correct output with the friend function and the overloaded operators ( > ,
1.Add a friend function, ostream &operator
2.Add the following overloaded operator functions:
bool operator>(const Account &) bool operator or
Write a test program that creates five Account objects in a dynamic array (use new to create the Account array). The account IDs are 1000 to 5000, balance of $1,000 to 5000, and an annual interest rate of 5%.
Create another single Account object, accountObj, with ID=3000, and balance=3000.
Use a for-loop to compare the accountObj with each account object in the dynamic array with ==, !=, > and
Display each object information use cout
Dont forget to delete the dynamic array before the end of program.
The correct output:
My output( which is incorrect):
My code so far:
// ACCOUNT.H
----------------------------------------
#ifndef Account_h
#define Account_h
#include
using namespace std;
class Account
{
const int id;
double balance;
double static annualInterestRate;
static int NumberOfObjects;
double setBalance(double);
friend ostream &operator
public:
Account(const int = 0, double = 0.0); // Constructor
~Account(); // Destructor
// set function
void static setAnnualInterestRate(double); // set the initial value as 5%
// get functions
unsigned int getID() const;
double getBalance() const;
static double getAnnualInterestRate();
static int getNumberofObjects();
double getMonthlyInterestRate() const;
double getMonthlyInterest() const;
void withdraw(double);
void deposit(double);
// overloaded operator functions (bool data type is for true&false values)
bool operator>(const Account &);
bool operator
bool operator==(const Account &);
bool operator!= (const Account &);
};
#endif // Account_h
----------------------------------------
// ACCOUNT.CPP
#include
#include
#include
#include "Account.h"
using namespace std;
double Account::annualInterestRate = 0.05;
int Account::NumberOfObjects = 0;
Account::Account(const int newID, double newBalance) // constructor initialises each data member
: id(newID), balance(newBalance)
{
cout
NumberOfObjects++;
cout
}
Account::~Account() // destructor
{
cout
--NumberOfObjects;
cout
}
double Account::setBalance(double Newbalance)
{
this->balance = Newbalance;
return 0;
}
void Account::setAnnualInterestRate(double newannualInterestRate)
{
annualInterestRate = newannualInterestRate;
}
unsigned int Account::getID() const
{
return id;
}
double Account::getBalance() const
{
return balance;
}
double Account::getAnnualInterestRate()
{
return annualInterestRate;
}
int Account::getNumberofObjects()
{
return NumberOfObjects;
}
double Account::getMonthlyInterestRate() const
{
return (annualInterestRate / 12);
}
double Account::getMonthlyInterest() const
{
return balance*(annualInterestRate / 12);
}
void Account::withdraw(double withdrawAmount)
{
if (balance >= withdrawAmount)
{
balance -= withdrawAmount; //balance = balance - withdrawAmount
}
else
throw invalid_argument("No sufficient balance");
}
void Account::deposit(double depositAmount)
{
balance += depositAmount; //balance = balance + depositAmount
}
ostream &operator
{
{
output
return output;
}
}
bool Account::operator>(const Account &newBalance) // checking if greater than
{
if(this->balance > newBalance.balance)
return false;
// Need to fix 'return ;'
return true;
}
bool Account::operator
{
if (this->balance
return false;
// Need to fix 'return ;'
return true;
}
bool Account::operator==(const Account &newID) // checking equality
{
if (this->balance == newID.id)
return false;
// Need to fix 'return ;'
return true;
}
bool Account::operator!= (const Account &newID) // checking if not equal to
{
if (this->balance != newID.id)
return false;
// Need to fix 'return ;'
return true;
}
----------------------------------------
// ACCOUNTMAIN.CPP
#include
#include
#include
#include "Account.h"
using namespace std;
// creates five Account objects in a dynamic array (use new to create the Account array).
//The account IDs are 1000 to 5000, balance of $1,000 to 5000, and an annual interest rate of 5%.
Account *AccountPtr = new Account[5]{ { 1000,1000 },{ 2000,2000 },{ 3000,3000 },{ 4000,4000 },{ 5000,5000 } };
Account accountObj(3000, 3000); // Create single Account object, accountObj, with ID = 3000, and balance = 3000
int main()
{
Account output;
cout
cout
cout
cout
cout
return 0;
}
. UML Diagram Account id: int coast balance: double annuallnterestRate: double NumberOfObiects: int + > Account(id: int=0, balance: double=0) + >Account() +getlD: int const setBalance(balance: double + getBalance): double const + setAnnuallnterestRate(annualilnterestRate: double) + getAnnualnterestRate: double + getNumberOfObiects: int + getMonthlylnterestRate0: double + getMonthlylnterest( + withdraw(amount: double) + deposit(amount: double) (friend) &operator (const Account &): bool + operatorStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started