0% found this document useful (0 votes)
11 views6 pages

Array of Objects in C++

Uploaded by

ketankumar2614
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views6 pages

Array of Objects in C++

Uploaded by

ketankumar2614
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Array of Objects in C++

Raj Kumar
Array
 An array in C/C++ or be it in any programming
language is a collection of similar data items
stored at contiguous memory locations and
elements can be accessed randomly using indices
of an array.
 They can be used to store the collection of
primitive data types such as int, float, double,
char, etc of any particular type.
 To add to it, an array in C/C++ can store derived
data types such as structures, pointers, etc.
Array
Array of Objects
 Like array of other user-defined data types, an
array of type class can also be created.
 The array of type class contains the objects of
the class as its individual elements.
 Thus, an array of a class type is also known as
an array of objects.
 An array of objects is declared in the same way
as an array of any built-in data type.
Array of Objects
 When a class is defined, only the specification for the
object is defined; no memory or storage is allocated.
 To use the data and access functions defined in the
class, you need to create objects.

The syntax for declaring an array of objects


is

• class_name array_name [size] ;


#include<iostream.h>
#include<conio.h>
class employee
{
int id;
char name[30];
public:
void getdata()
{
cout << "Enter Id : ";
cin >> id;
cout << "Enter Name : ";
cin >> name;
}
void putdata()
{
cout << id << " \n";
cout << name << " \n";
}
};
void main()
{
employee emp[30];
int n, i;

cout << "Enter Number of Employees - ";


cin >> n;

for(i = 0; i < n; i++)

emp[i].getdata();

cout << "Employee Data -\n " ;

for(i = 0; i < n; i++)


emp[i].putdata();
getch();
}

You might also like