Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

How would you modify this code to use instead to have an array of pointers Account**accounts instead of Accounts *accounts. #pragma once class Account {

How would you modify this code to use instead to have an array of pointers Account**accounts instead of Accounts *accounts.

#pragma once class Account { private: int accountNum; double balance; public: Account(); Account(int, double); Account(const Account&); ~Account(); int getAccountNumber()const; double withdrawal(double);//need the balance! double deposit(double);//need the balance! void setbalance(double);//need the balance! double getbalance()const; };

#include #include "account.h" using namespace std; class Owner { private: string name; Account *accounts;//list of accounts (array of accounts) int numAccounts;//have this for each array!! const int capacity=10;//it is a const data member so initialize it in the member initialization list!!

public: Owner(int);// an int for the capacity to initialise it in the member initialization list!!. Owner(string,int); Owner(const Owner&, int); ~Owner(); void addAccount(const Account&); void removeAccount(int);//remove an account with the AccountNum!! void listAllAccounts(); bool searchAccount(int)const;//search an account with the AccountNum!! return true if we found it otherwise false. };

#include "owner.h" #include using namespace std;

Owner::Owner(int c) :capacity(c)//initialise the capacity in the member initialization lists { name = ""; accounts = new Account[capacity]; int numAccounts = 0; } Owner::Owner(string n, int c):capacity(c) { name = n; accounts = new Account[capacity]; int numAccounts = 0; }

Owner::Owner(const Owner&anotherOwner,int c):capacity(c) { name = anotherOwner.name; accounts = new Account[capacity]; int numAccounts = anotherOwner.numAccounts; //copy each element of the old array into the new one for (int i = 0; i { accounts[i] = anotherOwner.accounts[i]; } } Owner::~Owner() { delete[]accounts;//delete all attributes that are pointers!! } void Owner::addAccount(const Account&a)//to add an account we need to verify if we have not exceeded the capacity! { if (numAccounts <= capacity)//no for looop!!!!! { accounts[numAccounts] = a; numAccounts++; } else cout << "Cant add the account we have the maximum capacity of accounts"<< a.getAccountNumber() << endl;//we can add a.getAccountNumber()!!//a is const so //getAccountNumber() should also be const!! for a.getAccountNumber() to work!! } void Owner::removeAccount(int acc)//first find the account with the account Num { for (int i = 0; i < numAccounts; i++) { if (accounts[i].getAccountNumber() == acc)//2 == bc it is a const a number not a variable!!and dont forget() in the getAccountNumber()!!!!! { for (int j = i; j < numAccounts; j++) accounts[j] = accounts[j + 1]; numAccounts--; //dont forget to do numAccount--;!!! return; //you can write return to be faster to get out of the loop } else cout << "we couldnt find this account!"< } } void Owner::listAllAccounts() { for (int i = 0; i < numAccounts; i++) cout << "Account" << accounts[i].getAccountNumber() << endl; } bool Owner::searchAccount(int acc)const { for (int i = 0; i < numAccounts; i++) { if (accounts[i].getAccountNumber() == acc) return true; else return false; } }

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

Database Concepts

Authors: David M Kroenke, David J Auer

6th Edition

0132742926, 978-0132742924

More Books

Students also viewed these Databases questions

Question

Identify the critical elements in a performance management system

Answered: 1 week ago

Question

2. What potential barriers would you encourage Samuel to avoid?

Answered: 1 week ago

Question

6. How would you design your ideal position?

Answered: 1 week ago