diff --git a/OOP/02/Exercise01.cpp b/OOP/02/Exercise01.cpp new file mode 100644 index 0000000..55e09df --- /dev/null +++ b/OOP/02/Exercise01.cpp @@ -0,0 +1,40 @@ +#include + +class Student { +private: + int ID, grade; + +public: + Student() { + this->ID = 0; + this->grade = 0; + }; + + Student(int newID, int newGrade) : ID(newID), grade(newGrade){}; + + void display(); + + void set(int, int); +}; + +void Student::display() { + std::cout << "ID: " << this->ID << ", Grade: " << this->grade << std::endl; +} + +void Student::set(int newID, int newGrade) { + this->ID = newID; + this->grade = newGrade; +} + +int main() { + Student stu[5]; + stu[0].set(21394,89); + stu[1].set(2394888, 100); + stu[2].set(299, 70); + stu[3].set(3999, 10); + stu[4].set(4999, 60); + stu[0].display(); + stu[2].display(); + stu[4].display(); + return 0; +} \ No newline at end of file diff --git a/OOP/02/Optional02.cpp b/OOP/02/Optional02.cpp new file mode 100644 index 0000000..0e5d374 --- /dev/null +++ b/OOP/02/Optional02.cpp @@ -0,0 +1,47 @@ +#include +#include + +class Strings { +public: + Strings(char *s); + ~Strings(); + void Print(); + void Set(char *s); + +private: + int length; + char *str; +}; + +Strings::Strings(char *s) { + length = strlen(s); + str = (char *)malloc(length + 1); + strcpy(str, s); +} + +Strings::~Strings() { + free(str); +} + +void Strings::Print() { + std::cout << str << std::endl; +} + +void Strings::Set(char *s) { + free(str); + length = strlen(s); + str = (char *)malloc(length + 1); + strcpy(str, s); +} + +int main() { + Strings* str1 = new Strings("didig"); + Strings* str2 = new Strings("dirir"); + + str1->Print(); + str2->Print(); + str1->Set("ifirk"); + str1->Print(); + + return 0; +} \ No newline at end of file diff --git a/OOP/EmployeeAdmin/employee.h b/OOP/EmployeeAdmin/employee.h new file mode 100644 index 0000000..5305538 --- /dev/null +++ b/OOP/EmployeeAdmin/employee.h @@ -0,0 +1,15 @@ +class Employee { +private: + int individualEmpNo; + int grade; + int accumPay; + +public: + Employee(); + Employee(int no, int newGrade, int newAccumPay); + ~Employee(); + void printInfo(); + void setEmpNo(int); + void setGrade(int); + void setAccumPay(int); +}; \ No newline at end of file diff --git a/OOP/EmployeeAdmin/main.cpp b/OOP/EmployeeAdmin/main.cpp new file mode 100644 index 0000000..b10488d --- /dev/null +++ b/OOP/EmployeeAdmin/main.cpp @@ -0,0 +1,50 @@ +#include +#include "employee.h" + +Employee::Employee() { + this->individualEmpNo = 0; + this->grade = 0; + this->accumPay = 0; +} + +Employee::Employee(int no, int newGrade, int newAccumPay) { + this->individualEmpNo = no; + this->grade = newGrade; + this->accumPay = newAccumPay; +} + +Employee::~Employee() { + std::cout << "欢迎使用,再见" << std::endl; +} + +void Employee::printInfo() { + std::cout << "Employee No. " << this->individualEmpNo << ": grade " << this->grade << ", accumPay: " << this->accumPay << std::endl; +} + +void Employee::setEmpNo(int newNo) { + this->individualEmpNo = newNo; +} + +void Employee::setGrade(int newGrade) { + this->grade = newGrade; +} + +void Employee::setAccumPay(int newAccumPay) { + this->accumPay = newAccumPay; +} + +int main() { + Employee* staff[4]; + staff[0] = new Employee(); + staff[1] = new Employee(11343, 2, 2000); + staff[2] = new Employee(199, 1, 4000); + staff[3] = new Employee(59949, 4, 400); + + for (int i = 0; i < 4; i++) { + staff[i]->printInfo(); + } + + delete staff[3]; + + return 0; +} \ No newline at end of file