Reputation: 87
I have a class called Account with below parameters:
Account::Account(string ibanCode, string paramOwner, double amount) {}
I have created a vector consisting of class Accounts inside main function:
accs.push_back(Account(fullName, iban, value));
I want to write a function to print all the Account values in my vector by a class member function called displayAll() , and so far I tried this:
void Account::displayAll()
{
for (int i = 0; i < accs.size(); i++)
{
cout << accs[i].displayBalance() << endl;;
}
}
And I want to write it inside the class file. Do you have any suggestions?
Upvotes: 0
Views: 125
Reputation: 212
I think making it a member would be extremely complicated, the best option should be using a normal function that can access the parameters.
#include <iostream>
#include <vector>
using namespace std;
struct Account {
Account (string ibanCode, string paramOwner, double amount) : _amount(amount), _ibanCode(ibanCode), _paramOwner(paramOwner) {};
string _ibanCode;
string _paramOwner;
double _amount;
};
void DisplayAll (const vector<Account>& Accs) {
for (const auto& Acc : Accs) {
cout << Acc._ibanCode<<' '<<Acc._paramOwner<<' '<< Acc._amount<<'\n';
}
return;
}
int main () {
vector<Account> Accs;
Accs.push_back(Account("SomeCode", "SomeOwner", 2.0));
Accs.push_back(Account("SomeOtherCode", "SomeOtherOwner", 3000.42));
DisplayAll(Accs);
}
To avoid complicating the answer too much I made a struct but you can either make the DisplayAll
function a friend
of the class or make some getters.
Upvotes: 1