Untitled
unknown
plain_text
a year ago
1.8 kB
12
Indexable
#include <iostream>
#include <cstring>
class Student {
public:
int id;
char name[50];
Student(int studentId, const char* studentName) {
id = studentId;
strcpy(name, studentName);
}
};
class Course {
private:
Student* enrolledStudents[10]; // Maximum 10 students
int maxCapacity;
int currentEnrollment;
public:
Course(int capacity) : maxCapacity(capacity), currentEnrollment(0) {
for (int i = 0; i < 10; i++) {
enrolledStudents[i] = nullptr;
}
}
bool enrollStudent(Student* student) {
if (currentEnrollment < maxCapacity) {
enrolledStudents[currentEnrollment++] = student;
std::cout << student->name << " has been enrolled in the course.\n";
return true;
} else {
std::cout << "Course is full. Cannot enroll " << student->name << ".\n";
return false;
}
}
bool withdrawStudent(int studentId) {
for (int i = 0; i < currentEnrollment; i++) {
if (enrolledStudents[i]->id == studentId) {
std::cout << enrolledStudents[i]->name << " has been withdrawn from the course.\n";
enrolledStudents[i] = enrolledStudents[--currentEnrollment]; // Replace with last student
return true;
}
}
std::cout << "Student not found in this course.\n";
return false;
}
};
int main() {
Student s1(1, "Alice");
Student s2(2, "Bob");
Course course(2); // Course capacity of 2
course.enrollStudent(&s1);
course.enrollStudent(&s2);
course.enrollStudent(&s1); // Attempt to enroll again (should fail)
course.withdrawStudent(1); // Withdraw Alice
course.withdrawStudent(3); // Attempt to withdraw non-existent student
return 0;
}Editor is loading...
Leave a Comment