array of object examples in C++

This program based on array of object examples of C++ , If the concept of your array of object is clear, then you can easily understand it.

As mentioned earlier, in the array of object, each element of the array is an object, and the object is a collection of data members and function members.

As given in the diagram below, the object of class “student” is declared as array of object, so here different object (obj [0], obj [1], obj [2], obj [3 ], obj [4]) will create. Which will store 5 student records which means that one element will be related to one student.

an array of object syntax is given below,

syntax

class-name array-variable[size];

for example

student obj[4];

Let’s try this example here,

#include<iostream> 
using namespace std;
class student
{
    char name[20],fname[20];
public:
    void getdata(void);  
    void putdata(void);
}

void student::getdata() 
{ 
   cout<<"Enter the student Rno: "; 
   cin>>no; 
   cout<<"Enter the student name: "; 
   gets(name); cout<<"Enter the student Fname: "; 
   gets(fname);
}
void student::putdata() 
 { 
   cout<<"===============================n";
   cout<<"Rollno : ";  cout<<no<<endl;
   cout<<"Name : ";    puts(name); 
   cout<<"FName : ";   puts(fname); 
   cout<<"Age : "<<ag<<endl; 
   cout<<"===============================n"; 
}

int main()
{
 student obj[2]; // for 2 students
 obj[0].getdata(); // 1st student
 obj[0].getdata(); // 2nd student

 obj[1].putdata(); // 1st student
 obj[1].putdata(); // 2nd student
 return 0;
}

The above program is given in detail below, in which more tasks have been given –

#include<iostream>
#include<stdio>
#include<stdlib>
using namespace std;
  class student
  {
   // member by default are private
    char name[20],fname[20],sub[5][20],grade;
    int no,age,av,ag, mark[5];
    public:
// member declared as public
       void getdata(void);     // to get student record from user
       void putdata(void);   // for displaying student record
       void calculate();   //  calculate result like percentage and division     
       void result(void); // displaying result
       void sub_marks(void);   // storing subject marks
       int retno(void);    // return a specific student roll no
       void sub_name(void);    // store subject name
       void class_re(void);     // display class result
  };
void student::getdata()
{
    cout<<"Enter the student Rno: ";
    cin>>no; 
    cout<<"Enter the student name: ";
    gets(name);
    cout<<"Enter the student Fname: ";
    gets(fname);
    cout<<"Enter the student Age: ";
    cin>>ag;
    sub_name();
}
int student::retno()
{
    return (no);
}
void student::putdata()
{
   cout<<"===============================n";
   cout<<"Rollno : ";cout<<no<<endl;
   cout<<"Name : ";puts(name);
   cout<<"FName : ";puts(fname);
   cout<<"Age : "<<ag<<endl;
   cout<<"===============================n";
}
void student::sub_marks()
{
   cout<<"Enter the subject marks according to subjectn";
   cout<<"++++++++++++++++++++++++++++++++++++++++++++n";
   for(int i=0;i<5;i++)
   {
     cin>>mark[i];
   }
   calculate();
}
void student::result()
{
   cout<<"Student Rollno: "<<no<<"n";
   cout<<"tSno.ttNamettMarksn";
   cout<<"=============================================n";
   for(int i=0;i<5;i++) 
   {
     cout<<"\t"<<i;
     cout<<"\t\t"<<sub[i];
     cout<<"\t\t"<<mark[i];
     cout<<endl;
   }
   cout<<"==============================================nn";
   cout<<"______________________________________________n";
   cout<<" ";
    calculate();
    cout<<"n";
   
}
void student::class_re()
{
   cout<<"+++++++++++++++++++++++++++++++++++++++++++++++++++n";
   cout<<"Rollno: "<<no<<"tName: "<<name<<"n";
   cout<<"--------------------------------n";
   calculate();
   cout<<"n";
   cout<<"___________________________________________________n";
   cout<<"%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%n";
}
void student::sub_name()
{ 
   cout<<"nEnter the subject namen";
   cout<<"++++++++++++++++++++++n";
   for(int i=0;i<5;i++)
   {
      cout<<"subject: "<<i+1<<" = ";
      gets(sub[i]);
    }
   sub_marks();
}
void student::calculate()
{
   int sum=0;
   for(int i=0;i<5;i++)
   {
      sum=sum+mark[i];
   }
   av=sum/5;
   {
     cout<<"TOTAL: "<<sum<<"t"<<"PERCENTAGE: "<<av<<"%"<<"t";
     {
        if(av>=60)
         {
             cout<<"FIRSTn";
          }
          else if(av>=45)
          {
             cout<<"SECONDn";
          }
          else if(av>=33)
          {
             cout<<"THIRDn";
           }
      }
  }
}
int main()
{
   
   int x,s;  
    student ar[10]; // array of object
  do{ 
         start:
         cout<<"nnnnn";
         int j=0;
         while(j<80)
         {
             cout<<"%";
             j++;
         }
          cout<<"/t/t/t/t***OPTION MENU***n";
          cout<<"/n/t/t/t/t1.EDITn";
          cout<<"/n/t/t/t/t2.RESULTn";
          cout<<"/n/t/t/t/t3.PERSONAL INFORMATIONn";
          cout<<"/n/t/t/t/t4.SEARCH RECORDn";
          cout<<"/n/t/t/t/t5.CLASS RESULTn";
          cout<<"/n/t/t/t/t0.EXITn";
          int l=0;
          while(l<80)
          {
             cout<<"%";
             l++;
          }
           cout<<"/n/n/n/n/t/t/t/tselect a option(EDIT,first): ";
           cout<<"t/t/t/t/t/t/t";
           cin>>s;
           switch(s)
           {
            case 1:
                         cout<<"How many student's record you want to store: ";
                         cin>>x;
                         for(int i=0;i<x;i++)
                         {
                             
                             cout<<i+1<<". STUDENTn";
                             ar[i].getdata();
                             cout<<"nn";
                             cout<<"record created ";
                         }
                         break;
          case 3:
                       cout<<"display the student informationn";
                       for(int j=0;j<x;j++)
                       {
                          if(ar[j].retno()==0)
                               goto start;
                           cout<<j+1<<" STUDENTn";
                           ar[j].putdata();
                           
                           }
                           break;
            case 2:
                         cout<<"DISPLAY THE RESULTn";
                         for(int k=0;k<x;k++)
                         {
                             if(ar[k].retno()==0)goto start;
                              cout<<k<<".";
                              ar[k].result();
                              cout<<"%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%n";
                           }
                          
                            break;
           case 4:
                        cout<<"SEARCHn";
                         int found, true=0;
                         cout<<"Enter the Rollno to be search: ";
                         cin>>found;
                         for(int l=0;l<x;l++)
                         {
                           if(ar[l].retno()==found)
                           {
                               true=1;
                              cout<<"PERSONAL INFORMATIONn";
                             ar[l].putdata();
                              cout<<"%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%n";
                              cout<<"RESULT INFORMATIONn";
                              ar[l].result();;goto start;}
                            }
                          if(!true){
                           cout<<"record not found";
                          
                           goto start; 
                         }
                        break;
             case 5:
                           for(int a=0;a<x;a++)
                          {
                             if(ar[a].retno()==0)goto start;
                             ar[a].class_re();
                              
                           }
                          
                           break;
             case 0:   exit(0);
                           break;
              default: cout<<"/t/t/t/t/t/t/tInvalid choicen";
                          
                          goto start;
           }
   }while(s!=0);
return 0;
}

