Can't get Class Array within Another Class Working

Jun 1, 2018 at 9:42pm
I am working on a beginner project that requires an array of Class_A to be an object of Class_B


Below is what I am trying
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class Customers{

    std::string First_Name;
    std::string Last_Name;
    int Age;
    std::string Address;
    std::string City;
    std::string Phone;

public:
    Customer();
    Customer(std::string fName, std::string lName, int Age, std::string Address, std::string City, std::string Phone);

class GroupType{

public:
std::string GroupName;


// Allocate memory for Customer Array that accepts 5 objects from the class Customer
Customer *premiumGroup = new Customer[5];


One of the Objects I want to insert into array:

 
Customer *Customer1 = new Customer("Johnny","Bravo", 90,"2342 Hairwax Ave", "Omaha","(100) 100-1000");


I created a construction to deal with the private variables. They are not an issue. I don't know how I to call create the class array inside this other class and how to insert items into it because its a double class.

Last edited on Jun 1, 2018 at 9:43pm
Jun 1, 2018 at 10:36pm
Something like this?
1
2
3
4
5
6
7
8
9
10
11
class GroupType {
  std::string GroupName;
  std::vector<Customer> premiumGroup;
public:
  GroupType();
};

GroupType::GroupType()
 : premiumGroup( {{"Johnny","Bravo", 90,"2342 Hairwax Ave", "Omaha","(100) 100-1000"}} )
{
}
Jun 2, 2018 at 5:54pm
I need to use an array for this.

An array holding customers as an object of the GroupType Class.
Jun 2, 2018 at 8:38pm
I need to use an array for this.

Need? As in "I must practice low-level, manual memory management even though I should not use it in production code"?

Array is not an "object". Perhaps the class GroupType has to have an array as member?

What kind of array?

Plain array:
1
2
class GroupType {
  Customer premiumGroup[5];

Dynamically allocated array:
1
2
class GroupType {
  Customer * premiumGroup;

Plain array of pointers:
1
2
class GroupType {
  Customer * premiumGroup[5];

Dynamically allocated array of pointers:
1
2
class GroupType {
  Customer ** premiumGroup;

You did rule out the vector, which is effectively an array too.
Topic archived. No new replies allowed.