Question

In: Computer Science

C++ I will thumb it up if its accurate. Learning Objective: To implement friend functions, and...

C++ I will thumb it up if its accurate.

Learning Objective: To implement friend functions, and overload operators as friend functions and member functions


Instructions:

  • Create a class called BankAccount
  • Declare the following member variable: double balance
  • Include the necessary constructors, accessors and mutators
    • Be sure to use the const keyword where necessary

Part 1 Overload the +, -,* and /, >=, and <= as friend Functions

  • Apply the overloaded the following operators as friend functions:
    +, -, *, /, >=, <=, << and >>
  • Implement all of them in main, any way you see fit, demonstrate their use.
ex of overloaded operator as friend function prototype: 
friend BankAccount operator +(const BankAccount& account1, const BankAccount& account2);
friend BankAccount operator -(const BankAccount& account1, const BankAccount& account2);
friend bool operator >=(const BankAccount& account1, const BankAccount& account2)
friend bool operator =<(const BankAccount& account1, const BankAccount& account2)
friend bool operator ==(const BankAccount& account1, const BankAccount& account2)

Part 2 Overload the +=, -=, and == as member functions

  • Apply the overloaded the following operators as friend functions:
    +=, == and -=
  • Implement all of them in main, any way you see fit, demonstrate their use.

Grading criteria

The following is the basic criteria to be used to grade your submission. You start with 30 points and then you start losing points, as you don't do something that is required.

-5: missing header

-5: does not implement friend functions properly

-5: missing constructors

-5: does not implement accessors and mutators where necessary

-5: does not use const where necessary

-5: coding style: missing indentation

-5 missing an operator

-5 does not overload as member function properly

-10: Incorrect output.

-30: Late submission.

Solutions

Expert Solution

Here is the answer for your question in C++ Programming Language.

Kindly upvote if you find the answer helpful.

NOTE : I have used dev-c++ to run this code, hence I had to used #include "BankAccount.cpp" in main.cpp,

if including #include "BankAccount.h" please include header file only.

###################################################################################

CODE :

BankAccount.h

#include<iostream>

using namespace std;

class BankAccount{
   //Class public attribute here
   public:
       //Necessary constructors
       BankAccount();
       BankAccount(double balance);
       ~BankAccount(); //Destructor
      
       //Accessor for balance
       double getBalance();
      
       //Mutator for balance
       void setBalancce(double balance);
      
       //Overloading operators
       //Part 1 Overload the +, -,* and /, >=, and <= as friend Functions
       friend BankAccount operator +(const BankAccount& account1, const BankAccount& account2);
       friend BankAccount operator -(const BankAccount& account1, const BankAccount& account2);
       friend bool operator>=(const BankAccount& account1, const BankAccount& account2);
       friend bool operator<=(const BankAccount& account1, const BankAccount& account2);
       friend bool operator==(const BankAccount& account1, const BankAccount& account2);
  
       //Part 2 Overload the +=, -=, and == as member functions
       BankAccount operator+=(const BankAccount& account2);
       BankAccount operator-=(const BankAccount& account2);
       bool operator==(const BankAccount& account2);
      
   //class private attributes here
   private:
       double balance;
};

################################################

BankAccount.cpp

//Include header
#include "BankAccount.h"

//Implementing constructors
BankAccount::BankAccount(){
   this->balance = 0;
}
BankAccount::BankAccount(double balance){
   this->balance = balance;
}
BankAccount::~BankAccount(){ }

//Implementing accessors and mutators
double BankAccount::getBalance(){
   return this->balance;
}
void BankAccount::setBalancce(double balance){
   this->balance = balance;
}
/*
   PART - 1
*/
//Implementing friend functions
BankAccount operator+(const BankAccount& account1, const BankAccount& account2){
   BankAccount account;      
   double balance = (account1.balance + account2.balance);
   account.setBalancce(balance);  
  
   return account;
}

BankAccount operator-(const BankAccount& account1, const BankAccount& account2){
   BankAccount account;  
   double balance = (account1.balance - account2.balance);
   account.setBalancce(balance);  
  
   return account;
}

bool operator>=(const BankAccount& account1, const BankAccount& account2){  
   return account1.balance >= account2.balance;
}

bool operator<=(const BankAccount& account1, const BankAccount& account2){
   return account1.balance <= account2.balance;
}

bool operator==(const BankAccount& account1, const BankAccount& account2){
   return account1.balance == account2.balance;
}

/*
   PART 2
*/
//Implementing member functions
BankAccount BankAccount::operator+=(const BankAccount& account2){
   this->balance += account2.balance;  
   return *this;
}
BankAccount BankAccount::operator-=(const BankAccount& account2){
   this->balance -= account2.balance;  
   return *this;
}
bool BankAccount::operator==(const BankAccount& account2){
   return this->balance == account2.balance;
}

###############################################################

main.cpp

#include "BankAccount.cpp"
#include<string>

