1

I am trying to create an array of objects in C++. As C++ Supports native objects like int, float, and creating their array is not a problem.

But when I create a class and create an array of objects of that class, it's not working.

Here is my code:

#include <iostream>
#include <string.h>

using namespace std;
class Employee
{
    string name;
    int age;
    int salary;

public:
    Employee(int agex, string namex, int salaryx)
    {
        name = namex;
        age = agex;
        salary = salaryx;
    }

    int getSalary()
    {
        return salary;
    }

    int getAge()
    {
        return age;
    }

    string getName()
    {
        return name;
    }
};
int main(void)
{
    Employee **mycompany = {};

    //Create a new Object
    mycompany[0] = new Employee(10, "Mayukh", 1000);
    string name = mycompany[0]->getName();
    cout << name << "\n";

    return 0;
}

There is no compilation error, but when I'm running the Program, it is crashing. I don't know exactly what is happening here.

Please Help.

Here are some more details:

OS: 64bit Windows 8.1 on Intel x64 (i3) Architecture of Compiler: MinGW64 G++ Compiler

anastaciu
  • 23,467
  • 7
  • 28
  • 53
Mayukh
  • 23
  • 6
  • Your bug is here: `Employee **mycompany = {};` and `mycompany[0] = new Employee(10, "Mayukh", 1000);` if you are to create a 2D dynamic array you need to allocate the first dimension before using it. – drescherjm May 01 '21 at 14:44
  • Related: [https://stackoverflow.com/questions/936687/how-do-i-declare-a-2d-array-in-c-using-new](https://stackoverflow.com/questions/936687/how-do-i-declare-a-2d-array-in-c-using-new) – drescherjm May 01 '21 at 14:47
  • @drescherjm, How should I do that? I tried doing `Employee *mycompany = {}` and removed the new keyword, as it is now pointing to objects, still my program crashes at runtime. Please Help. – Mayukh May 01 '21 at 14:48
  • I noticed when I use Vectors, it just works Fine. Please Tell Me Where I am wrong – Mayukh May 01 '21 at 14:52
  • 1
    `int main(void) { vector a; a.push_back(Employee(10, "Mayukh", 200)); string name = a[0].getName(); cout << name << "\n"; return 0; }` – Mayukh May 01 '21 at 14:52
  • A vector should be the preferred way unless an academic restriction prevents you from using it. – drescherjm May 01 '21 at 15:01

2 Answers2

1

this is how you would do that:

#include <iostream>
#include <string.h>

using namespace std;

class Employee
{
    string name;
    int age;

    int salary;

    public:
        Employee(int agex, string namex, int salaryx)
        {
            name = namex;
            age = agex;
            salary = salaryx;
        }

        int getSalary()
        {
            return salary;
        }

        int getAge()
        {
            return age;
        }

        string getName()
        {
            return name;
        }
};

int main(void)
{
    //Create an Array length of 10 
    // in c++ its static you have to give the length of the array while declaring
    Employee mycompany [10];

    //Create a new Object
    mycompany[0] = new Employee(10, "Mayukh", 1000);

    string name = mycompany[0]->getName();

    cout << name << "\n";

    return 0;
}
Muaz
  • 19
  • 7
  • I tried that one, but my problem is I dont actually know the actual size of the array, meaning I'm Working with a dynamically sized array. Vector works just fine, But I want to know why my implementation does not works – Mayukh May 01 '21 at 14:54
  • You need default constructor, as mentioned in another answer. `Employee() = default; // or Employee(){}` – pvc May 01 '21 at 15:09
  • You can make a function that resizes the Array while you adding new objects to it i did here with int but you can do it with Employee too ```c++ void addToArray(int *&original, int &SIZE, const int &number) { int *temporiginal = new int[SIZE + 1]; //(final)new array for (int i = 0; i < SIZE; i++) //copy old array to new array { temporiginal[i] = original[i]; } temporiginal[SIZE] = number delete[] original; //delete old array original = temporiginal; //point old array to new array } ``` – Muaz May 01 '21 at 15:14
  • you can look here too :D http://www.cplusplus.com/forum/beginner/235218/ maybe ist clearer than – Muaz May 01 '21 at 15:16
  • and yea you need a constructor too See here: https://www.w3schools.com/cpp/cpp_constructors.asp – Muaz May 01 '21 at 15:19
1

The advice here, as always, is to use some STL container, like an std::vector.

That said you're probably going to need a default Employee constructor (unless you always initialize all the elements of the container with the constructor you already have, which is unlikely what you'll do, if you are to manually allocate memory, but more likely if you use a std::vector).

//...
Employee() = default; // or Employee(){}
Employee(int agex, string namex, int salaryx)
{
    name = namex;
    age = agex;
    salary = salaryx;
}
//...

If you really, absolutely, must do it by manual memory allocation, it would look roughly like this:

// Employee array with 5 employees, with the first two initialized with your constructor
Employee *mycompany = new Employee[5] {{10, "Mayukh1", 1000}, {20, "Mayukh2", 2000}};

//adding an employee
mycompany[2] = {30, "Mayukh3", 3000};

// it still has space for 2 more

Don't forget to delete the memory afterwards:

delete [] mycompany;

Live demo

anastaciu
  • 23,467
  • 7
  • 28
  • 53