Menu
Coddy logo textTech

이러닝 플랫폼

Coddy C++ 여정의 객체 지향 프로그래밍 섹션에 포함된 레슨. 104개 중 103번째.

challenge icon

챌린지

쉬움

이제 이 과정에서 지금까지 익힌 모든 OOP 개념을 한데 모은 E-Learning Platform을 만들어 보겠습니다. courses, 다양한 lesson 유형을 관리하고 학생의 진행 상황을 추적하는 시스템을 만들게 됩니다. 이 시스템은 inheritance hierarchy, polymorphism, smart pointer, 그리고 design pattern이 조화롭게 작동하는 모습을 보여 줍니다.

코드를 다음 여섯 개의 파일로 구성합니다.

  • User.h: base User class와 두 derived class인 StudentInstructor를 사용해 user hierarchy를 만듭니다.

    base User에는 name과 ID가 있어야 하며, user type을 string으로 반환하는 virtual getRole() method와 "[name] ([id]) - [role]"을 반환하는 getInfo() method를 포함해야 합니다.

    Student class는 enrolled course ID(문자열 vector 사용)와 completed lesson 수를 추적합니다. enrollInCourse(const std::string& courseId), count를 증가시키는 completedLesson(), 그리고 getCompletedCount() method를 추가합니다.

    Instructor class는 specialty string을 저장하고 자신이 가르치는 course ID를 추적합니다. addCourse(const std::string& courseId)getSpecialty()를 추가합니다.

  • Lesson.h: abstract base class를 사용해 lesson hierarchy를 구축합니다.

    Lesson base class에는 title과 duration(분 단위)이 있어야 하며, completion message를 반환하는 pure virtual complete() method와 lesson type을 반환하는 getType() method를 포함해야 합니다.

    세 개의 derived class를 만듭니다.

    • VideoLesson: video URL을 가집니다. complete()"Watched video: [title]"을 반환합니다.
    • QuizLesson: question 수를 가집니다. complete()"Completed quiz: [title] ([questions] questions)"를 반환합니다.
    • TextLesson: word count를 가집니다. complete()"Read article: [title]"을 반환합니다.

    각 class는 getType()을 override하여 각각 "Video", "Quiz", "Text"를 반환해야 합니다.

  • Course.h: smart pointer를 사용해 자신의 lessons를 소유하는 Course class를 만듭니다.

    course에는 title, ID, instructor ID가 있습니다. course가 lessons를 독점적으로 소유하므로 lessons는 std::vector<std::unique_ptr<Lesson>>으로 저장합니다.

    다음 method를 추가합니다.

    • addLesson(std::unique_ptr<Lesson> lesson): lesson의 ownership을 course로 이전합니다.
    • getLessonCount(): lesson 수를 반환합니다.
    • getLesson(size_t index): 해당 index의 lesson에 대한 pointer를 반환합니다(유효하지 않으면 nullptr).
    • displayCurriculum(): 각 lesson의 type과 title을 "[index]. [Type]: [title]" 형식으로 출력합니다.
  • LessonFactory.h: 서로 다른 lesson type을 생성하는 Factory pattern을 구현합니다.

    std::unique_ptr<Lesson>을 반환하는 static method createLesson(const std::string& type, const std::string& title, int duration, const std::string& extra)를 가진 LessonFactory class를 만듭니다.

    type parameter가 생성할 lesson을 결정합니다("video", "quiz", 또는 "text"). extra parameter에는 type별 데이터가 포함됩니다. video에는 URL, quiz에는 question 수(int로 parsing), text에는 word count(int로 parsing)가 들어갑니다.

  • Platform.h: 모든 요소를 조정하는 central Platform class를 구축합니다.

    courses는 std::vector<std::shared_ptr<Course>>로, users는 std::vector<std::shared_ptr<User>>로 저장합니다.

    다음을 구현합니다.

    • addCourse(std::shared_ptr<Course> course)
    • addUser(std::shared_ptr<User> user)
    • findCourse(const std::string& id): shared_ptr 또는 nullptr를 반환합니다.
    • findUser(const std::string& id): shared_ptr 또는 nullptr를 반환합니다.
    • enrollStudent(const std::string& studentId, const std::string& courseId): 둘 다 찾고, student를 enroll한 뒤 "Enrolled [name] in [course title]" 또는 "Enrollment failed"를 출력합니다.
    • completeLesson(const std::string& studentId, const std::string& courseId, size_t lessonIndex): student와 course를 찾고 lesson의 complete() method를 호출한 다음, student의 completed count를 증가시키고 completion message 또는 "Could not complete lesson"을 출력합니다.
  • main.cpp: 모든 요소를 결합하여 platform을 시연합니다.

    네 개의 입력을 읽습니다.

    1. Platform name
    2. Course details (format: title,courseId,instructorId)
    3. Student details (format: name,studentId)
    4. 추가할 lesson (format: type,title,duration,extra)

    platform을 만들고 "Welcome to [platform name]!"을 출력합니다. course와 student를 만들고 platform에 추가합니다. LessonFactory를 사용해 lesson을 만들고 course에 추가합니다. course curriculum을 표시합니다. student를 course에 enroll합니다. 해당 student의 첫 번째 lesson(index 0)을 complete합니다. 마지막으로 student의 info와 completed lesson count를 "Completed lessons: [count]" 형식으로 출력합니다.

