Utilizing postfix/prefix increment/decrement operators. Wanted to verify output
ID: 3869302 • Letter: U
Question
Utilizing postfix/prefix increment/decrement operators. Wanted to verify output is valid.
Enter number of hours for a: 20
Enter number of hours for b: 8
a=2.500
b=1.000
c = a + b: c=3.500
c = a - b: c=1.500
c = (a++) + (b++): a=2.625, b=1.125, c=3.500
c = (++a) + (++b): a=2.750, b=1.250, c=4.000
c = (a--) - (b--): a=2.625, b=1.125, c=1.500
c = (--a) - (--b): a=2.500, b=1.000, c=1.500
/*********************************
This program converts the number
of hours worked to number of days.
*********************************/
#include <iostream>
#include <iomanip>
using namespace std;
class NumDays
{
private:
double hours;
double days;
public:
NumDays()
{
hours = 0;
days = 0;
}
NumDays(double h)
{
hours = h;
days = h / 8;
}
void setHours(double h)
{
hours = h;
days = h / 8;
}
void setDays(double d)
{
days = d;
hours = d * 8;
}
double getHours()
{
return hours;
}
double getDays()
{
return days;
}
NumDays operator+ (NumDays &);
NumDays operator- (NumDays &);
NumDays operator++ (); //Prefix
NumDays operator++ (int); //Postfix
NumDays operator-- (); //prefix
NumDays operator-- (int); //Postfix
};
NumDays NumDays::operator+ (NumDays &n)
{
NumDays t;
t.setHours(hours + n.getHours());
return t;
}
NumDays NumDays::operator- (NumDays &n)
{
NumDays t;
t.setHours(hours - n.getHours());
return t;
}
NumDays NumDays::operator++ ()
{
hours++;
days = hours / 8;
return *this;
}
NumDays NumDays::operator-- ()
{
hours--;
days = hours / 8;
return *this;
}
NumDays NumDays::operator++ (int)
{
NumDays t(hours);
hours++;
days = hours / 8;
return t;
}
NumDays NumDays::operator-- (int)
{
NumDays t(hours);
hours--;
days = hours / 8;
return t;
}
int main()
{
double hours1, hours2;
// User input and validation
cout << "Enter number of hours for a: ";
cin >> hours1;
while (hours1 <= 0)
{
cout << " Enter a positive number greater than 0: ";
cin >> hours1;
cout << endl;
}
cout << "Enter number of hours for b: ";
cin >> hours2;
while (hours2 <= 0)
{
cout << " Enter a positive number greater than 0: ";
cin >> hours2;
cout << endl;
}
// Define objects of hours
NumDays a(hours1), b(hours2), c;
cout << showpoint << fixed << setprecision(3);
cout << endl;
cout << "a=" << a.getDays() << endl;
cout << "b=" << b.getDays() << endl;
c = a + b;
cout << "c = a + b: c=" << c.getDays() << endl;
c = a - b;
cout << "c = a - b: c=" << c.getDays() << endl;
// Postfix and prefix increment operators++
c = a++ + b++;
cout << "c = (a++) + (b++): a=" << a.getDays() << ", b=" << b.getDays() << ", c=" << c.getDays() << endl;
c = ++a + ++b;
cout << "c = (++a) + (++b): a=" << a.getDays() << ", b=" << b.getDays() << ", c=" << c.getDays() << endl;
// Postfix and prefix decrement operators--
c = a-- - b--;
cout << "c = (a--) - (b--): a=" << a.getDays() << ", b=" << b.getDays() << ", c=" << c.getDays() << endl;
c = --a - --b;
cout << "c = (--a) - (--b): a=" << a.getDays() << ", b=" << b.getDays() << ", c=" << c.getDays() << endl;
system("pause");
return 0;
Explanation / Answer
OUTPUT
a=2.500
b=1.000
c = a + b: c=3.500
c = a - b: c=1.500
c = (a++) + (b++): a=2.625, b=1.125, c=3.500
c = (++a) + (++b): a=2.750, b=1.250, c=4.000
c = (a--) - (b--): a=2.625, b=1.125, c=1.500
c = (--a) - (--b): a=2.500, b=1.000, c=1.500
is valid
as a=20 hours and days worked=20/8=2.5
and b=8 and b's days work=1.0
c=a+b=2.5 + 1.0 =3.5
Similarly c = a - b: c=1.500 is true
c = (a++) + (b++): a=2.625, b=1.125, c=3.500
first brackets are done and in left to right manner
a=2.5 + (1/8)=2.5+.125=2.625
and b=1+.125=1.125
c=2.625+1.125=4.0
Similarly, the next 3 output
c = (++a) + (++b): a=2.750, b=1.250, c=4.000
c = (a--) - (b--): a=2.625, b=1.125, c=1.500
c = (--a) - (--b): a=2.500, b=1.000, c=1.500
are correct
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.