Answer to Question #330462 in C++ for mak

Question #330462

Write a program to implement the Date class which has three data members named day, month and year.

 

1.  Provide a constructor, which takes arguments (you may set values of data members to 0).

 

2.  Write getter and setter functions (input and show functions) for the Date class.

3.  Also write a member function void CompareDates(Date d1, Date d2) which tells which date comes first.


1
Expert's answer
2022-04-21T07:02:53-0400






#include <iostream>
#include <iomanip>
#include <string>


using namespace std;
//Implement class Date
class Date
{
private:
unsigned day;//Day of month
unsigned month;//Month of year
unsigned year;//Current year
public:
Date();//Default constructor
Date(unsigned d,unsigned m,unsigned y);//Parametr 
//Implement getters
unsigned getDay()const;
unsigned getMonth()const;
unsigned getYear()const;
//Sets
void setDay(unsigned d);
void setMonth(unsigned m);
void setYear(unsigned y);
//print method
void Print();
void CompareDates(Date d1,Date d2);//Compare two date
};
int main()
{
  //For example 
  Date d1;
  cout<<"Please enter data for Date1\n";
  cout<<"Input day(1-31):";
  unsigned c;
  cin>>c;
  d1.setDay(c);


  cout<<"Input month(1-12):";
  cin>>c;
  d1.setMonth(c);


  cout<<"Input year:";
  cin>>c;
  d1.setYear(c);


  Date d2;
  cout<<"Please enter data for Date2\n";
  cout<<"Input day(1-31):";
  cin>>c;
  d2.setDay(c);


  cout<<"Input month(1-12):";
  cin>>c;
  d2.setMonth(c);


  cout<<"Input year:";
  cin>>c;
  d2.setYear(c);
  cout<<"Date1:=";
  d1.Print();
  cout<<"\nDate2:=";
  d2.Print();
  cout<<endl;
  d1.CompareDates(d1,d2);
   return 0; 
}
//Implemention out 
Date::Date()
{
  //set 0
  this->day=0;
  this->month=0;
  this->year=0;
}
Date::Date(unsigned d,unsigned m,unsigned y)
{
  this->day=d;
  this->month=m;
  this->year=y;
}
//return day
unsigned Date::getDay()const
{
  return day;
}
//-----return month
unsigned Date::getMonth()const
{
  return month;
}
//return year
unsigned Date::getYear()const
{
  return year;
}
void Date::setDay(unsigned d)
{
  this->day=d;
}
void Date::setMonth(unsigned m)
{
  this->month=m;
}
void Date::setYear(unsigned y)
{
  this->year=y;
}
void Date::Print()
{
  cout<<getDay()/10<<getDay()%10<<"/"<<getMonth()/10<<getMonth()%10<<"/"<<getYear();
}
void Date::CompareDates(Date d1,Date d2)
{
  cout<<"Come First: ";
  if(d1.getYear()<d2.getYear())
  {
    d1.Print();
    return;
  }
  else if(d1.getYear()>d2.getYear())
  {
    d2.Print();
    return;
  }
  //if both the same year then compare by month 
  if(d1.getMonth()<d2.getMonth())
  {
    d1.Print();
    return;
  }
  else if(d1.getMonth()>d2.getMonth())
  {
    d2.Print();
    return;
  } 
  if(d1.getDay()<d2.getDay())
  {
    d1.Print();
    return;
  }
  else if(d1.getDay()>d2.getDay())
  {
    d2.Print();
    return;
  } 
  cout<<" Both\n";
}

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