-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharrofobj.cpp
More file actions
76 lines (62 loc) · 2 KB
/
arrofobj.cpp
File metadata and controls
76 lines (62 loc) · 2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include <iostream>
using namespace std;
class Student {
string name, studentClass, section;
string* subjects;
char* grades;
int numSubjects;
public:
// Constructor to initialize the student details
Student() {
cout << "Enter student's name: ";
cin >> name;
cout << "Enter class: ";
cin >> studentClass;
cout << "Enter section: ";
cin >> section;
cout << "Enter number of subjects: ";
cin >> numSubjects;
// Dynamically allocate memory for subjects and grades
subjects = new string[numSubjects];
grades = new char[numSubjects];
for (int i = 0; i < numSubjects; ++i) {
cout << "Enter subject " << i + 1 << " name: ";
cin >> subjects[i];
cout << "Enter grade for " << subjects[i] << ": ";
cin >> grades[i];
}
}
// Function to display the student details
void displayDetails() {
cout << "\nStudent Details:\n";
cout << "Name: " << name << endl;
cout << "Class: " << studentClass << endl;
cout << "Section: " << section << endl;
cout << "\nSubjects and Grades:\n";
for (int i = 0; i < numSubjects; ++i) {
cout << "Subject: " << subjects[i] << " | Grade: " << grades[i] << endl;
}
}
// Destructor
~Student() {
// Free dynamically allocated memory
delete[] subjects;
delete[] grades;
cout << "Student object destroyed.\n";
}
};
int main() {
int numStudents;
cout << "Enter the number of students: ";
cin >> numStudents;
// Create an array of Student objects
Student* students = new Student[numStudents];
// Display details of each student
for (int i = 0; i < numStudents; ++i) {
cout << "\nEnter details for student " << i + 1 << ":\n";
students[i].displayDetails();
}
// Free the array of objects
delete[] students;
return 0;
}