eラーニングプラットフォーム
CoddyのC++ジャーニー「オブジェクト指向プログラミング」セクションの一部。レッスン 103/104。
チャレンジ
簡単このコースを通して習得したすべての OOP 概念を組み合わせた E-Learning Platform を構築しましょう。courses、さまざまな種類の lessons を管理し、student の進捗を追跡する system を作成します。inheritance hierarchy、polymorphism、smart pointer、design pattern が調和して機能する様子を示します。
コードを 6 つのファイルに分けて整理します。
User.h:baseUserclass と 2 つの derived class、StudentとInstructorによる user hierarchy を作成します。base
Userは name と ID を持ち、user type を string として返す virtualgetRole()method と、"[name] ([id]) - [role]"を返すgetInfo()method を持つ必要があります。Studentclass は、enroll している course ID(string の vector を使用)と、completed lesson の数を追跡します。enrollInCourse(const std::string& courseId)、count を増加させるcompletedLesson()、およびgetCompletedCount()method を追加します。Instructorclass は specialty の string を保持し、teach する course ID を追跡します。addCourse(const std::string& courseId)とgetSpecialty()を追加します。Lesson.h:abstract base class を使用して lesson hierarchy を構築します。Lessonbase class は title と duration(分単位)を持ち、completion message を返す pure virtualcomplete()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 を所有するCourseclass を作成します。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 methodcreateLesson(const std::string& type, const std::string& title, int duration, const std::string& extra)を持つLessonFactoryclass を作成します。typeparameter によって、作成する lesson("video"、"quiz"、または"text")を決定します。extraparameter には type 固有の data が含まれます。video の場合は URL、quiz の場合は question count(int として parse)、text の場合は word count(int として parse)です。Platform.h:すべてを調整する中心的なPlatformclass を構築します。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 を読み取ります。
- Platform name
- Course details(形式:
title,courseId,instructorId) - Student details(形式:
name,studentId) - 追加する 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 が 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: 1input が 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 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;
}
オブジェクト指向プログラミングのすべてのレッスン
3コンストラクタとデストラクタ
デフォルトコンストラクタパラメータ付きコンストラクタコピーコンストラクタムーブコンストラクタコンストラクタ初期化リスト委譲コンストラクタデストラクタ徹底解説Rule of Three / Five / Zero復習 - Stringクラス6演算子オーバーロード
演算子オーバーロード入門算術演算子のオーバーロード比較演算子のオーバーロードストリーム演算子代入演算子のオーバーロード[] と () の演算子オーバーロード型変換演算子復習 - Matrixクラス自分で練習してみよう: C++オンラインコンパイラ