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...

  • Greetings, everybody I already started working in this program. I just need someone to help me...

    Greetings, everybody I already started working in this program. I just need someone to help me complete this part of the assignment (my program has 4 parts of code: main.cpp; Student.cpp; Student.h; StudentGrades.cpp; StudentGrades.h) Just Modify only the StudentGrades.h and StudentGrades.cpp files to correct all defect you will need to provide implementation for the copy constructor, assignment operator, and destructor for the StudentGrades class. Here are my files: (1) Main.cpp #include <iostream> #include <string> #include "StudentGrades.h" using namespace std; int...

  • 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 }...

  • 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...

  • //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...

  • Please zoom in so the pictures become high resolution. I need three files, FlashDrive.cpp, FlashDrive.h and...

    Please zoom in so the pictures become high resolution. I need three files, FlashDrive.cpp, FlashDrive.h and user_main.cpp. The programming language is C++. I will provide the Sample Test Driver Code as well as the codes given so far in text below. Sample Testing Driver Code: cs52::FlashDrive empty; cs52::FlashDrive drive1(10, 0, false); cs52::FlashDrive drive2(20, 0, false); drive1.plugIn(); drive1.formatDrive(); drive1.writeData(5); drive1.pullOut(); drive2.plugIn(); drive2.formatDrive(); drive2.writeData(2); drive2.pullOut(); cs52::FlashDrive combined = drive1 + drive2; // std::cout << "this drive's filled to " << combined.getUsed( )...

  • Hi, I have C++ programming problem here: Problem: Please modify your string type vector in for...

    Hi, I have C++ programming problem here: Problem: Please modify your string type vector in for push_back() function as below: void push_back(string str) { // increase vector size by one // initialize the new element with str } In addition, the standard library vector doesn't provide push_front(). Implement push_front() for your vector. Test your code with the main function below. int main() {    vector v1(3);    cout<<"v1: ";    v1.print(); // this should display -, -, -    for...

  • You are to implement a MyString class which is our own limited implementation of the std::string...

    You are to implement a MyString class which is our own limited implementation of the std::string Header file and test (main) file are given in below, code for mystring.cpp. Here is header file mystring.h /* MyString class */ #ifndef MyString_H #define MyString_H #include <iostream> using namespace std; class MyString { private:    char* str;    int len; public:    MyString();    MyString(const char* s);    MyString(MyString& s);    ~MyString();    friend ostream& operator <<(ostream& os, MyString& s); // Prints string    MyString& operator=(MyString& s); //Copy assignment    MyString& operator+(MyString&...

  • 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...

  • 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...

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
ADVERTISEMENT