0% found this document useful (0 votes)
681 views2 pages

Array of Objects in C++

1) An array of objects is created by declaring an array of a class type, where each element of the array is an object of that class. 2) For example, an array of MyClass objects called obs is declared with a size of 4, and each obs element has its x value set using the setX method. 3) To input name and marks for multiple students, an array of Student objects can be declared to hold each student's data, with methods to get input and display output for each student object.

Uploaded by

virat rehani
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
681 views2 pages

Array of Objects in C++

1) An array of objects is created by declaring an array of a class type, where each element of the array is an object of that class. 2) For example, an array of MyClass objects called obs is declared with a size of 4, and each obs element has its x value set using the setX method. 3) To input name and marks for multiple students, an array of Student objects can be declared to hold each student's data, with methods to get input and display output for each student object.

Uploaded by

virat rehani
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 2

Array of Objects in c++

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.

Syntax:
class_name array_name [size] ;

Example:
#include <iostream> MyClass obs[4];
class MyClass { int i;
int x; for(i=0; i < 4; i++)
public: obs[i].setX(i);
void setX(int i) { x = i; } for(i=0; i < 4; i++)
int getX() { return x; } cout << "obs[" << i << "].getX(): " <<
}; obs[i].getX() << "\n";
void main() getch();
{ }

Suppose we have 50 students in a class and we have to input the name and marks of all the 50 students.
Then creating 50 different objects and then inputting the name and marks of all those 50 students is not
a good option. In that case, we will create an array of objects as we do in case of other data-types.

Let's see an example of taking the input of name and marks of 5 students by creating an array of the
objects of students.
#include <iostream> cin >> marks;
#include <string> }
void displayInfo()
using namespace std; {
class Student cout << "Name : " << name << endl;
{ cout << "Marks : " << marks << endl;
string name; }
int marks; };
public:
void getName()
{
getline( cin, name );
}
void getMarks()
{
int main()
{
Student st[5];
for( int i=0; i<5; i++ )
{
cout << "Student " << i + 1 <<
endl;
cout << "Enter name" << endl;
st[i].getName();
cout << "Enter marks" << endl;
st[i].getMarks();
}

for( int i=0; i<5; i++ )


{
cout << "Student " << i + 1 <<
endl;
st[i].displayInfo();
}
return 0;
}

You might also like