Answer to Question #215492 in C++ for qwe

Question #215492

write a c++ program to create a class called date that has separate member data for day month year one constructor should initialize 

this data to 0 and another should initialize it to fixed values .another member function should display it The final member function 

should compare two objects of type date passed as arguments. A main() program should create two initialized date objects(among these one 

should contain today's date and another one should contain your date of birth) and one that isn’t initialized. Then it should compare 

the two initialized values together, leaving the result in the third date variable. Finally it should display the value of this 

third variable. Make appropriate member functions const. 


1
Expert's answer
2021-07-09T03:35:04-0400
#include <iostream>
using namespace std;
class Date{
    int day, month, year;
    public:
    Date():day(0), month(0), year(0){}
    Date(int d, int m, int y): day(d), month(m), year(y){
        while(day > 30){
            day -= 30;
            month++;
        }
        while(month > 12){
            month -= 12;
            year++;
        }
    }
    void display() const {
        cout<<day<<"/"<<month<<"/"<<year<<endl;
    }
    Date add(Date a, Date b){
        return Date(a.day + b.day, a.month + b.month, a.year + b.year);
    }
};
int main(){
    Date today(8, 7, 2021), dob(21, 03, 1999), third;
    third = third.add(today, dob);
    third.display();
    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