Answer to Question #213025 in C++ for Hemambar

Question #213025

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.

1
Expert's answer
2021-07-11T14:38:53-0400
#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;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS