From 96af0a68393af0b56e08fdc9a4c61f4b497a0c1e Mon Sep 17 00:00:00 2001 From: unlockable Date: Sat, 1 Apr 2023 14:14:04 +0800 Subject: [PATCH] =?UTF-8?q?=E7=AC=AC=E5=85=AD=E5=91=A8=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- OOP/06/Exercise01.cpp | 43 +++++++++ OOP/06/Exercise02.cpp | 217 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 260 insertions(+) create mode 100644 OOP/06/Exercise01.cpp create mode 100644 OOP/06/Exercise02.cpp diff --git a/OOP/06/Exercise01.cpp b/OOP/06/Exercise01.cpp new file mode 100644 index 0000000..abd9f9e --- /dev/null +++ b/OOP/06/Exercise01.cpp @@ -0,0 +1,43 @@ +#include + +class Base { +private: + int b_number; + +public: + Base() { + } + Base(int i) : b_number(i) { + } + int get_number() { + return b_number; + } + void print() { + std::cout << b_number << std::endl; + } +}; + +class Derived : public Base { +// class Derived : private Base { +// class Derived : protected Base { +private: + int d_number; + +public: + Derived(int i, int j) : Base(i), d_number(j){}; + void print() { + std::cout << get_number() << " "; + std::cout << d_number << std::endl; + } +}; + +int main() { + Base a(2); + Derived b(3, 4); + std::cout << "a is "; + std::cout << "b is "; + b.print(); + std::cout << "base part of b is "; + b.Base::print(); + return 0; +} \ No newline at end of file diff --git a/OOP/06/Exercise02.cpp b/OOP/06/Exercise02.cpp new file mode 100644 index 0000000..fa243a1 --- /dev/null +++ b/OOP/06/Exercise02.cpp @@ -0,0 +1,217 @@ +#include + +class People { +protected: + int number; + std::string name; + std::string sex; + +public: + People(int _number = 0, std::string _name = "Undefined", + std::string _sex = "Undefined") { + this->number = _number; + this->name = _name; + this->sex = _sex; + } + void displayNewProperties(){}; + void inputNewProperties(){}; + void display() { + this->displayProperty("Number", this->number); + this->displayProperty("Name", this->name); + this->displayProperty("Sex", this->sex); + } + void inputInfo() { + this->askForNewValue("Number", this->number); + this->askForNewValue("Name", this->name); + this->askForNewValue("Sex", this->sex); + } + + void askForNewValue(std::string valueName, int &value) { + std::cout << valueName << " (Original: " << value << "): "; + std::cin >> value; + } + + void askForNewValue(std::string valueName, std::string &value) { + std::cout << valueName << " (Original: " << value << "): "; + std::cin >> value; + } + + void displayProperty(std::string propName, const int &value) { + std::cout << propName << ": " << value << std::endl; + } + + void displayProperty(std::string propName, const std::string &value) { + std::cout << propName << ": " << value << std::endl; + } +}; + +class Student : public People { +protected: + std::string classNo; + +public: + Student(int _number = 0, std::string _name = "Undefined", + std::string _sex = "Undefined", std::string _classNo = "Undefined") + : People(_number, _name, _sex), classNo(_classNo){}; + void displayNewProperties() { + this->displayProperty("Class No.", this->classNo); + } + void inputNewProperties() { + this->askForNewValue("Class No.", this->classNo); + } + void display() { + People::display(); + this->displayNewProperties(); + } + void inputInfo() { + People::inputInfo(); + this->inputNewProperties(); + } +}; + +class Teacher : public People { +protected: + std::string principalship; + std::string department; + +public: + Teacher(int _number = 0, std::string _name = "Undefined", + std::string _sex = "Undefined", + std::string _principalship = "Undefined", + std::string _department = "Undefined") + : People(_number, _name, _sex), principalship(_principalship), + department(_department){}; + void displayNewProperties() { + this->displayProperty("Principalship", this->principalship); + this->displayProperty("Department", this->department); + } + void inputNewProperties() { + this->askForNewValue("Principalship", this->principalship); + this->askForNewValue("Department", this->department); + } + void display() { + People::display(); + this->displayNewProperties(); + } + void inputInfo() { + People::inputInfo(); + this->inputNewProperties(); + } +}; + +class Graduate : public Student { +protected: + std::string subject; + Teacher advisor; + +public: + Graduate(int _number = 0, std::string _name = "Undefined", + std::string _sex = "Undefined", std::string _subject = "Undefined", + int _advisorNumber = 0, std::string _advisorName = "Undefined", + std::string _advisorSex = "Undefined", + std::string _advisorPrincipalship = "Undefined", + std::string _advisorDepartment = "Undefined") + : Student(_number, _name, _sex), subject(_subject), + advisor(_advisorNumber, _advisorName, _advisorSex, + _advisorPrincipalship, _advisorDepartment){}; + void displayNewProperties() { + this->displayProperty("Subject", this->subject); + std::cout << "\n***** Advisor: *****" << std::endl; + this->advisor.display(); + } + void inputNewProperties() { + this->askForNewValue("Subject", this->subject); + std::cout << "\n***** Advisor: *****" << std::endl; + this->advisor.inputInfo(); + } + void display() { + Student::display(); + this->displayNewProperties(); + } + void inputInfo() { + Student::inputInfo(); + this->inputNewProperties(); + } +}; + +class TeacherAssistant : public Graduate, public Teacher { +protected: + std::string RA; + +public: + TeacherAssistant(int _number = 0, std::string _name = "Undefined", + std::string _sex = "Undefined", + std::string _subject = "Undefined", int _advisorNumber = 0, + std::string _advisorName = "Undefined", + std::string _advisorSex = "Undefined", + std::string _advisorPrincipalship = "Undefined", + std::string _advisorDepartment = "Undefined", + std::string _principalship = "Undefined", + std::string _department = "Undefined", + std::string _RA = "Undefined") + : Graduate(_number, _name, _sex, _subject, _advisorNumber, _advisorName, + _advisorSex, _advisorPrincipalship, _advisorDepartment), + Teacher(_number, _name, _sex, _principalship, _department), RA(_RA){}; + void displayNewProperties() { + Graduate::displayProperty("RA", this->RA); + } + void inputNewProperties() { + Graduate::askForNewValue("RA", this->RA); + } + void display() { + Teacher::display(); + Graduate::displayNewProperties(); + this->displayNewProperties(); + } + void inputInfo() { + Teacher::inputInfo(); + Graduate::inputNewProperties(); + this->inputNewProperties(); + } +}; + +int main() { + Student stu(102, "SomeStuName", "Male", "SomeClassNo"); + Teacher tea(192, "SomeTeacherName", "Female", "Lecturer", "SomeDepartment"); + Graduate grad; + TeacherAssistant TA( + 12, "Some TA", "Female", "Sleeping", 293, "SomeOtherAdvisor", "Male", + "Professor", "SomeDepartment", "Teacher Assistant", "Sleep", "SomeRA"); + + std::cout << "\n\n####### Student ########" << std::endl; + stu.display(); + + std::cout << "\n\n######## Teacher ########" << std::endl; + tea.display(); + + std::cout << "\n\n######## Graduate ########" << std::endl; + grad.display(); + + std::cout << "\n\n######## TeacherAssistant ########" << std::endl; + TA.display(); + + std::cout << "\n\n######## Input for stu ########" << std::endl; + stu.inputInfo(); + + std::cout << "\n\n######## Input for tea ########" << std::endl; + tea.inputInfo(); + + std::cout << "\n\n######## Input for grad ########" << std::endl; + grad.inputInfo(); + + std::cout << "\n\n######## Input for TA ########" << std::endl; + TA.inputInfo(); + + std::cout << "\n\n####### Student ########" << std::endl; + stu.display(); + + std::cout << "\n\n######## Teacher ########" << std::endl; + tea.display(); + + std::cout << "\n\n######## Graduate ########" << std::endl; + grad.display(); + + std::cout << "\n\n######## TeacherAssistant ########" << std::endl; + TA.display(); + return 0; +} \ No newline at end of file