第六周。

This commit is contained in:
unlockable
2023-04-01 14:14:04 +08:00
parent 8ddf7735d1
commit 96af0a6839
2 changed files with 260 additions and 0 deletions

43
OOP/06/Exercise01.cpp Normal file
View File

@@ -0,0 +1,43 @@
#include <iostream>
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;
}

217
OOP/06/Exercise02.cpp Normal file
View File

@@ -0,0 +1,217 @@
#include <iostream>
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;
}