Overloading Operators

Hey there! I'm writing a program that converts the amount of hours input into the number of days. It then uses overloaded operators to manipulate this data, and I just need to prove that they are working with my output. Everything is working except one part and I'm beating my head against a wall trying to figure out exactly why. Here's my code:

NumDays.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
// Implementation file for the NumDays class
#include <iostream>
#include <math.h>
#include "NumDays.h"
using namespace std;

// Calculate number of days
void NumDays::calcDays()
{
	days = hours / 8;
}

// Default Constructor
NumDays::NumDays()
{
	hours = 0;
	days = 0;
}

// Constructor
NumDays::NumDays(double h)
{
	hours = h;
	days = hours * 1 / 8;
}


// Mutator Functions
void NumDays::setHours(double h)
{
	hours = h;
}


// Accessor Functions
double NumDays::getHours()
{
	return hours;
}

double NumDays::getDays()
{
	NumDays::calcDays();
	return days;
}


// Overloaded operator functions
NumDays NumDays::operator + (const NumDays &a)	// Addition
{
	NumDays temp;
	temp.hours = this->hours + a.hours;
	return temp;
}

NumDays NumDays::operator - (const NumDays &a)	// Subtraction
{
	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;
}


NumDays.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#ifndef NUMDAYS_H
#define NUMDAYS_H
#include <iostream>
#include <math.h>
using namespace std;

class NumDays
{
private:
	double hours, days;
	void calcDays();
public:
	// Constructors
	NumDays();
	NumDays(double);

	// Mutator Functions
	void setHours(double);

	// Accessor Functions
	double getHours();
	double getDays();

	// Overloaded operator functions
	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


main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <iostream>
#include "NumDays.h"
using namespace std;

int main()
{
	double hours1, hours2;

	// Retrieve input from user on number of hours worked
	cout << "Enter the number of hours to be placed in A: ";
	cin >> hours1;
	cout << "Enter the number of hours to be placed in B: ";
	cin >> hours2;

	// Define two objects of WorkHours
	NumDays first(hours1), second(hours2);

	cout << "A: " << first.getDays() << " day(s)" << endl;
	cout << "B: " << second.getDays() << " day(s)" << endl << endl;

	// Demonstrate addition and subtraction operators
	cout << "A + B = C: " << (first + second).getDays() << " day(s)" << endl;
	cout << "A - B: " << (first - second).getDays() << " day(s)" << endl << endl;

	// Define a third and fourth object to be used for further operator demonstrations
	NumDays third(first + second), fourth;

	// Demonstrate increment and decrement operators
	cout << "C: " << third.getDays() << " day(s)" << endl;
	fourth = third++;
	cout << "D = C++ " << endl;
	cout << "D: " << fourth.getDays() << " day(s)" << endl << endl;

	cout << "C: " << third.getDays() << " day(s)" << endl;
	fourth = ++third;
	cout << "D = ++C: " << endl;
	cout << "D: " << fourth.getDays() << " day(s)" << endl << endl;

	cout << "C: " << third.getDays() << " day(s)" << endl;
	fourth = third--;
	cout << "D = C--: " << endl;
	cout << "D: " << fourth.getDays() << " day(s)" << endl << endl;
	
	cout << "C: " << third.getDays() << " day(s)" << endl;
	fourth = --third;
	cout << "D = --C: " << endl;
	cout << "D: " << fourth.getDays() << " day(s)" << endl;

	system("pause");
	return 0;
}


Here is my output:

Enter the number of hours to be placed in A: 20
Enter the number of hours to be placed in B: 8
A: 2.5 day(s)
B: 1 day(s)

A + B = C: 3.5 day(s)
A - B: 1.5 day(s)

C: 3.5 day(s)
D = C++
D: 3.5 day(s)

C: 3.625 day(s)
D = ++C:
D: 3.75 day(s)

C: 3.75 day(s)
D = C--:
D: 3.625 day(s)

C: 3.625 day(s)
D = --C:
D: 3.5 day(s)
Press any key to continue . . .

As you can see, everything is working except for the prefix ++ operator. Help! - Sarah
Nevermind! I'm a dummy and was looking at the wrong increment section. I fixed it!
Topic archived. No new replies allowed.