Question

      C++ -- Vector of Bank Accounts (15 pts) Please help! :( Use csegrid or Clion for...

      C++ -- Vector of Bank Accounts (15 pts) Please help! :(

  • Use csegrid or Clion for this part
  • Add on to the lab12a solution(THIS IS PROVIDED BELOW). You will add an overload operator function to the operator << and a sort function (in functions.h and functions.cpp)
    •    Print out the customer’s name, address, account number and balance using whatever formatting you would like
    •    Sort on account balance (hint: you can overload the < operator and use sort from the algorithms library [#include <algorithm>])
  • Remember to declare the operator << in the Bank class with friend ostream & operator << (ostream &out, Bank bank);
  • Then in functions.cpp, implement the operator as a stand alone function.
  • Declare a vector of 3 Bank objects, sort them, then print all 3 using the overloaded << operator.
  • Take a screenshot of the output showing the averages and place it below.
  • Turn in yourlastnamefirstLab13.cpp, Bank.h, Bank.cpp, functions.h, functions.cpp to canvas along with this lab and 2 screenshots.

Lab 12a Solution:

       Bank.h:

#ifndef BANK_H
#define BANK_H

#include <string>
using namespace std;
struct Customer //Capitalize Names of classes and structs
{
   
string name;
   
string address;

};

class Bank
{
private:
   
Customer customer;
   
int acctNumber;
   
float balance;
public:
    Bank();
//default constructor
   
Bank(string _name, int _number, float _balance); //constructor

   
string getName(){return customer.name;}
   
void setName(string _name){customer.name=_name;}
   
string getAddress(){return customer.address;}
   
void setAddress(string _address) {customer.address = _address;}
   
int getAcctNumber() {return acctNumber;}
   
void setAcctNumber(int _number){acctNumber = _number;}
   
float getBalance(){return balance;}
   
void increaseBalance(float amount);
   
void print();
};

#endif

Bank.cpp:

#include "Bank.h"
#include <iostream>
using namespace std;

Bank::Bank()
{
   
customer.name = "";
   
customer.address = "";
   
acctNumber = 0;
   
balance = 0;
}


Bank::Bank(string _name, int _number, float _balance)
{
   
customer.name = _name;
   
customer.address = ""; //this requires that you set address after declaring
   
acctNumber = _number;
   
balance = _balance;
}


void Bank::increaseBalance(float amount)
{
   
balance = balance + amount;
}


void Bank::print()
{
    cout
<< customer.name <<endl;
    cout
<< customer.address << endl;
    cout
<< "Account Number: " << acctNumber << endl;
    cout
<< "Account Balance: " << balance << endl;

}

lab12a.cpp:

#include <iostream>
#include "Bank.h"
using namespace std;
int main()
{
   
Bank acct1("Alpha", 123, 12.50); //constructor
   
Bank acct2; //default constructor

   
cout << "After declaring\n";
    cout
<< "Account1:\n";
    acct1.print();
    cout
<<"\nAccount2:\n";
    acct2.print();

    acct1.increaseBalance(
200.00);
    cout
<< "After increasing Balance\n";
    cout
<< "Account1:\n";
    acct1.print();

    cout
<< "Since we are outside the class definition \n";
    cout
<< "we must use public member functions to access private variables\n";
    cout
<< "Acct 1: " << acct1.getName() <<endl;
   
return 0;
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code and code as text for each file:

//Lab13.cpp

#include <iostream> 1 #include <algorithm> 2 #include<vector> 4 #include Bank . h 5 #include functions . h 6 using names

#include <iostream>

#include <algorithm>

#include <vector>

#include "Bank.h"

#include "functions.h"

using namespace std;

int main()

{

vector<Bank> accounts;

Bank acct1("Alpha", 123, 12.50); //constructor

Bank acct2("Beta", 456, 9.27);

Bank acct3("Gamma", 789, 11.22);

accounts.push_back(acct1);

accounts.push_back(acct2);

accounts.push_back(acct3);

cout << "Unsorted accounts are: \n\n";

for (int i = 0; i < accounts.size(); i++) {

cout << accounts[i] << endl;

}

cout << "\nSorting by account balance..\n";

SortByBalance(accounts);

cout << "Sorted accounts are: \n\n";

for (int i = 0; i < accounts.size(); i++) {

cout << accounts[i] << endl;

}

return 0;

}

//Bank.cpp

#include Bank . h 1 #include <iostream> 2 using namespace std; 3 Bank: : Bank(O. 6 customer.name = 7 customer.address =

void Bank::print ( ) 26 { 27 cout <<customer.name <<endl; 28 cout << customer.address << endl; 29 cout << Account Number:

#include "Bank.h"

#include <iostream>

using namespace std;

Bank::Bank()

{

customer.name = "";

customer.address = "";

acctNumber = 0;

balance = 0;

}

Bank::Bank(string _name, int _number, float _balance)

{

customer.name = _name;

customer.address = ""; //this requires that you set address after declaring

acctNumber = _number;

balance = _balance;

}

void Bank::increaseBalance(float amount)

{

balance = balance + amount;

}

void Bank::print()

{

cout << customer.name <<endl;

cout << customer.address << endl;

cout << "Account Number: " << acctNumber << endl;

cout << "Account Balance: " << balance << endl;

}

//Bank.h

#ifndef BANK_H #define BANK_H 2 3 #include <string> #include <iost ream> using namespace std struct Customer //Capitalize Nam

int getAcctNumber () {return acctNumber;} void setAcctNumber ( int _number) {acctNumber = float getBalance ( ) {return balanc

#ifndef BANK_H

#define BANK_H

#include <string>

#include <iostream>

using namespace std;

struct Customer //Capitalize Names of classes and structs

{

string name;

string address;

};

class Bank

{

private:

Customer customer;

int acctNumber;

float balance;

public:

Bank(); //default constructor

Bank(string _name, int _number, float _balance); //constructor

string getName(){return customer.name;}

void setName(string _name){customer.name=_name;}

string getAddress(){return customer.address;}

void setAddress(string _address) {customer.address = _address;}

int getAcctNumber() {return acctNumber;}

void setAcctNumber(int _number){acctNumber = _number;}

float getBalance(){return balance;}

void increaseBalance(float amount);

void print();

// overload << operator

friend ostream &operator<<(ostream &out, Bank bank) {

out << "Customer name: " << bank.customer.name << "\nAccount Number: " << bank.acctNumber << "\nBalance: " << bank.balance;

return out;

}

// overload < operator

bool operator <(Bank bank) {

if (balance < bank.balance) {

return true;

}

return false;

}

};

#endif

//functions.h

/* function to sort accounts on basis of account balance * 1 2 void SortByBalance (vector<Bank>) ; 3

/* function to sort accounts on basis of account balance */

void SortByBalance(vector<Bank>);

//functions.cpp

Bank . h #include 1 #include <vector> 2 #include <algorithm> 3 void SortByBalance (vector< Bank> accounts) sort (accounts.b

#include "Bank.h"

#include <vector>

#include <algorithm>

void SortByBalance(vector<Bank> accounts) {

sort(accounts.begin(), accounts.end()); // use sort from <algorithms>

}

Sample run:

Add a comment
Know the answer?
Add Answer to:
      C++ -- Vector of Bank Accounts (15 pts) Please help! :( Use csegrid or Clion for...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • This is assignment and code from this site but it will not compile....can you help? home...

    This is assignment and code from this site but it will not compile....can you help? home / study / engineering / computer science / computer science questions and answers / c++ this assignment requires several classes which interact with each other. two class aggregations ... Question: C++ This assignment requires several classes which interact with each other. Two class aggregatio... (1 bookmark) C++ This assignment requires several classes which interact with each other. Two class aggregations are formed. The program...

  • C++ please! OVERVIEW This homework builds on HW4. We will modify the classes to do a...

    C++ please! OVERVIEW This homework builds on HW4. We will modify the classes to do a few new things We will add copy constructors to the Antique and Merchant classes. We will overload the == operator for Antique and Merchant classes. We will overload the + operator for the antique class. We will add a new class the "MerchantGuild." Please use the provided templates for the Antique and Merchant classes, as we simplified their functionality for this homework. Also, you...

  • You are given a partial implementation of two classes. GroceryItem is a class that holds the...

    You are given a partial implementation of two classes. GroceryItem is a class that holds the information for each grocery item. GroceryInventory is a class that holds an internal listing of GroceryItems as well as the tax rate for the store. Your inventory could be 100 items it could be 9000 items. You won’t know until your program gets the shipment at runtime. For this you should use the vector class from the C++ standard library. I've completed the GroceryItem.h...

  • /* Implementation of the main() method of the program. */ #include <iostream> #include <string> #include <vector>...

    /* Implementation of the main() method of the program. */ #include <iostream> #include <string> #include <vector> #include <algorithm> using namespace std; class Student { public:    // default constructor    Student()    {        studentID = 0;        studentFirst = "First";        studentMiddle = "Middle";        studentLast = "Last";    }    void SetStudentID(int number) { studentID = number; }    void SetStudentFirst(string name) { studentFirst = name; }    void SetStudentMiddle(string name) { studentMiddle =...

  • Overview This checkpoint is intended to help you practice the syntax of operator overloading with member...

    Overview This checkpoint is intended to help you practice the syntax of operator overloading with member functions in C++. You will work with the same money class that you did in Checkpoint A, implementing a few more operators. Instructions Begin with a working (but simplistic) implementation of a Money class that contains integer values for dollars and cents. Here is my code: /********************* * File: check12b.cpp *********************/ #include using namespace std; #include "money.h" /**************************************************************** * Function: main * Purpose: Test...

  • //header files #ifndef Contact_H // header files should always have this to avoid #define Contact_H   ...

    //header files #ifndef Contact_H // header files should always have this to avoid #define Contact_H    // multiple inclusion in other files #include <string> // this is the only programming assignment which will use this statement. // normally "using namespace std" is looked down upon because it // introduces many common keywords that could be accidentally used, but // it identifies useful types such as string and would normally be used // std::string or std::vector. using namespace std; class Contact...

  • C++ Banks offer various types of accounts, such as savings, checking, certificate of deposits, and money...

    C++ Banks offer various types of accounts, such as savings, checking, certificate of deposits, and money market, to attract customers as well as meet their specific needs. Two of the most commonly used accounts are savings and checking. Each of these accounts has various options. For example, you may have a savings account that requires no minimum balance but has a lower interest rate. Similarly, you may have a checking account that limits the number of checks you may write....

  • C++ Language I have a class named movie which allows a movie object to take the...

    C++ Language I have a class named movie which allows a movie object to take the name, movie and rating of a movie that the user inputs. Everything works fine but when the user enters a space in the movie's name, the next function that is called loops infinetly. I can't find the source of the problem. Down below are my .cpp and .h file for the program. #include <iostream> #include "movie.h" using namespace std; movie::movie() { movieName = "Not...

  • This is a c++ code /**    Write a function sort that sorts the elements of...

    This is a c++ code /**    Write a function sort that sorts the elements of a std::list without using std::list::sort or std::vector. Instead use list<int>::iterator(s) */ using namespace std; #include <iostream> #include <iomanip> #include <string> #include <list> void print_forward (list<int>& l) { // Print forward for (auto value : l) { cout << value << ' '; } cout << endl; } void sort(list<int>& l) { write code here -- do not use the built-in list sort function }...

  • Subject: Object Oriented Programming (OOP) Please kindly solve the above two questions as soon as possible...

    Subject: Object Oriented Programming (OOP) Please kindly solve the above two questions as soon as possible would be really grateful to a quick solution. would give a thumbs up. Thank you! Q3: Question # 3 [20] Will the following code compile? If it does not, state the errors. If it does compile, write the output. //Function.cpp #include <iostream> using namespace std; void printData (long i) cout<<"In long print Data "«<i<<endl; } void printData(int i) cout<<"In int printData "<<i<<endl; ) void...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT