From 4343508f54421a9d1e21e92e14d560ec92210579 Mon Sep 17 00:00:00 2001 From: unlockable Date: Tue, 11 Apr 2023 18:47:05 +0800 Subject: [PATCH] =?UTF-8?q?=E7=AC=AC=E4=B8=83=E5=91=A8=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- OOP/07/Exercise01.cpp | 49 ++++++++++ OOP/07/Exercise02.cpp | 222 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 271 insertions(+) create mode 100644 OOP/07/Exercise01.cpp create mode 100644 OOP/07/Exercise02.cpp diff --git a/OOP/07/Exercise01.cpp b/OOP/07/Exercise01.cpp new file mode 100644 index 0000000..2e23965 --- /dev/null +++ b/OOP/07/Exercise01.cpp @@ -0,0 +1,49 @@ +#include +using namespace std; + +class B1 { +public: + B1(int i) { + cout << "constructing B1 " << i << endl; + } + ~B1() { + cout << "destructing B1 " << endl; + } +}; + +class B2 { +public: + static int count; + int selfCount; + +public: + B2() { + cout << "constructing B2 " << endl; + count++; + this->selfCount = count; + } + + ~B2() { + cout << "destructing B2, self count: " << this->selfCount << endl; + } +}; + +class C : public B2, virtual public B1 { +public: + int j; + B1 memberB1; + B2 memberB2; + +public: + C(int a, int b, int c) : B1(a), memberB1(b), j(c) { + } + ~C() { + cout << "destructing c" << endl; + } +}; + +int B2::count = 0; +int main() { + C obj(1, 2, 3); + cout << obj.selfCount << endl << obj.memberB2.selfCount << endl; +} \ No newline at end of file diff --git a/OOP/07/Exercise02.cpp b/OOP/07/Exercise02.cpp new file mode 100644 index 0000000..17b4ae0 --- /dev/null +++ b/OOP/07/Exercise02.cpp @@ -0,0 +1,222 @@ +#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 : virtual 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 : virtual 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 : virtual 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") + : People(_number, _name, _sex), + Graduate(_number, _name, _sex, _subject, _advisorNumber, _advisorName, + _advisorSex, _advisorPrincipalship, _advisorDepartment), + Teacher(_number, _name, _sex, _principalship, _department), RA(_RA){}; + void displayNewProperties() { + displayProperty("RA", this->RA); + } + void inputNewProperties() { + askForNewValue("RA", this->RA); + } + void display() { + People::display(); + Teacher::displayNewProperties(); + Student::displayNewProperties(); + Graduate::displayNewProperties(); + this->displayNewProperties(); + } + void inputInfo() { + People::inputInfo(); + Teacher::inputNewProperties(); + Student::inputNewProperties(); + 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