C++ Program to Load and Store Records of N Students N विद्यार्थियों के रिकॉर्ड को इनपुट एवं स्टोर करने का C++ प्रोग्राम
In C++, an array of objects can be used to store records of multiple students. Each object represents one student and contains information such as Roll Number, Name, and Marks.
By creating an array of objects, we can accept and display the records of multiple students using loops.
C++ में Multiple Students के Records को Store करने के लिए Array of Objects का उपयोग किया जा सकता है। प्रत्येक Object एक Student को दर्शाता है तथा इसमें Roll Number, Name और Marks जैसी जानकारियाँ Store की जाती हैं।
Objects का Array बनाकर Loops की सहायता से अनेक विद्यार्थियों के Records को Input तथा Display किया जा सकता है।
Program:
#include <iostream>#include <string>using namespace std;#define N 5class Student{int rollno;string name;float marks;static int count;public:Student(){count++;}void setStudent();void getStudent();};// Initializing static data memberint Student::count = 0;// Function to accept student detailsvoid Student::setStudent(){cout << "Enter Student Details:" << endl;cout << "Enter Roll Number: ";cin >> rollno;cout << "Enter Name: ";cin >> name;cout << "Enter Marks: ";cin >> marks;}// Function to display student detailsvoid Student::getStudent(){cout << "\nStudent Details:" << endl;cout << "Roll Number = " << rollno << endl;cout << "Name = " << name << endl;cout << "Marks = " << marks << endl;cout << "Total Objects Created = " << count << endl;}int main(){Student s[N];// Accepting student recordsfor (int i = 0; i < N; i++){cout << "\nStudent " << i + 1 << endl;s[i].setStudent();}// Displaying student recordsfor (int i = 0; i < N; i++){s[i].getStudent();}return 0;}
Output:
Student 1Enter Student Details:Enter Roll Number: 101Enter Name: AmitEnter Marks: 85Student 2Enter Student Details:Enter Roll Number: 102Enter Name: RaviEnter Marks: 78Student 3Enter Student Details:Enter Roll Number: 103Enter Name: NehaEnter Marks: 92Student 4Enter Student Details:Enter Roll Number: 104Enter Name: PoojaEnter Marks: 88Student 5Enter Student Details:Enter Roll Number: 105Enter Name: MohanEnter Marks: 75Student Details:Roll Number = 101Name = AmitMarks = 85Total Objects Created = 5Student Details:Roll Number = 102Name = RaviMarks = 78Total Objects Created = 5Student Details:Roll Number = 103Name = NehaMarks = 92Total Objects Created = 5Student Details:Roll Number = 104Name = PoojaMarks = 88Total Objects Created = 5Student Details:Roll Number = 105Name = MohanMarks = 75Total Objects Created = 5
Explanation (व्याख्या)
1. Macro Definition
#define N 5The #define directive defines a macro named N with the value 5. It specifies that the program will store records of five students.
#define Directive की सहायता से N नाम का Macro बनाया गया है, जिसका मान 5 है। इसका अर्थ है कि प्रोग्राम पाँच विद्यार्थियों के Records Store करेगा।
2. Class Declaration
class Student{int rollno;string name;float marks;};
The Student class contains three data members: rollno, name, and marks. These variables store the Roll Number, Name, and Marks of each student.
Student Class में तीन Data Members rollno, name तथा marks हैं। ये प्रत्येक विद्यार्थी का Roll Number, Name तथा Marks Store करते हैं।
3. Static Data Member
static int count;The static data member count is shared by all objects of the class. It maintains the total number of objects created.
Static Data Member count Class के सभी Objects के लिए Common होता है। यह Create किए गए Objects की कुल संख्या को Maintain करता है।
4. Constructor
Student(){count++;}
The constructor is called automatically whenever an object is created. It increases the value of count by one.
जब भी कोई Object Create होता है, Constructor अपने आप Call होता है। यह count के मान को एक से बढ़ा देता है।
5. setStudent() Function
This member function accepts the Roll Number, Name, and Marks of a student from the user.
यह Member Function User से विद्यार्थी का Roll Number, Name तथा Marks Input करता है।
6. getStudent() Function
This member function displays the stored information of a student.
यह Member Function विद्यार्थी की Store की गई जानकारी को Display करता है।
7. Array of Objects
Student s[N];This statement creates an array of five objects of the Student class. Each object stores the record of one student.
यह Statement Student Class के पाँच Objects का Array बनाता है। प्रत्येक Object एक विद्यार्थी का Record Store करता है।
8. Using Loops
The first for loop calls setStudent() for all five objects to accept student records. The second for loop calls getStudent() to display all stored records.
पहला for Loop सभी पाँच Objects के लिए setStudent() Function Call करके Student Records Input करता है। दूसरा for Loop getStudent() Function Call करके सभी Store किए गए Records को Display करता है।
An array of objects allows us to store and manage multiple student records using a single class. Constructors, member functions, static data members, and loops make record management systematic and convenient.
Array of Objects की सहायता से एक ही Class का उपयोग करके अनेक विद्यार्थियों के Records को Store तथा Manage किया जा सकता है। Constructors, Member Functions, Static Data Members तथा Loops इस कार्य को व्यवस्थित और आसान बनाते हैं।
Comments
Post a Comment