store student record using array of object

In the program, array of object is used. 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. so,

student-management-cpp

Here, the class object is declared as an array-of-object, so different multiple object (obj[0], obj[1], obj[2], obj[3], obj[4]). Will store 5 student records

In the given program, first of all, the student record will be stored by edit-option and then other options will be used.

Here is an example,

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>

  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 subject\n";
   cout<<"++++++++++++++++++++++++++++++++++++++++++++\n";
   for(int i=0;i<5;i++)
   {
     cin>>mark[i];
   }
   calculate();
}

void student::result()
{
   cout<<"Student Rollno: "<<no<<"\n";
   cout<<"\tSno.\t\tName\t\tMarks\n";
   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<<"==============================================\n\n";
   cout<<"______________________________________________\n";
   cout<<" ";
    calculate();cout<<"\n";
   getch();
}

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 name\n";
   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<<"FIRST\n";
          }
          else if(av>=45)
          {
             cout<<"SECOND\n";
          }
          else if(av>=33)
          {
             cout<<"THIRD\n";
           }
      }
  }
}

void main()
{
   clrscr();
   int x,s;  
    student ar[10]; // array of object
  do{ 
         start:
         cout<<"\n\n\n\n\n";
         int j=0;
         while(j<80)
         {
             cout<<"%";
             j++;
         }
          cout<<"\t\t\t\t***OPTION MENU***\n";
          cout<<"\n\t\t\t\t1.EDIT\n";
          cout<<"\n\t\t\t\t2.RESULT\n";
          cout<<"\n\t\t\t\t3.PERSONAL INFORMATION\n";
          cout<<"\n\t\t\t\t4.SEARCH RECORD\n";
          cout<<"\n\t\t\t\t5.CLASS RESULT\n";
          cout<<"\n\t\t\t\t0.EXIT\n";
          int l=0;
          while(l<80)
          {
             cout<<"%";
             l++;
          }
           cout<<"\n\n\n\n\t\t\t\t\t\tselect a option(EDIT,first): ";
           cout<<"\t\t\t\t\t\t\t\t";
           cin>>s;
           switch(s)
           {
            case 1:clrscr();
                         cout<<"How many student's record you wil enter: ";
                         cin>>x;
                         for(int i=0;i<x;i++)
                         {
                             clrscr();
                             cout<<i+1<<". STUDENT\n";
                             ar[i].getdata();
                             cout<<"\n\n";
                             cout<<"record created ";getch();clrscr();
                         }
                         break;
          case 3:clrscr();
                       cout<<"display the student information\n";
                       for(int j=0;j<x;j++)
                       {
                          if(ar[j].retno()==0)
                               goto start;
                           cout<<j+1<<" STUDENT\n";
                           ar[j].putdata();
                           getch();
                           }
                           clrscr();
                            break;
            case 2:clrscr();
                         cout<<"DISPLAY THE RESULT\n";
                         for(int k=0;k<x;k++)
                         {
                             if(ar[k].retno()==0)goto start;
                              cout<<k<<".";
                              ar[k].result();
                              cout<<"%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n";

                           }
                           clrscr();
                            break;
           case 4:clrscr();
                        cout<<"SEARCH\n";
                         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 INFORMATION\n";
                             ar[l].putdata();
                              cout<<"%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n";
                              cout<<"RESULT INFORMATION\n";
                              ar[l].result();getch();clrscr();goto start;}
                            }
                          if(!true){
                           cout<<"record not found";
                           getch();
                          clrscr();
                           goto start; 
                         }
                        break;
             case 5:clrscr();
                           for(int a=0;a<x;a++)
                          {
                             if(ar[a].retno()==0)goto start;
                             ar[a].class_re();
                              getch();
                           }
                           clrscr();
                           break;
             case 0:   exit(0);
                           break;
              default: cout<<"\t\t\t\t\t\t\t\tInvalid choice\n";
                          getch();
                          clrscr();
                          goto start;
           }
   }while(s!=0);
getch();
}
OUTPUT:-
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
            ***OPTION MENU***

                  1.EDIT

                  2.RESULT
               
                  3.PERSONAL INFORMATION
         
                  4.SEARCH RECORD
             
                  5.CLASS RESULT
 
                  0.EXIT

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

explanation-

Because, in the program, file handling is not used so after the program terminates, it will remove all the details inputted by the user. In a way we can manipulate the input data only during execution.

If you want, you can also create this program without an array of object such as,

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. such as,

x.getdata (); // for first student
y.getdata () // for second student
z.getdata () // for third 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 in an array of object, we make an object an array type. It depends on the object of number array size. The greater the size of the array, the more objects will be created. such as,

student obj [5];

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

Here is the example,

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

The more students records stored, the more times the getdata() function will be called with different object.

  The value change of variable i means that a new object is used, not create because we have already created 5 object (obj[5]).
And as already mentioned, we can use less than 5 objects during execution, meaning less than 5 student records can be stored.

So other functions such as,

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

will also be called.

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