'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 arepublicby default, and inheriting from astructusespublicinheritance by default.in a
class, members areprivateby default, and inheriting from aclassusesprivateinheritance 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];
};
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 |
