Create new object array with new operator - C++ Class

C++ examples for Class:object

Description

Create new object array with new operator

Demo Code

#include <iostream>
#include <new>
#include <string>
using namespace std;

struct Employee//www  .j  a va 2s.c om
{
    string Name;
    int Age;
};

int main()
{
    Employee* DynArray;

    DynArray = new (nothrow) Employee[3];
    DynArray[0].Name = "H";
    DynArray[0].Age = 33;
    DynArray[1].Name = "S";
    DynArray[1].Age = 26;
    DynArray[2].Name = "J";
    DynArray[2].Age = 52;

    cout << "Displaying the Array Content" << endl;

    for (int i = 0; i < 3; i++)
    {
        cout << "Name: " << DynArray[i].Name <<
            "\tAge: " << DynArray[i].Age << endl;
    }

    delete[] DynArray;

    return 0;
}

Result


Related Tutorials