Advertisement
irmantas_radavicius

Untitled

Mar 23rd, 2022
763
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.12 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <sstream>
  4.  
  5. using namespace std;
  6.  
  7. class Person{
  8.     private:
  9.         string name;
  10.         int currentYear, currentMonth, currentDay, birthYear, birthMonth, birthDay,
  11.                 calculatedYears, calculatedMonths, calculatedDays;
  12.    
  13.     public:
  14.         Person(string name, int currentYear, int currentMonth, int currentDay, int birthYear, int birthMonth, int birthDay){
  15.             this->name = name;
  16.             this->currentYear = currentYear;
  17.             this->currentMonth = currentMonth;
  18.             this->currentDay = currentDay;
  19.             this->birthYear = birthYear;
  20.             this->birthMonth = birthMonth;
  21.             this->birthDay = birthDay;
  22.             this->calculatedYears = 0;
  23.             this->calculatedMonths = 0;
  24.             this->calculatedDays = 0;
  25.         }
  26.  
  27.         void calculateAge(){
  28.             int month[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  29.            
  30.             if (birthDay > currentDay){
  31.                 currentDay = currentDay + month[birthMonth - 1];
  32.                 currentMonth = currentMonth - 1;
  33.             }
  34.            
  35.             if (birthMonth > currentMonth){
  36.                 currentYear = currentYear - 1;
  37.                 currentMonth = currentMonth + 12;
  38.             }
  39.  
  40.             calculatedDays = currentDay - birthDay;
  41.             calculatedMonths = currentMonth - birthMonth;
  42.             calculatedYears = currentYear- birthYear;
  43.         }
  44.  
  45.         string getName(){
  46.             return name;
  47.         }
  48.  
  49.         int getYears(){
  50.             return calculatedYears;
  51.         }
  52.  
  53.         int getMonths(){
  54.             return calculatedMonths;
  55.         }
  56.  
  57.         int getDays(){
  58.             return calculatedDays;
  59.         }
  60.  
  61.         string toString(){
  62.             stringstream ss;
  63.             ss << getName() << " is " << getYears() << " years, " << getMonths() << " months and " << getDays() << " days old";
  64.             return ss.str();
  65.         }
  66. };
  67.  
  68. int main(){
  69.     Person person("John", 2022, 3, 3, 1985, 1, 1);
  70.  
  71.     person.calculateAge();
  72.    
  73.     cout << person.toString() << endl;
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement