Overloaded Operators – Basic Date Class Create a new project named Assign08-Over
ID: 3712812 • Letter: O
Question
Overloaded Operators – Basic Date Class Create a new project named Assign08-OverloadedOperators. The driver program can be copied from OverloadedOperators-DateDriver.txt, and the Date class header file can be found in OverloadedOperators- DateHeader.txt. Both files are located in C:VCProjectsoop. Add these six overloaded operators to the Date class Date.cpp implementation file: == != = along with the insertion and extraction operators >> and (istream&, Date&); loverloaded extraction operator zz friend to Date class friend ostream& operator 1/31/2018 allows direct input of month day year Inn dd yyyy -- Enter date using this format values into a cin input stream with space separators. Press any key to continue.. 18/01/2012 2/09/1996Explanation / Answer
Program plan:as per the question date header file with overloaded methods must be defined.the operators like <,<=,>,>=,==,!= i.e., all the logical operators and insertion(<<) and extraction (>>) operators must be overloaded in date header file.the driver class which includes the date eader file should be able to use all the ovaerloaded methods.
header file definition:
#pragma once
#include<iostream.h>
using namespace std;
class Date
{
private:
int mm,dd,yyyy;
friend istream& operator>>(istream&,date&);
friend ostrema& operator>>(ostream&,date&);
public:
Date() //default constructor
{
mm=01;
dd=01;
yyyy=1990;
}
//parametrised constructor
Date(int m,int d,int y)
{
mm=m,
dd=d;
yyyy=y;
}
void setdate(int mm,int dd,int yyyy)
{
mm=this.mm;
dd=this.dd;
yyyy=this.yyyy;
}
void displayDate()
{
cout<<"date is"<<dd<<"/"<<mm<<"/"<<yyyy<<"yyyy"<<" ";
}
/*overloading methods
bool operator==(const Date&);
bool operator!=(const Date&);
bool operator<(const Date&);
bool operator>(const Date&);
bool operator<=(const Date&);
bool operator>=(const Date&);
*/
bool operator==(const Date& d)
{
if(this->mm==d.mm && this->dd==d.dd && this->yyyy==d.yyyy) return 1;
else return 0;
}
bool operator!=(const Date& d)
{
if(this->mm!=d.mm)return 1;
else return 0;
}
bool operator>(const Date& d)
{
if(this->yyyy > d.yyyy)return 1;
else if(this->yyyy == d.yyyy)
{
if(this->mm > d.mm)return 1;
else if(this->mm==d.mm)
{
if(this->dd>d.dd) return 1;
else return 0;
}
}
bool operator <(const Date& d)
{
if(this->yyyy<d.yyyy) return 1;
else
if(this->yyyy==d.yyyy)
{
if (this->mm<d.mm) return1;
else if(this->mm==d.mm)
{
if(this->dd<d.dd )return 1;
else return0;
}
}
}
bool operator<=(const Date& d)
{
if(this->yyyy > d.yyyy)return 1;
else if(this->yyyy == d.yyyy)
{
if(this->mm > d.mm)return 1;
else if(this->mm==d.mm)
{
if(this->dd>d.dd) return 1;
else return 0;
}
}
}
bool operator >=(const Date& d)
{
if(this->yyyy<d.yyyy) return 1;
else
if(this->yyyy==d.yyyy)
{
if (this->mm<d.mm) return1;
else if(this->mm==d.mm)
{
if(this->dd<d.dd )return 1;
else return0;
}
}
}
private:
int mm,dd,yyyy;
};//class end
//>> overloading
istream& operator>>(istream &in,Date &d)
{
clrscr();
cout<<"enter date in mm/dd/yyyy format ";
cout<<"enter month";
in>>d.mm;
cout<<"enter day no";
in>>d.dd;
cout<<"enter year";
in>>d.yyyy;
}
//<< overloading
istream& operator<<(ostream &out,Date &d)
{
out<<"date in mm/dd/yyyy format: ";
out<<d.mm<<"/"<<d.dd<<"/"<d.yyyy;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.