int main(){
   BankAccount account1(10000);
   BankAccount account2(3000);
   BankAccount account3;
   BankAccount account4;
  
   cout << endl << "Using friend functions: " << endl;
   cout << "===================================" << endl;
   cout << "Account 1: (" << "Balance: " << account1.getBalance() << ")" << endl;
   cout << "Account 2: (" << "Balance: " << account2.getBalance() << ")" << endl;
  
   account3 = (account1 + account2);
   cout << endl << "Adding account1 and account2 " << account3.getBalance() << endl;
  
   account2.setBalancce(5000);
  
   cout << endl << "Account2 balance has been set to " << account2.getBalance() << endl;
  
   account4 = (account1 - account2);  
   cout << endl << "Subtracting account1 from account2 " << account4.getBalance() << endl;
      
   string res = (account1 >= account2)?"True":"False";
   cout << endl << "Is account1 greater than or equal to account2 ? " << res << endl;
  
   res = (account1 <= account2)?"True":"False";
   cout << endl << "Is account1 lesser than or equal to account2 ? " << res << endl;
  
   res = (account1 == account2)?"True":"False";
   cout << endl << "Is account1 equal to account2 ? " << res << endl;
  
   cout << endl << "Using member functions: " << endl;
   cout << "===================================" << endl;
   account3 = account1 += account2;  
   cout << endl << "After adding account2 to account1 using +=,balance in account1 is: " << account1.getBalance() << endl;
  
   account4 = account1 -= account2;  
   cout << endl << "After subtracting account2 from account1 using -=,balance in account1 is: " << account1.getBalance() << endl;
  
   res = (account1 == account2)?"True":"False";
   cout << endl << "Is account1 equal to account2 ? " << res << endl;
  
   return 0;
}

####################################################################

SCREENSHOTS :

Please see the screenshots of the code below for the indentations of the code.

BankAccount.h

#######################################################

BankAccount.cpp

################################################################

main.cpp

#########################################################################

OUTPUT :

NOTE : I had to put the comment "Using friend functions: " after displaying account balances. Kindly change accordingly.

Any doubts regarding this can be explained with pleasure :)


Related Solutions

C++ Please Fill in for the functions for the code below. The functions will implement an...
C++ Please Fill in for the functions for the code below. The functions will implement an integer list using dynamic array ONLY (an array that can grow and shrink as needed, uses a pointer an size of array). Additional public helper functions or private members/functions can be used. The List class will be instantiated via a pointer and called similar to the code below: class List { public: // Default Constructor List() {// ... } // Push integer n onto...
C++ Please Fill in for the functions for the code below. The functions will implement an...
C++ Please Fill in for the functions for the code below. The functions will implement an integer stack using deques ONLY. It is possible to use only one deque but using two deques also works. Additional public helper functions or private members/functions can be used. The Stack class will be instantiated via a pointer and called as shown below: Stack *ptr = new Stack(); ptr->push(value); int pop1 = ptr->pop(); int pop2 = ptr->pop(); bool isEmpty = ptr->empty(); class Stack{     public:...
C++ please Fill in for the functions for the code below. The functions will implement an...
C++ please Fill in for the functions for the code below. The functions will implement an integer stack using deques ONLY. It is possible to use only one deque but using two deques also works. Additional public helper functions or private members/functions can be used. The Stack class will be instantiated via a pointer and called as shown below: Stack *ptr = new Stack(); ptr->push(value); int pop1 = ptr->pop(); int pop2 = ptr->pop(); bool isEmpty = ptr->empty(); class Stack{     public:...
The objective of this assignment is to implement the tic-tac-toe game with a C program. The...
The objective of this assignment is to implement the tic-tac-toe game with a C program. The game is played by two players on a board defined as a 5x5 grid (array). Each board position can contain one of two possible markers, either ‘X’ or ‘O’. The first player plays with ‘X’ while the second player plays with ‘O’. Players place their markers in an empty position of the board in turns. The objective is to place 5 consecutive markers of...
PLEASE ANSWER I WILL RATE YOUR ANSWER AND THUMBS UP For the following C functions: int...
PLEASE ANSWER I WILL RATE YOUR ANSWER AND THUMBS UP For the following C functions: int q7(int x) {     return x & (~x+1); } int q8(int x, int m, int n) {     int a = ~m+1;     int b = ~x +1;     a = x + a;     b = b + n;     return !((a|b) >> 31); } int q9(int x, int n) {    /* assume x and n are not a negative integer */...
Data Science, I will give thumb up, thank you What is one critical drawback to the...
Data Science, I will give thumb up, thank you What is one critical drawback to the MLR (multiple linear regression model) model (or any MLR model) for predicting shardnado hazard? What are some modifications that could improve on this issue? sharknado hazard: the hazard of a sharknado, where 1 is very unlikely and 100 is highly likely
Implement two functions in C language: stringLength() - Takes a pointer to a string, and a...
Implement two functions in C language: stringLength() - Takes a pointer to a string, and a pointer to an int variable. It computes the length of the string and updates the int variable with the length. stringCopy() - Copies one string onto another using pointers #include<stdio.h> #define MAX_STR_LEN 2048 void stringLength(char *str, int *len) { // This function computes the length of the string // at the address in the pointer *str. Once the length // has been determined, it...
C++ Objective To wrap up this week, we will be looking at the Call by Value...
C++ Objective To wrap up this week, we will be looking at the Call by Value mechanism, and how it differs from Call by Reference; whether it is how they work in memory or how they help you accomplish a practical task, knowing the differences between these two mechanisms is important foundational knowledge. **You should have one main function + 4 other functions for your submission for this lab. you have no need for extra functions beyond that, and you...
For this program you will implement the following utility functions to test mastery of C strings....
For this program you will implement the following utility functions to test mastery of C strings. *******you MUST use these these function***** void removeBlanks(char *src, char *dest); void replaceChar(char *src, char oldChar, char newChar); char *flipCase(const char *src); Please read the description of these functions carefully. In the removeBlanks function you will implement a routine that takes a string in as src and outputs the same string into dest but removing any blank space character encountered. For example, if the...
Design and implement a C++ program with functions to calculate the pre-tax charge: If the customer...
Design and implement a C++ program with functions to calculate the pre-tax charge: If the customer subscribes to a phone plan called Plan200, then he is charged $5 for the first 200 minutes. For each additional minutes, the customer will be charged $0.10. If the customer subscribes to a phone plan called Max20, then he is charged $0.05 for each minute up to $20. (I.e., the customer never needs to pay more than $20.) If the customer is not subscribed...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT