So I am trying to define a class and I am using another array of a different class to define it.
//header file for Name.h
class Name {
string last;
string first;
};
//header file for Depositor.h
class Depositor {
Name name;
string ssn;};
//header file for Account.h
class Account {
Depositor depositor;
int acctnum;
string type;
double balance;
};
//header file fo开发者_StackOverflowr Bank.h
#include "Account.h"
class Bank {
Account account[]; //is this possible?
int active_accts;
};
When I am writing the cpp file I am running into a lot of problems!
//example of mutator
void Bank::setLastname(string lastname)
{
account.setLastname (lastname);
}
I didn't include the mutators and acessors that I wrote into the header file, but they are there and are public -- it won't compile. Can you help? Is it even valid to use an array of a class in Bank.h?
Is it even valid to use an array of a class in Bank.h?
Yes, but it has to have a fixed dimension, e.g.,
Account account[3];
A type always has a fixed size in C++, and since an array member variable forms part of the class's size, you need to specify how many elements are in the array.
If you don't know how many elements you are going to need, you can use a sequence container:
std::vector<Account> account;
Account
is not a nested class ofBank
.Bank
has a member data instance of typeAccount
array.You can have a primitive array member in a class, but you must specify the size of the array in the class definition:
Account account[42];
. The reason is that when you#include
the class definition in another compilation unit, and then instantiate an instance of the class, the compiler needs to know what the size of that instance is.It would be a wise idea to use
std::vector<Account>
rather than a primitive array.std::vector
doesn't require committing to a particular size at construction; it grows dynamically. How come astd::vector
doesn't require a size in the class definition, while a primitive array does? Astd::vector
holds as member a pointer to the elements on the heap. So the compiler does know the size of astd::vector
; it uses the size of the pointer rather than the count of the elements.
By declaring the value of the array in the header file and by adding a variable in the .cpp file you can solve all the problems and leave it as an array.
//header file
class Bank {
Account account[100];
int active_accts;
public:
//mutator
void setLastname (string,int);
};
//the implementation file
void Bank::setLastname (string last, int index)
{
account[index].setLastname(last);
}
this will solve all your problems
account
is an array of Account
s, which means you would need to do something like account[0].setLastname(lastname);
Instead of arrays, consider using vectors.
#include <vector>
// ...
class Bank {
std::vector<Account> accounts;
int active_accts;
};
you can't call setLastname(lastname)
on the whole array. You need to call it on a specific instance of the Account class inside the array, like this: account[0].setLastname(lastname);
On another note, you really should be storing an array of pointers to Account objects.
精彩评论