C++ Program to find Age and Days With Date of Birth Given using Class

Concept:
In this C++ Program the problem is to find age and days of birth when Date of Birth and present date is given. So main concept behind it is to find the difference between two given dates, using class structure in C++ Programming Language.
To Find Age in format Year/Month/Day

Year(Final) = Date(PresentYear) – BirthYear.
Month(Final) = Date(PresentMonth) – BirthMonth.
Day(Final) = Date(PresentDay) – BirthDay.

Again another function is to find the total days after birth.
To Find total days after birth

totalDays = (finalYear*365 + finalMonth*30 + finalDay);
/*
* Program to calculate age and days
* Given 
* --- Current date
* --- Your Date of Birth
* Results
* --- Finds out your age in format of Year/Month/Day
* --- Finds out Total Days 
* Note: I have not calculated the days with refrence to Leap Year that occurs in 4 Years gap.
* Author: Luzan Baral
* Twitter: http://twitter.com/luzanb
*/

#include<iostream>
#include<cstdlib>
#include<conio.h>
using namespace std;

class Find_DOB{
	public:
		int finalYear; // The Year 
		int birthYear; // The Year (Maths) 
		int presentYear; // The Year (Current) 
		int finalMonth; // The Month 
		int birthMonth; // The Month (Maths) 
		int presentMonth; // The Month (Current) 
		int finalDay; // The Day 
		int birthDay; // The Day (Maths) 
		int presentDay; // The Day (Current) 
		long int totalDays; //Stores total days
		//default constructur
		Find_DOB(){
			finalYear=0;
			birthYear=0;
			presentYear=0;
			finalMonth=0;
			birthMonth=0;
			presentMonth=0;
			finalDay=0;
			birthDay=0;
			presentDay=0;
			totalDays=0;
		}
		//fuction to get today's date
		void get_todayDate(){
			cout < < "Please enter the current date. (In Numeral)" << endl; 
			cout << "" << endl; 
			cout << "" << endl; 
			cout << "Enter the current year: ";  			cin >> presentYear; // Enter current year of date and hold in YYY. 
			cout < < "Enter the current month: ";  			cin >> presentMonth; // Enter current month of date and hold in MMM. 
			cout < < "Enter the current day: ";  			cin >> presentDay; // Enter current day of date and hold in DDD. 
			cout < < "" << endl; 
			cout << "" << endl; 
		}
		void get_DOB(){
			// The DOB. 
			cout << "Please enter your DOB." << endl; 
			cout << "Year: "; 
                        cin >> birthYear; // Enter year of birth and hold in YY. 
			cout < < "Month: ";  			
                        cin >> birthMonth; // Enter month of birth and hold in MM. 
			cout < < "Day: ";
  			cin >> birthDay; // Enter day of birth and hold in DD. 			
		}
		void calc_days(){
			finalYear = abs(presentYear - birthYear); // Year(Final) = Date(PresentYear) - BirthYear. 
			finalMonth = abs(presentMonth - birthMonth); // Month(Final) = Date(PresentMonth) - Birth(Month). 
			finalDay = abs(presentDay - birthDay); // Day(Final) = Date(PresentDay) - BirthDay. 
			cout < < "" << endl; 
			cout << "You are " << finalYear << "/" << finalMonth << "/" << finalDay << " years old." << endl; // Result. 
			totalDays = (finalYear*365 + finalMonth*30 + finalDay);
			cout << "Total of "<< totalDays <<" Days."<<endl; 
		}		
};

int main() 
{ 
	Find_DOB obj;
	// The Date. 
	cout << "This is a progran to tell how old you are..." << endl; 

	obj.get_todayDate();
	obj.get_DOB();
	obj.calc_days();

	getch();
	return 0; 
}

 

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>