예를 들어 입력이 CodeAcademy, C++ Mastery,CPP101,I001, Alice,S001, video,Introduction to OOP,30,https://example.com/oop인 경우:

Welcome to CodeAcademy!
1. Video: Introduction to OOP
Enrolled Alice in C++ Mastery
Watched video: Introduction to OOP
Alice (S001) - Student
Completed lessons: 1

입력이 LearnHub, Python Basics,PY100,I002, Bob,S002, quiz,Variables Quiz,15,10인 경우:

Welcome to LearnHub!
1. Quiz: Variables Quiz
Enrolled Bob in Python Basics
Completed quiz: Variables Quiz (10 questions)
Bob (S002) - Student
Completed lessons: 1

이 challenge는 실제 e-learning platform이 어떻게 구성되는지 보여 줍니다. users, content, 그리고 이들을 조정하는 platform을 명확히 분리합니다. Factory pattern은 lesson 생성을 유연하게 만들고, smart pointer는 안전한 memory management를 보장하며, polymorphism을 사용하면 각 lesson type이 고유한 방식으로 동작하면서도 모든 lesson type을 일관되게 다룰 수 있습니다.

직접 해보기

#include <iostream>
#include <string>
#include <sstream>
#include <memory>
#include "User.h"
#include "Lesson.h"
#include "Course.h"
#include "LessonFactory.h"
#include "Platform.h"

int main() {
    // 입력 읽기
    std::string platformName;
    std::getline(std::cin, platformName);
    
    std::string courseDetails; // 형식: title,courseId,instructorId
    std::getline(std::cin, courseDetails);
    
    std::string studentDetails; // 형식: name,studentId
    std::getline(std::cin, studentDetails);
    
    std::string lessonDetails; // 형식: type,title,duration,extra
    std::getline(std::cin, lessonDetails);
    
    // TODO: courseDetails를 파싱하여 title, courseId, instructorId 추출
    
    // TODO: studentDetails를 파싱하여 name, studentId 추출
    
    // TODO: lessonDetails를 파싱하여 type, title, duration, extra 추출
    
    // TODO: 플랫폼을 생성하고 "Welcome to [platform name]!" 표시
    
    // TODO: course와 student를 생성하여 platform에 추가
    
    // TODO: LessonFactory를 사용하여 lesson을 생성하고 course에 추가
    
    // TODO: course 커리큘럼 표시
    
    // TODO: student를 course에 등록
    
    // TODO: 학생을 위해 첫 번째 레슨(인덱스 0)을 완료하세요
    
    // TODO: Print the student's info and completed lesson count
    // Format: "Completed lessons: [count]"
    
    return 0;
}

객체 지향 프로그래밍의 모든 레슨

직접 연습해 보세요: 온라인 C++ 컴파일러