Files
BasicsOfComputerSoftwareEng…/OOP/05/Exercise01.cpp
2023-03-24 08:51:40 +08:00

104 lines
2.6 KiB
C++

#include <iostream>
enum sexType { male, female, unknown };
class Date {
private:
int day, month, year;
friend std::ostream &operator<<(std::ostream &out, Date date);
public:
Date(int d = 1, int m = 1, int y = 1970);
};
Date::Date(int d, int m, int y) : day(d), month(m), year(y){};
std::ostream &operator<<(std::ostream &out, Date date) {
out << date.month << "/" << date.day << "/" << date.year;
return out;
}
class People {
private:
std::string name;
int number;
sexType sex;
Date birthday;
std::string id;
friend std::ostream &operator<<(std::ostream &output, People &ppl);
friend std::istream &operator>>(std::istream &input, People &ppl);
public:
People(std::string name = "undefined", int number = 0,
sexType sex = unknown, int birthdayDay = 1, int birthdayMonth = 1,
int birthdayYear = 1970, std::string id = "undefined");
};
People::People(std::string newName, int newNumber, sexType newSex,
int birthdayDay, int birthdayMonth, int birthdayYear,
std::string newID)
: name(newName), number(newNumber), sex(newSex),
birthday(birthdayDay, birthdayMonth, birthdayYear), id(newID){};
std::ostream &operator<<(std::ostream &output, People &ppl) {
output << "Name: " << ppl.name << std::endl;
output << "Employee number: " << ppl.number << std::endl;
output << "Sex: "
<< (ppl.sex == male ? "Male"
: (ppl.sex == female ? "Female" : "Unknown"))
<< std::endl;
output << "Birthday: " << ppl.birthday << std::endl;
output << "ID: " << ppl.id << std::endl;
return output;
}
std::istream &operator>>(std::istream &input, People &ppl) {
std::cout << "Name: ";
input >> ppl.name;
std::cout << "Employee No.: ";
input >> ppl.number;
std::string sex;
std::cout << "Sex (m/w): ";
input >> sex;
if (sex == "m") {
ppl.sex = male;
}
else if (sex == "w") {
ppl.sex = female;
}
else {
ppl.sex = unknown;
}
int d, m, y;
std::cout << "Birthday day: ";
input >> d;
std::cout << "Birthday month: ";
input >> m;
std::cout << "Birthday year: ";
input >> y;
ppl.birthday = Date(d, m, y);
std::cout << "ID number: ";
input >> ppl.id;
return input;
}
int main() {
People* a = new People[2];
for (int i = 0; i < 2; i++) {
std::cout << "####" << std::endl << "Update people No. "<< i + 1 << std::endl << "####" << std::endl;
std::cin >> a[i];
}
for (int i = 0; i < 2; i++) {
std::cout << a[i];
}
return 0;
}