第七周。

This commit is contained in:
unlockable
2023-04-11 18:47:05 +08:00
parent b82efce6f4
commit 4343508f54
2 changed files with 271 additions and 0 deletions

49
OOP/07/Exercise01.cpp Normal file
View File

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

222
OOP/07/Exercise02.cpp Normal file
View File

@@ -0,0 +1,222 @@
#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 : 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;
}