Lizz Craighten
Lizz Craighten

Reputation: 13

C++ : Using an array in a nested class definition (OOP)

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 for 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?

Upvotes: 1

Views: 2151

Answers (6)

Lizz Craighten
Lizz Craighten

Reputation: 28

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

Upvotes: 0

wilhelmtell
wilhelmtell

Reputation: 58675

  • Account is not a nested class of Bank. Bank has a member data instance of type Account 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 a std::vector doesn't require a size in the class definition, while a primitive array does? A std::vector holds as member a pointer to the elements on the heap. So the compiler does know the size of a std::vector; it uses the size of the pointer rather than the count of the elements.

Upvotes: 1

Scott M.
Scott M.

Reputation: 7347

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.

Upvotes: 0

user600838
user600838

Reputation:

Instead of arrays, consider using vectors.

#include <vector>

// ...

class Bank {
    std::vector<Account> accounts;
    int active_accts;
};

Upvotes: 0

Foo Bah
Foo Bah

Reputation: 26271

account is an array of Accounts, which means you would need to do something like account[0].setLastname(lastname);

Upvotes: 0

James McNellis
James McNellis

Reputation: 355099

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;

Upvotes: 2

Related Questions