giggiaus
asked on
use of an overloaded operator of class date
I have this class date:
// date.cpp
#include <iostream>
#include "Date.h"
#include <ctime>
//numero di giorni per ogni mese
const int Date::days[] = { 0, 31, 28, 31, 30, 31, 30,
31, 31, 30, 31, 30, 31 };
//costruttore
Date::Date( int m, int d, int y ) { setDate( m, d, y ); }
//set del giorno con controlli
void Date::setDay(int dd)
{
giornoCheck=1;
if ( month == 2 && leapYear( year ) )
{ if ( dd >= 1 && dd <= 29 ) day=dd;
else { day=1; giornoCheck=0;}
}
else
{ if ( dd >= 1 && dd <= days[ month ] ) day=dd;
else {day=1; giornoCheck=0;}
}
}
//set del mese con controlli
void Date::setMonth(int mm)
{
meseCheck=1;
if ( mm >= 1 && mm <= 12 ) month= mm;
else {month= 1; meseCheck=0; }
}
//set dell'anno con controlli
void Date::setYear(int yy)
{
annoCheck=1;
if ( yy >= 1900 && yy <= 2100) year = yy;
else {year = 2002; annoCheck=0; }
}
// Set della data
void Date::setDate( int mm, int dd, int yy )
{
setMonth(mm);
setYear(yy);
setDay(dd);
}
// le 3 get
int Date::getMonth() const {
return month;
}
int Date::getDay() const {
return day;
}
int Date::getYear() const {
return year;
}
//per usare l'operatore = anche con le date
bool Date::operator==(const Date & d) const {
return (d.month==month)&&(d.day== day)&&(d.y ear==year) ;
}
//per usare l'operatore < anche con le date
bool Date::operator<(const Date & d) const {
return (day+ ( month * days[month])+ (365*year) ) < ( d.day + (d.month * d.days[month]) + (365* d.year) );
}
//per usare l'operatore + anche con le date
Date Date::operator+( int g) const {
return Date( month , day+g , year );
}
//per usare l'operatore - anche con le date
Date Date::operator-( int g) const {
return Date( month , day-g , year );
}
//resoconto di validità totale
bool Date::isValid()
{
if ( giornoCheck==1 && annoCheck==1 && meseCheck==1) return true;
else return false;
}
//singoli resoconti di validità
bool Date::isDayValid()
{
return giornoCheck;
}
bool Date::isMonthValid()
{
return meseCheck;
}
bool Date::isYearValid()
{
return annoCheck;
}
// Operatore di Preincremento
Date & Date::operator++()
{
helpIncrement();
return *this;
}
// Operatore di postincremento
Date Date::operator++( int )
{
Date temp = *this;
helpIncrement();
// oggetto temporaneo salvato e non incrementato
return temp;
}
// aggiunge dei giorni ad una data
const Date &Date::operator+=( int additionalDays )
{
for ( int i = 0; i < additionalDays; i++ )
helpIncrement();
return *this;
}
//Controllo se bisestile o no
bool Date::leapYear( int y ) const
{
if ( y % 400 == 0 || ( y % 100 != 0 && y % 4 == 0 ) )
return true; // bisestile
else
return false; // non bisestile
}
//controlla che il giorno sia compatibile col mese
bool Date::endOfMonth( int d ) const
{
if ( month == 2 && leapYear( year ) )
return d == 29; // per febbraio
else
return d == days[ month ];
}
//incrementa la data
void Date::helpIncrement()
{
if ( endOfMonth( day ) && month == 12 ) { // finito l'anno
day = 1;
month = 1;
++year;
}
else if ( endOfMonth( day ) ) { // finito il mese
day = 1;
++month;
}
else // tutto ok si può incrementare
++day;
}
// per la stampa, overload dell'operatore <<
ostream &operator<<( ostream &output, const Date &d )
{
static char *monthName[ 13 ] = { "", "Gennaio",
"Febbraio", "Marzo", "Aprile", "Maggio", "Giugno",
"Luglio", "Agosto", "Settembre", "Ottobre",
"Novembre", "Dicembre" };
output << d.getDay() << ' '
<< monthName[ d.getMonth() ] << ' '<<d.getYear();
return output; //operatore a cascata
}
//per l'acquisizione, overload dell'operator >>
istream &operator>>( istream &input, Date &d )
{
int aux;
input >> aux;
d.setDay(aux);
input.ignore();
input >> aux;
d.setMonth(aux);
input.ignore();
input >> aux;
d.setYear(aux);
return input;
}
//legge la data del pc
Date Date::today()
{
time_t tempo_cal( time(NULL) );
struct tm * tempo_oggi ( localtime(&tempo_cal) );
return Date( tempo_oggi->tm_mon +1, tempo_oggi->tm_mday,tempo_ oggi->tm_y ear +1900 );
}
please give an example of using the operator>> to insert the date
// date.cpp
#include <iostream>
#include "Date.h"
#include <ctime>
//numero di giorni per ogni mese
const int Date::days[] = { 0, 31, 28, 31, 30, 31, 30,
31, 31, 30, 31, 30, 31 };
//costruttore
Date::Date( int m, int d, int y ) { setDate( m, d, y ); }
//set del giorno con controlli
void Date::setDay(int dd)
{
giornoCheck=1;
if ( month == 2 && leapYear( year ) )
{ if ( dd >= 1 && dd <= 29 ) day=dd;
else { day=1; giornoCheck=0;}
}
else
{ if ( dd >= 1 && dd <= days[ month ] ) day=dd;
else {day=1; giornoCheck=0;}
}
}
//set del mese con controlli
void Date::setMonth(int mm)
{
meseCheck=1;
if ( mm >= 1 && mm <= 12 ) month= mm;
else {month= 1; meseCheck=0; }
}
//set dell'anno con controlli
void Date::setYear(int yy)
{
annoCheck=1;
if ( yy >= 1900 && yy <= 2100) year = yy;
else {year = 2002; annoCheck=0; }
}
// Set della data
void Date::setDate( int mm, int dd, int yy )
{
setMonth(mm);
setYear(yy);
setDay(dd);
}
// le 3 get
int Date::getMonth() const {
return month;
}
int Date::getDay() const {
return day;
}
int Date::getYear() const {
return year;
}
//per usare l'operatore = anche con le date
bool Date::operator==(const Date & d) const {
return (d.month==month)&&(d.day==
}
//per usare l'operatore < anche con le date
bool Date::operator<(const Date & d) const {
return (day+ ( month * days[month])+ (365*year) ) < ( d.day + (d.month * d.days[month]) + (365* d.year) );
}
//per usare l'operatore + anche con le date
Date Date::operator+( int g) const {
return Date( month , day+g , year );
}
//per usare l'operatore - anche con le date
Date Date::operator-( int g) const {
return Date( month , day-g , year );
}
//resoconto di validità totale
bool Date::isValid()
{
if ( giornoCheck==1 && annoCheck==1 && meseCheck==1) return true;
else return false;
}
//singoli resoconti di validità
bool Date::isDayValid()
{
return giornoCheck;
}
bool Date::isMonthValid()
{
return meseCheck;
}
bool Date::isYearValid()
{
return annoCheck;
}
// Operatore di Preincremento
Date & Date::operator++()
{
helpIncrement();
return *this;
}
// Operatore di postincremento
Date Date::operator++( int )
{
Date temp = *this;
helpIncrement();
// oggetto temporaneo salvato e non incrementato
return temp;
}
// aggiunge dei giorni ad una data
const Date &Date::operator+=( int additionalDays )
{
for ( int i = 0; i < additionalDays; i++ )
helpIncrement();
return *this;
}
//Controllo se bisestile o no
bool Date::leapYear( int y ) const
{
if ( y % 400 == 0 || ( y % 100 != 0 && y % 4 == 0 ) )
return true; // bisestile
else
return false; // non bisestile
}
//controlla che il giorno sia compatibile col mese
bool Date::endOfMonth( int d ) const
{
if ( month == 2 && leapYear( year ) )
return d == 29; // per febbraio
else
return d == days[ month ];
}
//incrementa la data
void Date::helpIncrement()
{
if ( endOfMonth( day ) && month == 12 ) { // finito l'anno
day = 1;
month = 1;
++year;
}
else if ( endOfMonth( day ) ) { // finito il mese
day = 1;
++month;
}
else // tutto ok si può incrementare
++day;
}
// per la stampa, overload dell'operatore <<
ostream &operator<<( ostream &output, const Date &d )
{
static char *monthName[ 13 ] = { "", "Gennaio",
"Febbraio", "Marzo", "Aprile", "Maggio", "Giugno",
"Luglio", "Agosto", "Settembre", "Ottobre",
"Novembre", "Dicembre" };
output << d.getDay() << ' '
<< monthName[ d.getMonth() ] << ' '<<d.getYear();
return output; //operatore a cascata
}
//per l'acquisizione, overload dell'operator >>
istream &operator>>( istream &input, Date &d )
{
int aux;
input >> aux;
d.setDay(aux);
input.ignore();
input >> aux;
d.setMonth(aux);
input.ignore();
input >> aux;
d.setYear(aux);
return input;
}
//legge la data del pc
Date Date::today()
{
time_t tempo_cal( time(NULL) );
struct tm * tempo_oggi ( localtime(&tempo_cal) );
return Date( tempo_oggi->tm_mon +1, tempo_oggi->tm_mday,tempo_
}
please give an example of using the operator>> to insert the date
ASKER
I want only an example on how to use operator>> (of class date) to fill in the code.
ifstream is ("yourfile.txt"); // has to be intialized
Date d;
is >> d;
Date d;
is >> d;
ASKER
I have insert your exaple adepted to my code but it give me this error on compilation:
paerse error before '>'
on line is>>d
paerse error before '>'
on line is>>d
The following headers <iostream> and <fstream> need to be included. A parse error could be due to an error on the line before like ommiting the ; at the end of a command. Can you show the code of the example?
I could give you an example of a class that is a little bit simpler:
class Date
{
public:
string date;
};
istream& operator>>(istream& is, Date& dt)
{
is >> dt.date;
return is;
}
int main()
{
Date dt;
cin >> dt;
cout << dt.date << endl;
return 0;
}
Note, that operator>> function is defined outside of class.
You could easily adapt your class if you provide an operator=() that takes a const string& as argument
istream& operator>>(istream& is, Date& dt)
{
string temp;
is >> temp;
dt = temp; // Here the operator= applies
return is;
}
Tell me, if you need more help.
Regards, Alex
class Date
{
public:
string date;
};
istream& operator>>(istream& is, Date& dt)
{
is >> dt.date;
return is;
}
int main()
{
Date dt;
cin >> dt;
cout << dt.date << endl;
return 0;
}
Note, that operator>> function is defined outside of class.
You could easily adapt your class if you provide an operator=() that takes a const string& as argument
istream& operator>>(istream& is, Date& dt)
{
string temp;
is >> temp;
dt = temp; // Here the operator= applies
return is;
}
Tell me, if you need more help.
Regards, Alex
ASKER
hi Alex,
how can I use the overloaded operartor>> of my class Date in my code
how can I use the overloaded operartor>> of my class Date in my code
You see it in
int main()
{
// You would need a default constructor setting all members to 0 (invalid date)
Date dt; // or use your constructor: Date dt(0, 0, 0);
// Print to screen:
cout << "Please type in a valid date (DD.MM.YYYY): " << endl;
// Then you may type in the date from keyboard as string, e. g. "10.12.2003"
cin >> dt;
// This code is only for test
cout << dt.day << "." << dt.month << "." << dt.year << endl;
return 0;
}
Hope, that helps
Alex
int main()
{
// You would need a default constructor setting all members to 0 (invalid date)
Date dt; // or use your constructor: Date dt(0, 0, 0);
// Print to screen:
cout << "Please type in a valid date (DD.MM.YYYY): " << endl;
// Then you may type in the date from keyboard as string, e. g. "10.12.2003"
cin >> dt;
// This code is only for test
cout << dt.day << "." << dt.month << "." << dt.year << endl;
return 0;
}
Hope, that helps
Alex
ASKER
the compiler give me this error:
parse error before ">" in cin >> dt; .....................
parse error before ">" in cin >> dt; .....................
Does the error go away when you replace cin with std::cin?
In that case you could add the line
using namespace std;
just after the include lines.
What compiler are you using? Could be easier to follow if one test it on the same one.
In that case you could add the line
using namespace std;
just after the include lines.
What compiler are you using? Could be easier to follow if one test it on the same one.
That could have two reasons:
1. You forgot to include the iostream header
#include <iostream>
2. You forgot to include namespace std
using namespace std;
That statement you may include in Date.h after all standard includes:
// date.h
#ifndef DATE_H
#define DATE_H
#include <string>
#include <iostream>
...
using namespace std;
...
class Date
{
...
};
#endif
1. You forgot to include the iostream header
#include <iostream>
2. You forgot to include namespace std
using namespace std;
That statement you may include in Date.h after all standard includes:
// date.h
#ifndef DATE_H
#define DATE_H
#include <string>
#include <iostream>
...
using namespace std;
...
class Date
{
...
};
#endif
ASKER
the error there is again.....
Can you try
std::cin >> dt;
as STeH has suggested?
If this compiles you should include the line
using namespace std;
directly before
int main()
{
...
cin >> dt;
...
}
If the error still exists you should check the line before if the semicolon ';' is present and nothing is between
semicolon and cin.
Regards, Alex
std::cin >> dt;
as STeH has suggested?
If this compiles you should include the line
using namespace std;
directly before
int main()
{
...
cin >> dt;
...
}
If the error still exists you should check the line before if the semicolon ';' is present and nothing is between
semicolon and cin.
Regards, Alex
ASKER
my compilator is devcpp 4.9.6.0
ASKER
and now the error is :
parse error before "::" in line std::cin>>dt;
my code is :
.......
do
cout<<"insert the date(DD.MM.YYYY)";
std::cin>>dt;
while (dt.isValid());
.................
parse error before "::" in line std::cin>>dt;
my code is :
.......
do
cout<<"insert the date(DD.MM.YYYY)";
std::cin>>dt;
while (dt.isValid());
.................
Add some scope brackets
do
{
cout<<"insert the date(DD.MM.YYYY)";
std::cin>>dt;
}
while (dt.isValid());
regards, Alex
do
{
cout<<"insert the date(DD.MM.YYYY)";
std::cin>>dt;
}
while (dt.isValid());
regards, Alex
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you SteH, I solved the prblem....but now i have another problem
see the question: "(difference between two date) to number of days"
see the question: "(difference between two date) to number of days"
For the operator>> you should look at what will be in the stream when it was written with operator<<. The month is not an int any more.
Was that your question?