'C++ How do I create an array of an inherited class?

C++ Newbie here,

When I was using struct I would do it like this:

struct Employee
{
string name;
int id;
}

   struct Company
{
string companyName;
Employee employees[20]
}

Now that I am using classes it is confusing for me on how do I create the same array of employees in the Company class. Do I need to inherit employee and do it the same way, should I not inherit it at all. Thanks



Solution 1:[1]

struct and class are practically identical, pretty much the only difference between them is that:

  • in a struct, members are public by default, and inheriting from a struct uses public inheritance by default.

  • in a class, members are private by default, and inheriting from a class uses private inheritance by default.

The following class code works exactly the same as the struct code:

class Employee
{
public:
    string name;
    int id;
};

class Company
{
public:
    string companyName;
    Employee employees[20];
};

Online Demo

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Remy Lebeau