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 add 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.
#include <iostream>
using namespace std;
class Date{
int day, month, year;
public:
Date(){
day = month = year = 0;
}
Date(int dd, int mm, int yy){
while(dd > 30){
dd -= 30;
mm++;
}
while(mm > 12){
mm -= 12;
yy++;
}
day = dd;
month = mm;
year = yy;
}
void display() const {
cout<<day<<"/"<<month<<"/"<<year<<endl;
}
Date add(Date first, Date second) const{
return Date(first.day + second.day, first.month + second.month, first.year + second.year);
}
};
int main(){
Date today(11, 7, 2021), dob(12, 03, 1998), empty;
empty = today.add(today, dob);
cout<<"Today: "; today.display();
cout<<"DOB: "; dob.display();
cout<<"Today + DOB: ";
empty.display();
return 0;
}
Comments
Leave a comment