Array of Objects in C++ Program

Array of Objects in C++ Program . An array of objects with all of their elements belonging to the same class can be declared in the same way as any other built-in type array. 

The array’s elements are all objects of that class. The ability to declare arrays of objects in this way emphasises the similarity between a class and a type.

Array of Objects in C++ Program

A data structure that contains a group of elements is known as an array. 

These items are often all of the same data type, such as an integer or a string. 

In computer programs, arrays are widely used to organise data so that a related group of numbers can be readily sorted or searched. 

An array is a collection of data elements of the same type stored in contiguous memory regions.

Arrays are a derived data type in the C programming language that may hold primitive data types like int, char, double, float, and so on.

Objects Definition in C++

In C++, an object is a physical object such as a chair, automobile, pen, phone, laptop, and so on. 

To put it another way, an object is an entity with state and behaviour. 

The terms “state” and “behaviour” are used interchangeably in this context. 

A runtime entity is one that is formed during runtime. 

When a class is defined, it just defines the object’s specifications; no memory or storage is allocated. 

We need to build objects in order to use the data and access functionalities defined in the class.

Getdata and Putdata in C++

There are two member functions in this class: getdata and putdata. 

The user should provide the following information to the function getdata: the student’s name, age, and subject grades. 

The student’s name, age, grade sum, and cur id should all be printed by the function putdata. 

Using this program we execute array of objects in c++.

#include<iostream>
using namespace std;
class student
{
int id;
char name[30];
public:
void getdata();
void putdata();
};
void student::getdata()
{
cout<<"Enter Id ";
cin>>id;
cout<<"Enter Name ";
cin>>name;
}
void student::putdata()
{
cout<<id<<" ";
cout<<name<<" ";
cout<<endl;
}
int main()
{
student stud; 
stud.getdata();
stud.putdata();
return 0;
}

Output:

Enter Id 45
Enter Name inlarn.com
45 inlarn.com