C ++ का यह program, array of object पर आधारित है, इसलिए आपको पहले array of object को समझना होगा –
जैसा कि पहले उल्लेख किया गया है, array of object में, array का प्रत्येक element एक object होता है, और object , data -member और function -member का एक set होता है।
syntax
class-name array-variable[size];
उदाहरण के लिए ,
student obj[4];
जैसे कि नीचे diagram में दिया गया है यहाँ class “student ” के object को array of object के रूप में declare किया गया है, इसलिए यहाँ different object (obj [0], obj [1], obj [2], obj [3], obj [4]) create होंगे। जो 5 student record को store करेगा जिसका अर्थ है कि एक element, एक student से सम्बंधित होगा-
इसका एक सरल program नीचे दिया गया है –
#include<iostream.h>
#include<conio.h>
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";
}
void 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
getch();
}
ऊपर दिए गए program को नीचे विस्तार रूप में दिया गया है जिसमे ज्यादा task दिए गए हैं –
#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 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";
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 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";
}
}
}
}
void main()
{
clrscr();
int x,s;
student ar[10]; // array of object
do{
start:
cout<<"nnnnn";
int j=0;
while(j<80)
{
cout<<"%";
j++;
}
cout<<"tttt***OPTION MENU***n";
cout<<"ntttt1.EDITn";
cout<<"ntttt2.RESULTn";
cout<<"ntttt3.PERSONAL INFORMATIONn";
cout<<"ntttt4.SEARCH RECORDn";
cout<<"ntttt5.CLASS RESULTn";
cout<<"ntttt0.EXITn";
int l=0;
while(l<80)
{
cout<<"%";
l++;
}
cout<<"nnnnttttttselect a option(EDIT,first): ";
cout<<"tttttttt";
cin>>s;
switch(s)
{
case 1:clrscr();
cout<<"How many student's record you want to store: ";
cin>>x;
for(int i=0;i<x;i++)
{
clrscr();
cout<<i+1<<". STUDENTn";
ar[i].getdata();
cout<<"nn";
cout<<"record created ";getch();clrscr();
}
break;
case 3:clrscr();
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();
getch();
}
clrscr();
break;
case 2:clrscr();
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";
}
clrscr();
break;
case 4:clrscr();
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();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<<"ttttttttInvalid choicen";
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
क्योंकि program में, file handling का उपयोग नहीं किया है इसलिए program terminate होने के बाद, यह user दी गयी information को remove कर देगा। हालाँकि execution के दौरान हम कई task perform कर सकते हैं।
जैसे कि array of object में हम एक object को एक array type of object के रूप में declare करते हैं अर्थात एक array का size जितना ज्यादा होगा उतने class के उतने ही object create होंगे जैसे कि,
student obj[4];
यह 4 object create करेगा जो different student के record को store करेगा (program में student obj [10] के अनुसार 10 object declare होंगे लेकिन हमने यहाँ पर केवल 4 object को ही use किया है dynamic memory location से इस memory wastage को दूर किया जा सकता है) ।
execution के दौरान जैसे ही array की index -value change होगी function getdata() call होगा जो end -user से information के लिए कहेगा।
obj[index-value].getdata();
जैसे-
for(int i = 0; i<student; i++)
{
obj[i].getdata();
}
जैसे ही variable i की value increase होती है, एक नए छात्र का record store किया जाएगा। ऐसे ही class के अन्य function member call होंगे-
obj [i]. putdata();
obj [i] .result ();
जो किसी different task को perform करेंगे-will also be called.
What Happen If Array Of Object Will Not Be Used Here?
हमें 3 student के लिए 3 object declare करना होगा। ऐसा करने से हम केवल 3 ही student के record को store कर सकते हैं और साथ ही हरेक object को same function के साथ call करना पड़ता जैसे –
student x, b, z;
अर्थात,
x.getdata(); // for 1st student
y.getdata(); // for 2nd student
z.getdata(); // for 3rd student
ऐसे ही सभी function को तीनो objects के साथ एक एक करके call करना पड़ता।
Did you know
class , structure का एक modified version है इसलिए यह array of object को पूरी तरह से support करता है।
यहाँ array of object के अन्य उदाहरण दिया गया है।
student management system
Related Projects in C++ are:
- Bank Management system in C++
- Print a student mark-sheet in C++
- Billing Management System in C++
- Hotel Management System in C++