avr39ripe

cppHumanBazhivVladislavVersion

Jul 29th, 2021
689
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <string.h>
  3.  
  4. class Date
  5. {
  6.     uint16_t year;
  7.     uint8_t month;
  8.     uint8_t day;
  9.    
  10.     uint8_t checkMonth(uint8_t monthP)
  11.     {
  12.         if (monthP < 1) return 1;
  13.         if (monthP > 12) return 12;
  14.         return monthP;
  15.     }
  16.     uint8_t checkDay(uint8_t dayP, uint8_t monthP, uint16_t yearP)
  17.     {
  18.         const uint8_t maxDays{ Date::daysInMonth(monthP, yearP) };
  19.         if (dayP < 1) return 1;
  20.         if (dayP > maxDays) return maxDays;
  21.         return dayP;
  22.     }
  23. public:
  24.     static uint8_t daysInMonth(uint8_t month, uint16_t year)
  25.     {
  26.         return 30 + (((month < 8) and (month % 2 != 0)) or ((month >= 8) and (month % 2 == 0))) + ((month == 2) * (-2) + isLeapYear(year));
  27.     }
  28.  
  29.     static bool isLeapYear(uint16_t year)
  30.     {
  31.         return year % 400 == 0 or year % 4 == 0 and year % 100 != 0;
  32.     }
  33.     static const uint8_t maxMonth{ 12 };
  34.  
  35.     Date() : Date(1, 1, 1970) {}
  36.     Date(uint8_t dayP, uint8_t monthP, uint16_t yearP)
  37.         : year{yearP}, month{ checkMonth(monthP)},day{checkDay(dayP,month,yearP)} {}
  38.  
  39.     Date& setDay(uint8_t dayP) { day = checkDay(dayP, month, year); return *this; }
  40.     Date& setMonth(uint8_t monthP) { month = checkMonth(monthP); day = checkDay(day, month, year); return *this; }
  41.     Date& setYear(uint16_t yearP) { year = yearP; return *this; }
  42.  
  43.     uint8_t getDay() const { return day; }
  44.     uint8_t getMonth() const { return month; }
  45.     uint16_t getYear() const { return year; }
  46.  
  47.     Date& print() { std::cout << (int)day << '.' << (int)month << '.' << year << '\n'; return *this; }
  48. };
  49.  
  50. class Person{
  51.    
  52.     int id;
  53.     char* fName;
  54.     char* sName;
  55.     char* lName;
  56.     Date birthday;
  57.     static int count;
  58.    
  59. public:
  60.    
  61.     Person(): Person(10000, "John", "J.", "Doe", {1, 2, 3}) {}
  62.    
  63.     Person(int gotId, const char* fn, const char* sn, const char* ln, Date bd):
  64.     id{ gotId }, fName{ new char[strlen(fn)+1]{} }, sName{ new char[strlen(sn)+1]{} }, lName{ new char[strlen(ln)+1]{} }, birthday{ bd }
  65.     {
  66.         strcpy(fName, fn); strcpy(sName, sn); strcpy(lName, ln); ++count;
  67.     }
  68.    
  69.     Person(const Person& pers): id{pers.id}, birthday{pers.birthday} {
  70.        
  71.         strcpy(fName, pers.fName); strcpy(sName, pers.sName); strcpy(lName, pers.lName);
  72.     }
  73.    
  74.     void print();
  75.    
  76.     Person& setId(int gotId){ id = gotId; return *this; }
  77.     int getId() const { return id; }
  78.    
  79.     Person& setFName(const char* fn){ delete[] fName; fName = new char[strlen(fn)+1]; strcpy(fName, fn); return *this; }
  80.     const char* getFName() const { return fName; }
  81.    
  82.     Person& setSName(const char* sn){ delete[] sName; sName = new char[strlen(sn)+1]; strcpy(sName, sn); return *this; }
  83.     const char* getSName() const { return sName; }
  84.    
  85.     Person& setLName(const char* ln){ delete[] lName; lName = new char[strlen(ln)+1]; strcpy(lName, ln); return *this; }
  86.     const char* getLName() const { return lName; }
  87.    
  88.     Person& setBthDay(const Date& bd){ birthday = bd; return *this; }
  89.     Date& getBthDay() { return birthday; }
  90.    
  91.     static int getCount(){ return count; }
  92.    
  93.     ~Person() { delete[] fName; delete[] sName; delete[] lName; --count; }
  94. };
  95.  
  96. void Person::print(){
  97.     std::cout << "Id: " << id << "\nFirst Name: " << fName << "\nSecond Name: " << sName << "\nLast Name: " << lName << "\nDate of Birth: ";
  98.     birthday.print();
  99.     std::cout<<std::endl;
  100. }
  101.  
  102. int Person::count{0};
  103.  
  104. int main()
  105. {
  106.    
  107.     Person pers{12331, "Joanna", "Miley", "Dane", {17, 8, 1993}};
  108.    
  109.     pers.print();
  110.    
  111.     pers.setId(94959).setFName("Philip").setSName("Anthony").setLName("Hopkins").setBthDay({31, 7, 1964}).print();
  112.  
  113.     pers.getBthDay().setMonth(2);
  114.    
  115.     pers.print();
  116.    
  117.     std::cout << "Persons now: " << Person::getCount();
  118.    
  119.     return 0;
  120. }
  121.  
RAW Paste Data