An array is a data structure with the help of which a programmer can refer to perform operations on a collection of similar data types such as simple lists or tables. An array is a built-in data structure of every programming language. The index of the array is called a subscript. Therefore individuals element of the array is called subscripted variables.



Arrays of  Objects:

C++ provides a declaration of arrays of objects that are the same as arrays as of any other type. An individual element of arrays can be accessed by Subscript.

Example program:

Write a program that can collect book records ⏺ in an object array.

#include<iostream>
using namespace std;
class books
{
 private:
 char Book_name[20];
 int price;
 public:
 void put_data();
 void display_data();
};
void books::put_data()
{
 cout<<"Enter book name:";
 cin>>Book_name;
 cout<<"Enter price:";
 cin>>price;
}
void books::display_data()
{
 cout<<"Book name:"<<Book_name<<endl;
 cout<<"Price:"<<price<<endl;
}
int main()
{
 int n;
 books bokobj[50];
 cout<<"Enter Total number of books:";
 cin>>n;
 for(int i=0;i<=n;i++)
{
 bokobj[i].put_data();	
}
 for(int i=0;i<=n;i++)
{
 bokobj[i].display_data();
}

}

Object as a Function Argument:

C++ collect object as a built-in datatype and therefore an object can be passed to a function in two ways

  • Pass by value 
  • Pass by reference 
Pass by value:

In pass by value, the objects are passed to function by value. A copy of objects is received by the called function, not the actual object. The called function can only access public members. 

Example Program:

void test (books ob)//pass by value
{
 ob.put_data();
 cout<<"\nDisplay in test"<<endl;
 ob.display_data();
}
};
int main()
{
books bokobj;
bokobj.test(bokobj);  	
}

Pass by reference:

In this method address of an object is passed to function as an argument. The address of an object is representing by preceding the object name with the character '&'.

void test (books &ob)//pass by refrence
{
 ob.put_data();
 cout<<"\nDisplay in test"<<endl;
 ob.display_data();
}
};

int main()
{
  books bokobj;
  bokobj.test(bokobj);  	
}