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 5
class Student
{
int rollno;
string name;
float marks;
static int count;
public:
Student()
{
count++;
}
void setStudent();
void getStudent();
};
// Initializing static data member
int Student::count = 0;
// Function to accept student details
void 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 details
void 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 records
for (int i = 0; i < N; i++)
{
cout << "\nStudent " << i + 1 << endl;
s[i].setStudent();
}
// Displaying student records
for (int i = 0; i < N; i++)
{
s[i].getStudent();
}
return 0;
}

Output:

Student 1
Enter Student Details:
Enter Roll Number: 101
Enter Name: Amit
Enter Marks: 85

Student 2
Enter Student Details:
Enter Roll Number: 102
Enter Name: Ravi
Enter Marks: 78

Student 3
Enter Student Details:
Enter Roll Number: 103
Enter Name: Neha
Enter Marks: 92

Student 4
Enter Student Details:
Enter Roll Number: 104
Enter Name: Pooja
Enter Marks: 88

Student 5
Enter Student Details:
Enter Roll Number: 105
Enter Name: Mohan
Enter Marks: 75

Student Details:
Roll Number = 101
Name = Amit
Marks = 85
Total Objects Created = 5

Student Details:
Roll Number = 102
Name = Ravi
Marks = 78
Total Objects Created = 5

Student Details:
Roll Number = 103
Name = Neha
Marks = 92
Total Objects Created = 5

Student Details:
Roll Number = 104
Name = Pooja
Marks = 88
Total Objects Created = 5

Student Details:
Roll Number = 105
Name = Mohan
Marks = 75
Total Objects Created = 5

Explanation (व्याख्या)

1. Macro Definition

#define N 5

The #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

Popular posts from this blog

Important Questions of C++ including OOP's

Programming Methodologies and OOP's using C++ Language

OOP's using CPP Syllabus AND Programming Methodologies and Data Structures Using C++ (NEP 2020)