Answer to Question #214829 in C++ for Hemambar

Question #214829

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 andanother 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-12T17:18:58-0400
#include <iostream>
using namespace std;


class Date{
	int day;
	int month;
	int year;
public:
	Date(){
		this->day=0;
		this->month=0;
		this->year=0;
	}


	Date(int d, int m, int y){
		this->day=d;
		this->month=m;
		this->year=y;


	}
	
	void print() const {
		cout<<day<<"/"<<month<<"/"<<year<<"\n";
	}
	Date compare(Date Date1,Date Date2) const{


		int month[] = { 31, 28, 31, 30, 31, 30, 31,
			31, 30, 31, 30, 31 };


		if (Date2.day > Date1.day) {
			Date1.day+=  month[Date2.month - 1];
			Date1.month--;
		}
		if (Date2.month > Date1.month) {
			Date1.year--;
			Date1.month += 12;
		}
		
		int ageDays = Date1.day - Date2.day;
		int ageMonths = Date1.month - Date2.month;
		int ageYears =Date1.year - Date2.year;
		return Date(ageDays,ageMonths,ageYears);
	}


};
int main(){
	Date today_Date(12,7,2021);
	cout<<"Today's date: ";
	today_Date.print();
	cout<<"Birth's date: ";
	Date birth_Date(6,6,2000);
	birth_Date.print();
	Date third = third.compare(today_Date, birth_Date);
	cout<<"\nThird date: ";
	third.print();


	system("pause");
	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