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
Employee **mycompany = {};andmycompany[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.Employee *mycompany = {}and removed the new keyword, as it is now pointing to objects, still my program crashes at runtime. Please Help.int main(void) { vector<Employee> a; a.push_back(Employee(10, "Mayukh", 200)); string name = a[0].getName(); cout << name << "\n"; return 0; }