Student.cpp
1 #include <iostream> 2 3 using namespace std; 4 5 class Student { 6 private: 7 int id; 8 string name; 9 int numSubjects; 10 float *marks; 11 12 public: 13 14 Student (int id = 0, string name = "", int numSubjects = 0) 15 { 16 this->id = id; 17 this->name = name; 18 this->numSubjects = numSubjects; 19 }; 20 21 void accept() { 22 cout << "\nEnter Student ID: "; 23 cin >> id; 24 cin.ignore(); 25 cout << "Enter Student Name: "; 26 getline(cin, name); 27 cout << "Enter Number of Subjects: "; 28 cin >> numSubjects; 29 30 marks = new float[numSubjects]; 31 for (int i = 0; i < numSubjects; i++) { 32 cout << "Enter marks for Subject " << i + 1 << ": "; 33 cin >> marks[i]; 34 } 35 } 36 37 void display() { 38 cout << "Student ID: " << id << endl; 39 cout << "Student Name: " << name << endl; 40 cout << "Number of Subjects: " << numSubjects << endl; 41 cout << "Marks:" << endl; 42 for (int i = 0; i < numSubjects; i++) { 43 cout << "Subject " << i + 1 << ": " << marks[i] << endl; 44 } 45 cout << "\n"; 46 } 47 48 ~Student() { 49 delete[] marks; 50 } 51 }; 52 53 int main() { 54 int n; 55 cout << "Enter number of students: "; 56 cin >> n; 57 58 Student *students = new Student[n]; 59 60 for (int i = 0; i < n; i++) { 61 cout << "\nEnter details for Student " << i + 1 << ":" << endl; 62 students[i].accept(); 63 } 64 65 cout << "\nStudent Mark Lists:\n"; 66 for (int i = 0; i < n; i++) { 67 students[i].display(); 68 } 69 70 delete[] students; 71 return 0; 72 }