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