#include <iostream>
using namespace std;
class NumDays
{
private:
int hour;
int day;
void simplify();
public:
NumDays()
{
day = 0;
hour = 0;
}
void setData(int d, int h)
{
hour = h;
day = d;
simplify();
}
int getHour()
{
return hour;
}
int getDay()
{
return day;
}
NumDays operator++(int);
NumDays operator--(int);
};
void NumDays::simplify()
{
hour = 8*day + hour;
day = hour / 8;
hour = hour % 8;
}
NumDays NumDays::operator++(int)
{
NumDays obj1;
hour++;
simplify();
return obj1;
}
NumDays NumDays::operator--(int)
{
NumDays obj1;
hour--;
simplify();
return obj1;
}
void setFirst(NumDays &);
void setSecond(NumDays &);
void addData(NumDays &, NumDays &, NumDays &);
int main()
{
NumDays first, second, third;
setFirst(first);
setSecond(second);
addData(first, second, third);
}
void setFirst(NumDays &obj1)
{
int day, hour = 0;
cout << "Please enter the amount of days followed by hours." << endl;
cin >> day >> hour;
obj1.setData(day, hour);
}
void setSecond(NumDays &obj2)
{
int day, hour = 0;
cout << "Please enter the amount of days followed by hours again." << endl;
cin >> day >> hour;
obj2.setData(day, hour);
}
void addData(NumDays &obj1, NumDays &obj2, NumDays &obj3)
{
for (int k = 0; k < 8; k++)
{
obj3 = obj1++;
cout << " First Data: " << obj3.getDay() << " day(s), "
<< obj3.getHour() << " hour(s).";
cout << " First Data: " << obj1.getDay() << " day(s), "
<< obj1.getHour() << " hour(s).\n";
}
cout << endl;
for (int l = 0; l < 8; l++)
{
obj3 = obj2++;
cout << " Second Data: " << obj3.getDay() << " day(s), "
<< obj3.getHour() << " hour(s).";
cout << " Second Data: " << obj2.getDay() << " day(s), "
<< obj2.getHour() << " hour(s).\n";
}
}
When I run the file, the obj3 have 0 days and 0 hours and the obj2 increases. How come it is like this? When I tried it as obj3 = obj2 without any postfix signs, it copies it over. So that I assume that there is no problem on getting data from obj2. But when I include the postfix operator, the data becomes 0 and 0.