OUTPUT

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
            ***OPTION MENU***
                  1.EDIT
                  2.RESULT
                  3.PERSONAL INFORMATION
                  4.SEARCH RECORD
                  5.CLASS RESULT
                  0.EXIT
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

Explanation

Because, file handling is not used in the program, after the program terminates, it will remove the information given by the user. However during execution we can perform many tasks.

For example, in an array of object, we declare an object as an array type of object, that is, the higher the size of an array, the more objects of the class will be created, such as,

student obj[4];

It has 5 objects created so it can store 5 student records. But during the execution we can store less than 5 students in it and at the same time we do not have to call the same function multiple times to store 5 student records. like-

obj[index-value].getdata();

where index-value automatically increase

This will create 4 object which will store the record of different 4 student (in the program 10 objects will be declared according to student obj [10] but we have used only 4 object here so remove this memory wastage use dynamic memory allocation).

As the index-value of the array changes during execution, the function getdata() calls that which will take input from the end -user.

for(int i = 0; i<student; i++)
  {
    obj[i].getdata();
  }

A record of a new student will be stored as soon as the value of variable i increases. Other functions of will be call in same manner-

obj [i]. putdata(); 
obj [i] .result ();

will also be called.

what happned when if array of object will no be used here

student x, b, z;

But by doing this, we will be able to store only the records of 3 students ( each object store each student’s record) and additionally the extra statement will increase in the program. That is, to store each student record, we have to call the same function multiple times with different object. such as,

x.getdata(); // for 1st student
y.getdata(); // for 2nd student
z.getdata(); // for 3rd student

when this program will be execute, it will be mandatory for us to store 3 students record, i.e. during execution we cannot store less than 3 students whereas this does not happen when we use array of object.

Did you know

The class is the updated version of the structure, so the concept of the array-of-object will perform the same with the structure.

If the values of all the elements in the array-of-objects are not taken, then there will be memory loss that would not be considered good programming practice. So to overcome this problem, dynamic memory allocation will be used . C++ dynamic memory allocation in array.

Here is another example of an array of object, Banking Management System

student management system

Related Projects in C++ are:

Like it?

Leave a Reply

Your email address will not be published. Required fields are marked *

Exit mobile version