Menu
Coddy logo textTech

eラーニングプラットフォーム

CoddyのC++ジャーニー「オブジェクト指向プログラミング」セクションの一部。レッスン 103/104。

challenge icon

チャレンジ

簡単

このコースを通して習得したすべての OOP 概念を組み合わせた E-Learning Platform を構築しましょう。courses、さまざまな種類の lessons を管理し、student の進捗を追跡する system を作成します。inheritance hierarchy、polymorphism、smart pointer、design pattern が調和して機能する様子を示します。

コードを 6 つのファイルに分けて整理します。

  • User.h:base User class と 2 つの derived class、StudentInstructor による user hierarchy を作成します。

    base User は name と ID を持ち、user type を string として返す virtual getRole() method と、"[name] ([id]) - [role]" を返す getInfo() method を持つ必要があります。

    Student class は、enroll している course ID(string の vector を使用)と、completed lesson の数を追跡します。enrollInCourse(const std::string& courseId)、count を増加させる completedLesson()、および getCompletedCount() method を追加します。

    Instructor class は specialty の string を保持し、teach する 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 を持つ必要があります。

    3 つの derived class を作成します。

    • VideoLesson:video URL を持ちます。その complete()"Watched video: [title]" を返します
    • QuizLesson:question の数を持ちます。その complete()"Completed quiz: [title] ([questions] questions)" を返します
    • TextLesson:word count を持ちます。その complete()"Read article: [title]" を返します

    それぞれで getType() を override し、順に "Video""Quiz""Text" を返すようにします。

  • Course.h:smart pointer を使用して lesson を所有する Course class を作成します。

    course は title、ID、instructor ID を持ちます。course が lesson を排他的に所有するため、lesson は 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 固有の data が含まれます。video の場合は URL、quiz の場合は question count(int として parse)、text の場合は word count(int として parse)です。

  • Platform.h:すべてを調整する中心的な 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 を実演します。

    4 つの input を読み取ります。

    1. Platform name
    2. Course details(形式:title,courseId,instructorId
    3. Student details(形式:name,studentId
    4. 追加する lesson(形式: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]" として出力します。

たとえば、input が CodeAcademyC++ Mastery,CPP101,I001Alice,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

input が LearnHubPython Basics,PY100,I002Bob,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 creation が柔軟になり、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: コースと学生を作成し、それらをプラットフォームに追加する
    
    // TODO: LessonFactoryを使用してレッスンを作成し、コースに追加する
    
    // TODO: コースのカリキュラムを表示する
    
    // TODO: 学生をコースに登録する
    
    // TODO: 学生の最初のレッスン(インデックス 0)を完了する
    
    // TODO: Print the student's info and completed lesson count
    // Format: "Completed lessons: [count]"
    
    return 0;
}

オブジェクト指向プログラミングのすべてのレッスン

自分で練習してみよう: C++オンラインコンパイラ