Array of Object in C++

Advertisements

C++ Array of Objects

An object of class represents a single record in memory, if we want more than one record of class type, we have to create an array of class or object. As we know, an array is a collection of similar type, therefore an array can be a collection of class type.

Syntax for Array of object


              class class-name
              {
                     datatype var1;
                     datatype var2;
                     - - - - - - - - - -
                     datatype varN;

                     method1();
                     method2();
                     - - - - - - - - - -
                     methodN();
              };

              class-name obj[ size ];

Example for Array of object


       #include<iostream.h>
       #include<conio.h>

       class Employee
       {
              int Id;
              char Name[25];
              int Age;
              long Salary;

              public:
              void GetData()           //Statement 1 : Defining GetData()
              {
                    cout<<"\n\tEnter Employee Id : ";
                    cin>>Id;

                    cout<<"\n\tEnter Employee Name : ";
                    cin>>Name;

                    cout<<"\n\tEnter Employee Age : ";
                    cin>>Age;

                    cout<<"\n\tEnter Employee Salary : ";
                    cin>>Salary;
              }

              void PutData()           //Statement 2 : Defining PutData()
              {
                    cout<<"\n"<<Id<<"\t"<<Name<<"\t"<<Age<<"\t"<<Salary;
              }

       };


       void main()
       {

              int i;

              Employee E[3];           //Statement 3 : Creating Array of 3 Employees

              for(i=0;i<3;i++)
              {
                    cout<<"\nEnter details of "<<i+1<<" Employee";
                    E[i].GetData();
              }

              cout<<"\nDetails of Employees";
              for(i=0;i<3;i++)
              E[i].PutData();

       }


   Output :

              Enter details of 1 Employee
                        Enter Employee Id : 101
                        Enter Employee Name : Suresh
                        Enter Employee Age : 29
                        Enter Employee Salary : 45000

              Enter details of 2 Employee
                        Enter Employee Id : 102
                        Enter Employee Name : Mukesh
                        Enter Employee Age : 31
                        Enter Employee Salary : 51000

              Enter details of 3 Employee
                        Enter Employee Id : 103
                        Enter Employee Name : Ramesh
                        Enter Employee Age : 28
                        Enter Employee Salary : 47000

              Details of Employees
                        101       Suresh      29      45000
                        102       Mukesh      31      51000
                        103       Ramesh      28      47000

				

In the above example, we are getting and displaying the data of 3 employee using array of object. Statement 1 is creating an array of Employee Emp to store the records of 3 employees.

Advertisement