array of objectsa class is a template which can


Array of Objects

A class is a template, which can contain data items as well as member functions to operate on the data items. Various objects of the class can also be declared and used. Also, an array of objects can be declared and used just like an array of any other data type. An example will show the use of array of objects.

e.g.

                class student

                 {

                  public :

                                                void getdetails();

                                                void printdetails();

                  private :

                                                int rollno;

                                                char name[25];

                                                int marks[6];

                                                float percent;

                 };

                void student :: getdetails()

                 {

                  int ctr,total; 

                                                cout << "enter rollno";

                                cin >> rollno ;

                                cout << "enter name";

                                cin >> name;

                                cout << " enter 6 marks " ;

                                for( ctr = 1 ;ctr <= 6 ; ctr++ )

                                 {

                                  cin >> marks[ctr];

                                  total = total + marks[ctr];

                      }

 

                                percent = total / 6;

                 }

 

                void student :: printdetails ()

                 {

                                cout << rollno << name << percent ;

                 }

 

                void main()

                 {

                  student records[50];

                  int x=0;

                                cout << " How many students ";

                                cin >> x;

                                for ( int i =1; i<= x; i++)

                                 {

                                                records[i].getdeatils(); 

                                 }

                                for ( int i =1; i<= x; i++)

                                 {

                                                records[i].printdeatils();              

                                 }

 

                }

As can be seen above, an array of objects is declared just like any other array. Members of the class are accessed, using the array name qualified by a subscript.

 

Request for Solution File

Ask an Expert for Answer!!
C/C++ Programming: array of objectsa class is a template which can
Reference No:- TGS0309367

Expected delivery within 24 Hours