C++ Increment and Decrement keep returning the same numbers. I can\'t locate my
ID: 3891519 • Letter: C
Question
C++ Increment and Decrement keep returning the same numbers. I can't locate my error.
Design a class called NumDays. The class’s purpose is to store a value that will convert the number of worked hours to a number of days. For example, 8 hours would be converted to 1 day, 12 hours would be converted to 1.5 days and 18 hours converted to 2.25 days. The class must have a constructor that accepts a number of hours. There must also be member function to set and get the hours and days. The class should have 2 data members, hours and days.
The class will also overload several operators:
the addition operator. This operator will add the hours of the two objects and return a new instance of the NumDays
with its hour’s data member set to the sum of the other two objects.
the subtraction operator will also be overloaded which will subtract the two objects and return a new instance of the NumDays class.
the prefix and postfix increment operator. These operators should increment the number of hours stored in the object. It will return an instance of the NumDays object.
the prefix and postfix decrement operator. These operators should decrement the number of hours stored in the object. It will return an instance of the NumDays object.
Note that when the number of hours changes, the number of days should always be updated. The user of this class should be able to use the object in a statement like C = A + B; where A, B and C are instances of the NumDays class. Main must show that the class and all the operators work correctly.
NumDays.h
#ifndef NUMDAYS_H
#define NUMDAYS_H
#include <iostream>
#include <math.h>
class NumDays
{
private:
double hours, days;
void calcDays();
public:
// Constructors
NumDays();
NumDays(double);
// Mutator
void setHours(double);
// Accessor
double getHours();
double getDays();
// Overloaded operator
NumDays operator + (const NumDays &); // Overloaded +
NumDays operator - (const NumDays &); // Overloaded -
NumDays operator ++ (); // Overloaded Prefix ++
NumDays operator ++ (int); // Overloaded Postfix ++
NumDays operator -- (); // Overloaded Prefix --
NumDays operator -- (int); // Overloaded Postfix --
};
#endif
NumDays.cpp
// Implementation file for the NumDays class
#include <iostream>
#include <math.h>
#include "NumDays.h"
// Recalculation function
void NumDays::calcDays()
{
days = hours / 8;
}
// Default Constructor
NumDays::NumDays()
{
hours = 0;
days = 0;
}
// Constructor 1
NumDays::NumDays(double h)
{
hours = h;
days = hours * 1 / 8;
}
// Mutatory Functions
void NumDays::setHours(double h)
{
hours = h;
}
// Accessor Functions
double NumDays::getHours()
{
return hours;
}
double NumDays::getDays()
{
NumDays::calcDays();
return days;
}
// Overloaded operator
NumDays NumDays::operator + (const NumDays &a)
{
NumDays temp;
temp.hours = this->hours + a.hours;
return temp;
}
NumDays NumDays::operator - (const NumDays &a)
{
NumDays temp;
temp.hours = this->hours - a.hours;
return temp;
}
NumDays NumDays::operator ++ ()
{
++hours;
calcDays();
return *this;
}
NumDays NumDays::operator -- ()
{
--hours;
calcDays();
return *this;
}
NumDays NumDays::operator ++ (int)
{
NumDays temp(*this);
++hours;
return temp;
}
NumDays NumDays::operator -- (int)
{
hours--;
calcDays();
return *this;
}
Source.cpp
#include <iostream>
#include "NumDays.h"
using namespace std;
int main()
{
double hours1, hours2;
//Request hour input
cout << "Enter the number of hours worked during week 1: ";
cin >> hours1;
cout << "Enter the number of hours worked during week 2 ";
cin >> hours2;
// Define hours worked
NumDays one(hours1), two(hours2);
cout << "One: " << one.getDays() << " day(s)" << endl;
cout << "Two: " << two.getDays() << " day(s)" << endl << endl;
// Addition and subtraction
cout << "Three = One + Two: " << (one + two).getDays() << " day(s)" << endl;
cout << "Four = One - Two: " << (one - two).getDays() << " day(s)" << endl << endl;
// Define a third and fourth object
NumDays three(one + two), four(one - two);
// Increment and decrement
four = three++;
cout << "Four = Three++ " << endl;
cout << " Three: " << three.getDays() << " day(s)" << endl;
cout << " Four: " << four.getDays() << " day(s)" << endl << endl;
four = ++three;
cout << "Four = ++Three: " << endl;
cout << " Three: " << three.getDays() << " day(s)" << endl;
cout << " Four: " << four.getDays() << " day(s)" << endl << endl;
four = three--;
cout << "Four = Three--: " << endl;
cout << " Three: " << three.getDays() << " day(s)" << endl;
cout << " Four: " << four.getDays() << " day(s)" << endl << endl;
four = --three;
cout << "Four = --Three: " << endl;
cout << " Three: " << three.getDays() << " day(s)" << endl;
cout << " Four: " << four.getDays() << " day(s)" << endl;
system("pause");
return 0;
}
Explanation / Answer
Given below is the completed/fixed code for the NumDays.cpp
Please do rate the answer if it was helpful. Thank you
NumDays.cpp
--------
// Implementation file for the NumDays class
#include <iostream>
#include <math.h>
#include "NumDays.h"
// Recalculation function
void NumDays::calcDays()
{
days = hours / 8;
}
// Default Constructor
NumDays::NumDays()
{
hours = 0;
days = 0;
}
// Constructor 1
NumDays::NumDays(double h)
{
hours = h;
days = hours * 1 / 8;
}
// Mutatory Functions
void NumDays::setHours(double h)
{
hours = h;
calcDays();
}
// Accessor Functions
double NumDays::getHours()
{
return hours;
}
double NumDays::getDays()
{
return days;
}
// Overloaded operator
NumDays NumDays::operator + (const NumDays &a)
{
return NumDays(hours + a.hours);
}
NumDays NumDays::operator - (const NumDays &a)
{
return NumDays(hours - a.hours);
}
NumDays NumDays::operator ++ ()
{
++hours;
calcDays();
return *this;
}
NumDays NumDays::operator -- ()
{
--hours;
calcDays();
return *this;
}
NumDays NumDays::operator ++ (int)
{
NumDays temp(*this);
++hours;
calcDays();
return temp;
}
NumDays NumDays::operator -- (int)
{
NumDays temp(*this);
hours--;
calcDays();
return temp;
}
output
=----
Enter the number of hours worked during week 1: 40
Enter the number of hours worked during week 2 30
One: 5 day(s)
Two: 3.75 day(s)
Three = One + Two: 8.75 day(s)
Four = One - Two: 1.25 day(s)
Four = Three++
Three: 8.875 day(s)
Four: 8.75 day(s)
Four = ++Three:
Three: 9 day(s)
Four: 9 day(s)
Four = Three--:
Three: 8.875 day(s)
Four: 9 day(s)
Four = --Three:
Three: 8.75 day(s)
Four: 8.75 day(s)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.