Untitled

 avatar
unknown
plain_text
a year ago
3.4 kB
25
Indexable
Course.h




#ifndef COURSE_H
#define COURSE_H

#include <string>
#include <vector>

class Student;

class Course {
private:
    std::string name;
    int minStudents;
    int maxStudents;
    std::vector<Student*> enrolledStudents;

public:
    Course(const std::string& name, int minStudents, int maxStudents);
    ~Course();

    std::string getName() const;
    int getMinStudents() const;
    int getMaxStudents() const;
    bool addStudent(Student* student);
    void displayEnrolledStudents() const;
    bool isAvailable() const;
};

#endif






Course.cpp




#include "Course.h"
#include "Student.h"
#include <iostream>

Course::Course(const std::string& name, int minStudents, int maxStudents)
    : name(name), minStudents(minStudents), maxStudents(maxStudents) {}

Course::~Course() {}

std::string Course::getName() const {
    return name;
}

int Course::getMinStudents() const {
    return minStudents;
}

int Course::getMaxStudents() const {
    return maxStudents;
}

bool Course::addStudent(Student* student) {
    if (enrolledStudents.size() < maxStudents) {
        enrolledStudents.push_back(student);
        return true;
    }
    return false;
}

void Course::displayEnrolledStudents() const {
    std::cout << "Enrolled students in " << name << ":" << std::endl;
    for (const auto& student : enrolledStudents) {
        std::cout << student->getName() << std::endl;
    }
}

bool Course::isAvailable() const {
    return enrolledStudents.size() < maxStudents;
}






Student.h




#ifndef STUDENT_H
#define STUDENT_H

#include <string>
#include <vector>

class Course; // Forward declaration

class Student {
private:
    std::string name;
    std::vector<Course*> registeredCourses;

public:
    Student(const std::string& name);
    ~Student();

    std::string getName() const;
    bool registerForCourse(Course* course);
    void displayRegisteredCourses() const;
};

#endif // STUDENT_H







Student.cpp




#include "Student.h"
#include "Course.h"
#include <iostream>

Student::Student(const std::string& name)
    : name(name) {}

Student::~Student() {}

std::string Student::getName() const {
    return name;
}

bool Student::registerForCourse(Course* course) {
    if (registeredCourses.size() < 5) {
        registeredCourses.push_back(course);
        return true;
    }
    return false;
}

void Student::displayRegisteredCourses() const {
    std::cout << "Registered courses for " << name << ":" << std::endl;
    for (const auto& course : registeredCourses) {
        std::cout << course->getName() << std::endl;
    }
}





Coursemain.cpp




#include <iostream>
#include "Course.h"
#include "Student.h"

int main() {
    // Create courses
    Course math("Math", 5, 30);
    Course physics("Physics", 5, 25);

    // Create students
    Student alice("Alice");
    Student bob("Bob");

    // Register students for courses
    alice.registerForCourse(&math);
    alice.registerForCourse(&physics);
    bob.registerForCourse(&math);

    // Display courses available
    std::cout << "Available courses:" << std::endl;
    if (math.isAvailable()) {
        std::cout << math.getName() << std::endl;
    }
    if (physics.isAvailable()) {
        std::cout << physics.getName() << std::endl;
    }

    // Display registered courses for students
    alice.displayRegisteredCourses();
    bob.displayRegisteredCourses();

    return 0;
}
Editor is loading...
Leave a Comment