Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string.h>
- class Date
- {
- uint16_t year;
- uint8_t month;
- uint8_t day;
- uint8_t checkMonth(uint8_t monthP)
- {
- if (monthP < 1) return 1;
- if (monthP > 12) return 12;
- return monthP;
- }
- uint8_t checkDay(uint8_t dayP, uint8_t monthP, uint16_t yearP)
- {
- const uint8_t maxDays{ Date::daysInMonth(monthP, yearP) };
- if (dayP < 1) return 1;
- if (dayP > maxDays) return maxDays;
- return dayP;
- }
- public:
- static uint8_t daysInMonth(uint8_t month, uint16_t year)
- {
- return 30 + (((month < 8) and (month % 2 != 0)) or ((month >= 8) and (month % 2 == 0))) + ((month == 2) * (-2) + isLeapYear(year));
- }
- static bool isLeapYear(uint16_t year)
- {
- return year % 400 == 0 or year % 4 == 0 and year % 100 != 0;
- }
- static const uint8_t maxMonth{ 12 };
- Date() : Date(1, 1, 1970) {}
- Date(uint8_t dayP, uint8_t monthP, uint16_t yearP)
- : year{yearP}, month{ checkMonth(monthP)},day{checkDay(dayP,month,yearP)} {}
- Date& setDay(uint8_t dayP) { day = checkDay(dayP, month, year); return *this; }
- Date& setMonth(uint8_t monthP) { month = checkMonth(monthP); day = checkDay(day, month, year); return *this; }
- Date& setYear(uint16_t yearP) { year = yearP; return *this; }
- uint8_t getDay() const { return day; }
- uint8_t getMonth() const { return month; }
- uint16_t getYear() const { return year; }
- Date& print() { std::cout << (int)day << '.' << (int)month << '.' << year << '\n'; return *this; }
- };
- class Person{
- int id;
- char* fName;
- char* sName;
- char* lName;
- Date birthday;
- static int count;
- public:
- Person(): Person(10000, "John", "J.", "Doe", {1, 2, 3}) {}
- Person(int gotId, const char* fn, const char* sn, const char* ln, Date bd):
- id{ gotId }, fName{ new char[strlen(fn)+1]{} }, sName{ new char[strlen(sn)+1]{} }, lName{ new char[strlen(ln)+1]{} }, birthday{ bd }
- {
- strcpy(fName, fn); strcpy(sName, sn); strcpy(lName, ln); ++count;
- }
- Person(const Person& pers): id{pers.id}, birthday{pers.birthday} {
- strcpy(fName, pers.fName); strcpy(sName, pers.sName); strcpy(lName, pers.lName);
- }
- void print();
- Person& setId(int gotId){ id = gotId; return *this; }
- int getId() const { return id; }
- Person& setFName(const char* fn){ delete[] fName; fName = new char[strlen(fn)+1]; strcpy(fName, fn); return *this; }
- const char* getFName() const { return fName; }
- Person& setSName(const char* sn){ delete[] sName; sName = new char[strlen(sn)+1]; strcpy(sName, sn); return *this; }
- const char* getSName() const { return sName; }
- Person& setLName(const char* ln){ delete[] lName; lName = new char[strlen(ln)+1]; strcpy(lName, ln); return *this; }
- const char* getLName() const { return lName; }
- Person& setBthDay(const Date& bd){ birthday = bd; return *this; }
- Date& getBthDay() { return birthday; }
- static int getCount(){ return count; }
- ~Person() { delete[] fName; delete[] sName; delete[] lName; --count; }
- };
- void Person::print(){
- std::cout << "Id: " << id << "\nFirst Name: " << fName << "\nSecond Name: " << sName << "\nLast Name: " << lName << "\nDate of Birth: ";
- birthday.print();
- std::cout<<std::endl;
- }
- int Person::count{0};
- int main()
- {
- Person pers{12331, "Joanna", "Miley", "Dane", {17, 8, 1993}};
- pers.print();
- pers.setId(94959).setFName("Philip").setSName("Anthony").setLName("Hopkins").setBthDay({31, 7, 1964}).print();
- pers.getBthDay().setMonth(2);
- pers.print();
- std::cout << "Persons now: " << Person::getCount();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement