Create a class Date whose object can store a day, month and year. Include the necessary constructors to initialize the objects and function to display the date in ‘dd/mm/yyyy’ format. Define a non-member function findAge(Date dob, Date today) which should return the calculated age from the input dates ‘dob’ and ‘today’. Set this function as friend to Date class. Write a main function to read the today’s date and date of birth of a person from the user and display the age of that person by calling the proper function.
#include <iostream>
using namespace std;
class Date{
int d, m, y;
public:
Date(){}
Date(int dd, int mm, int yy){
d = dd;
m = mm;
y = yy;
}
void show(){
cout<<d<<"/"<<m<<"/"<<y<<endl;
}
friend int findAge(Date dob, Date today);
};
int findAge(Date dob, Date today){
return today.y - dob.y;
}
int main(){
char c; int day, month, year;
Date dob, today;
cout<<"Input today's date in the format (dd/mm/yy)\n";
cin>>day; cin>>c; cin>>month; cin>>c; cin>>year;
today = Date(day, month, year);
cout<<"Input date of birth in the format (dd/mm/yy)\n";
cin>>day; cin>>c; cin>>month; cin>>c; cin>>year;
dob = Date(day, month, year);
cout<<"\nToday: ";
today.show();
cout<<"Date of Birth: ";
dob.show();
cout<<"Age: "<<findAge(dob, today)<<" years";
return 0;
}
Comments
Leave a comment