CPP Program for Store the data of ten students and display the sorted data
/* Problem Statement :Write a Student class and use it in your program. Store the data of ten students and display the sorted data according to their roll numbers, date of births, and total */
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
class Date {
int day, month, year;
public:
void get_date() {
cout << "\nEnter the date as DAY, MONTH, YEAR" << endl;
cout << "Day:";
cin >> day;
cout << "Month:";
cin >> month;
cout << "Year:";
cin >> year;
}
void display() {
cout << "\nDate of birth is " << endl;
cout << "Day : " << day << "\tMonth : " << month << "\tYear : " << year << endl;
}
};
class Student {
public:
int roll_no;
Date dob;
float total_marks;
string name;
void get_data() {
cout << "\nEnter the details of student :" << endl;
cout << "\nEnter the Name : ";
cin >> name;
cout << "\nEnter Roll No : ";
cin >> roll_no;
cout << "\nEnter Total Marks : ";
cin >> total_marks;
dob.get_date();
}
void display() {
cout << "\nThe student details are as follow :" << endl;
cout << "\nName : " << name << "\tRoll No. : " << roll_no << "\tTotal marks : " << total_marks << endl;
dob.display();
cout << "***************************************************" << endl;
}
};
void sortByRoll(Student *students, int n) {
for (int i = 0; i < n - 1; i++) {
for (int j = i + 1; j < n; j++) {
if (students[i].roll_no > students[j].roll_no) {
swap(students[i], students[j]);
}
}
}
}
int main() {
int n;
cout << "\nEnter the number of students: ";
cin >> n;
Student *students = new Student[n];
for (int i = 0; i < n; i++) {
students[i].get_data();
}
cout << "***************************************************" << endl;
for (int i = 0; i < n; i++) {
students[i].display();
}
cout << "\nThe sorted data by roll number is: ";
sortByRoll(students, n);
for (int i = 0; i < n; i++) {
students[i].display();
}
delete[] students;
return 0;
}
This C++ code defines two classes: Date and Student, and utilizes them to create and sort an array of student objects based on their roll numbers.
Date Class:
- It has three private data members: day, month, and year.
- It has two member functions: get_date() to input date details from the user and display() to display the date.
Student Class:
- It has public data members for roll_no, dob (of type Date), total_marks, and name.
- It has member functions get_data() to input student details from the user and display() to display student details.
sortByRoll Function:
This function takes an array of Student objects and sorts them based on their roll_no. It uses a simple nested loop to compare roll_no of each student with every other student's roll_no and swaps them if necessary.
Main Function:
It starts by asking the user for the number of students (n). Dynamically allocates an array of Student objects of size n. Then, it iterates through each student, calls get_data() to input their details. After inputting all details, it displays the student details before sorting. It then calls sortByRoll() to sort the students by their roll numbers. Finally, it displays the sorted list of students.
Key Points:
- The code uses dynamic memory allocation (new[]) to create an array of Student objects.
- After usage, it deallocates the memory using delete[] to avoid memory leaks.
- It demonstrates basic usage of classes, member functions, dynamic memory allocation, and sorting algorithms in C++.
Sign up here with your email
ConversionConversion EmoticonEmoticon