C++ array of objects

C++ arrays of objects

We know that an array can be any data type including struct. similar we can also have arrays of variables that are of the types class. such variable are called array of objects consider the following class definition

The identifier employee is user defined data type and can be used to create objects that related to different categories of the employee Example

class student
{
char name[20];
float marks;
public:
void getdata();
void putdata();
};

student school[4]; //array of school
student graduate[8]; //array of graduate
student postgraduate[15]; //array of postgraduate

The array school contains four objects, namely, school[0],school[1],school[2],school[3] of type student class. similarly the graduate array contains 8 objects and postpraduate array contains 15 objects.

since an array of objects behaves like any other array, we can use the usual array accessing method to access individual elements, and then the dot member operator to access the member functions.

for example The statement school[i].putdata();

will display the data of the ith position element of the array manager. That is this statement requests the object school[i] to invoke the member function putdata()

An array of objects is stored inside the memory in the same way as multi dimensional array. The array school is represented in below figure.
Note that only the space for data items of the objects is created . Member functions are stored separately and will be used by all the objects

objects array

#include <iostream>
using namespace std;
class student
{
char name[20]; // string as class member
float marks;
public:
void getdata();
void putdata();
};
void student:: getdata()
{
std::cout <<"Enter name:";
std::cin >>name;
std::cout <<"Enter Marks:";
std::cin >>marks;
}
void student::putdata()
{
std::cout <<"Name:"<<name<<"\n";
std::cout <<"marks:"<<marks<<"\n";
}
const int size=3;
int main()
{
int i;
student school[size];
for(int i=0;i<size;i++)
{
std::cout<<"\n Details of school"<<i+1<<"\n";
school[i].getdata();
}
std::cout<<"\n";
for(i=0;i < size;i++)
{
std::cout <<"\n school"<<i+1 <<"\n";
school[i].putdata();
}
}

Input:
Details of school 1
Enter name:nitish
Enter Marks:56
Details of school2
Enter name:Gaurav
Enter Marks:78
Details of school3
Enter name:chetan
Enter Marks:99

program output
school1
Name:nitish
Marks:56


school2
Name:gaurav
Marks:78


school3
Name:chetan
Marks:99

  • C++ Dynamic Initialization of Objects
  • C++ Copy Constructor
  • C++ Dynamic Constructor
  • C++ Destructors
  • C++ Exercise
  • C++ Operator Overloading
  • C++ Overloading Unary Operators
  • Const pointer in C
  • Void pointer in c
  • C++ Overloading Binary Operators
  • C++ Overloading Binary Operators Using Friends
  • C++ Manipulation String Using Operators
  • C++ Rules for overloading operators
  • C++ Exercise
  • C++ Basic To class Type
  • C++ Class TO Basic Type
  • C++ One class To another class type
  • C++ Exercise
  • C++ Inheritance introduction
  • C++ Single Inheritance
  • C++ Multiple Inheritance
  • C++ Ambiguity Resolution in inheritance
  • C++ Hierarchical Inheritance
  • C++ Hybrid Inheritance
  • C++ Virtual Base Classes
  • C++ Exercise
  • C++ abstract class
  • C++ nesting of classes
  • C++ Exercise
  • C++ polymorphism
  • C++ Exercise
  • C++ pointers
  • C++ Pointers TO object
  • C++ this pointer
  • C++ Pointer to Derived class
  • C++ Virtual functions
  • C++ Exercise
  • C++ streams
  • C++ unformatted I/O operations
  • C++ Put() and get()
  • C++ getline() and write()
  • C++ Formatted console I/O
  • C++ Manipulators
  • C++ Exercise
  • C++ file handling
  • C++ file stream classes
  • C++ Open and closing file
  • C++ open using constructor
  • C++ open using open()
  • C++ Detecting End of file
  • C++ File modes
  • C++ File pointers and Manipulators
  • C++ Sequential I/O
  • C++ Reading and Writing
  • C++ Updating a File
  • C++ Error handling In File
  • C++ Command Line Arguments
  • C++ Exercise
  • C++ Template introduction
  • C++ Class Templates with multiple Parameters
  • C++ Function templates
  • C++ Function templates with multiple parameters
  • C++ member function Template
  • C++ Exercise
  • C++ Exception handling
  • C++ Basics of Exception Handling
  • C++ Exception Handling Mechanism
  • C++ Throwing Mechanism
  • C++ Catch Mechanism
  • C++ Catch all Exceptions
  • C++ Re-Throwing An Exception
  • C++ Specifying Exceptions
  • C++ Exercise