第八周。

This commit is contained in:
unlockable
2023-04-11 18:47:15 +08:00
parent 4343508f54
commit 30ae2dbc8c
3 changed files with 365 additions and 0 deletions

225
OOP/08/Exercise01.cpp Normal file
View File

@@ -0,0 +1,225 @@
#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();
}
virtual void pay() {
std::cout << "The pay is 1000" << std::endl;
}
};
class Prof : public Teacher {
public:
Prof(int _number = 0, std::string _name = "Undefined",
std::string _sex = "Undefined",
std::string _principalship = "Undefined",
std::string _department = "Undefined")
: People(_number, _name, _sex),
Teacher(_number, _name, "Female", _principalship, _department){};
void pay() {
std::cout << "The pay is 1100" << std::endl;
}
};
class FemaleProf : public Prof {
public:
FemaleProf(int _number = 0, std::string _name = "Undefined",
std::string _sex = "Female",
std::string _principalship = "Undefined",
std::string _department = "Undefined")
: People(_number, _name, "Female"),
Prof(_number, _name, "Female", _principalship, _department){};
void pay() {
std::cout << "The pay is 1200" << std::endl;
}
};
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() {
Teacher aTeacher(1, "Some Teacher", "Male", "Lecturer", "No Department");
Prof aProf(2, "Some Prof", "Male", "Sleeping", "Sleep");
FemaleProf aFemaleProf(3, "Some FemaleProf", "Female", "Sleeping2",
"Sleep2");
std::cout << "######## Teacher ########" << std::endl;
Teacher *t = &aTeacher;
t->display();
t->pay();
std::cout << "######## Prof ########" << std::endl;
t = &aProf;
t->display();
t->pay();
std::cout << "######## Female Prof ########" << std::endl;
t = &aFemaleProf;
t->display();
t->pay();
return 0;
}

33
OOP/08/Exercise02.cpp Normal file
View File

@@ -0,0 +1,33 @@
#include <iostream>
class Mammal {
public:
virtual void speak() {
std::cout << "I'm a mammal." << std::endl;
}
};
class Dog : public Mammal {
public:
virtual void speak() {
std::cout << "I'm a dog." << std::endl;
}
};
class Cat : public Mammal {
public:
virtual void speak() {
std::cout << "Nya!" << std::endl;
}
};
int main() {
Mammal* mammals[3];
mammals[0] = new Mammal();
mammals[1] = new Dog();
mammals[2] = new Cat();
mammals[0]->speak();
mammals[1]->speak();
mammals[2]->speak();
return 0;
}

107
OOP/08/Optional01.cpp Normal file
View File

@@ -0,0 +1,107 @@
#include <iostream>
class Shape {
public:
Shape(){};
~Shape(){};
virtual float getArea() = 0;
virtual float getPerim() = 0;
virtual std::istream &operator>>(std::istream &input) = 0;
};
class Rectangle : public Shape {
private:
float length, height;
public:
Rectangle(float _length = 0, float _height = 0)
: length(_length), height(_height){};
~Rectangle(){};
virtual float getArea() {
return this->length * this->height;
}
virtual float getPerim() {
return (this->length + this->height) * 2;
}
virtual std::istream &operator>>(std::istream &input) {
std::cout << "Length: ";
std::cin >> this->length;
std::cout << "Height: ";
std::cin >> this->height;
return input;
}
};
class Circle : public Shape {
private:
float radius;
public:
Circle(float _radius = 0) : radius(_radius){};
~Circle(){};
virtual float getArea() {
return 3.14 * this->radius * this->radius;
}
virtual float getPerim() {
return 2 * 3.14 * this->radius;
}
virtual std::istream &operator>>(std::istream &input) {
std::cout << "Radius: ";
std::cin >> this->radius;
return input;
}
};
inline std::istream &operator>>(std::istream &input, Shape &thisShape) {
return thisShape >> input;
}
int main() {
Shape *shapes[2];
{
std::string shapeType;
do {
std::cout << "The type of shape[0] ((r)ectangle, (c)ircle): ";
std::cin >> shapeType;
if (shapeType == "r") {
shapes[0] = new Rectangle();
break;
}
else if (shapeType == "c") {
shapes[0] = new Circle();
break;
}
else {
std::cout << "Unknown name. Try again." << std::endl;
}
} while (true);
std::cin >> *shapes[0];
do {
std::cout << "The type of shape[1] ((r)ectangle, (c)ircle): ";
std::cin >> shapeType;
if (shapeType == "r") {
shapes[1] = new Rectangle();
break;
}
else if (shapeType == "c") {
shapes[1] = new Circle();
break;
}
else {
std::cout << "Unknown name. Try again." << std::endl;
}
} while (true);
std::cin >> *shapes[1];
}
std::cout << "##### Shapes[0] #####" << std::endl;
std::cout << "Area = " << shapes[0]->getArea()
<< ", Perim = " << shapes[0]->getPerim() << std::endl;
std::cout << "##### Shapes[1] #####" << std::endl;
std::cout << "Area = " << shapes[1]->getArea()
<< ", Perim = " << shapes[1]->getPerim() << std::endl;
return 0;
}