Help with Overloading Unary Operators

Here are the details of the assignment:
I am to write a program with a class Worker that has 3 private data members (int age, int yrsService, and string jobTitle).
Then there is to be 2 objects of Worker type; Jones and Smith.
Data members initialized are as follows: 25, 3, Sales and 37, 10, President.

The displayed results should look like:
Age: 25
Service: 3
Job: Sales
Age: 37
Service: 10
Job: President
Age: 26
Service: 4
Job: Sales
Age: 38
Service: 11
Job: President
Age: 27
Service: 5
Job: Sales
Age: 39
Service: 12
Job: President

I'm not sure where to go from this point. Is overloading unary operators basically just a counter increment?

Edit: Almost forgot my code...

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
#include<iostream>
#include<string>

using namespace std;

class Worker
{
public:
	void printData() const
	{
		cout << "Age: " << age << endl;
		cout << "Years of Service: " << yrsService << endl;
		cout << "Job Title: " << jobTitle << endl;
	}

private:
	int age, yrsService;
	string jobTitle;
};

int main()
{

	return 0;
}
Last edited on
no suggestions?
See if you can work with this:

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
#include<iostream>
#include<string>

using namespace std;

class Worker
{
public:
    void setAge(int someAge){
               _age = someAge;
         }
    void setYrsService(int yrsService){
             _yrsService = yrsService;
         }
    void setJobTitle(string jobTitle){
               _jobTitle = jobTitle;
         }
    void printData(Worker xName){
        cout << "Age: " << _age << endl;
        cout << "Years of Service: " << _yrsService << endl;
        cout << "Job Title: " << _jobTitle << endl;
    }

private:
	int _age, _yrsService;
	string _jobTitle;
};

int main()
{
    Worker Jones;
    Jones.setAge(25);
    Jones.setYrsService(4);
    Jones.setJobTitle("Some Title");
    Jones.printData(Jones);
    
    
	return 0;
}
Last edited on
Thank you... I'll see what I can do... is this used basically as counter ++?
alright.... here is what I have now.... but I'm getting a bunch of errors... (I'm figuring that I'm overloading incorrectly for one) but "Worker" no appropriate default constructor available. Not sure what that is all about... errors are on lines 55, 62 (multiple errors), 71 (multiple errors), and line 64. here is my code.

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
#include<iostream>
#include<string>

using namespace std;

class Worker
{
public:
	void printData(Worker name) const
	{
		cout << "Age: " << age << endl;
		cout << "Years of Service: " << yrsService << endl;
		cout << "Job Title: " << jobTitle << endl;
	}
	void setAge(int theAge)
	{
		age = theAge;
	}
	void setYrsService(int serviceYrs)
	{
		yrsService = serviceYrs;
	}
	void setJobTitle(string job)
	{
		jobTitle = job;
	}

	int getAge() const
	{
		return age;
	}
	int getYrsService() const
	{
		return yrsService;
	}
	string getJobTitle() const
	{
		return jobTitle;
	}

	Worker(int theAge, int serviceYrs, string job)
	{
		age = theAge;
		yrsService = serviceYrs;
		jobTitle = job;
	}

private:
	int age, yrsService;
	string jobTitle;
};

int main()
{
	Worker Jones;
	Jones.setAge(25);
	Jones.getAge();
	Jones.setYrsService(3);
	Jones.getYrsService();
	Jones.setJobTitle("Sales");
	Jones.getJobTitle();
	Jones.printData++();

	Worker Smith;
	Smith.getAge();
	Smith.setYrsService(10);
	Smith.getYrsService();
	Smith.setJobTitle("President");
	Smith.getJobTitle();
	Smith.printData++();

	return 0;
}
1
2
3
4
5
6
Worker(int theAge, int serviceYrs, string job)
	{
		age = theAge;
		yrsService = serviceYrs;
		jobTitle = job;
	}

I think that's gonna be a problem.

And
Jones.printData++();
Will not work.

Also
Jones.printData++(); , what data are you printing? Your function expects an argument but
"0 provided."
Last edited on
You don't need the set() and get() accessors. In fact, I think the point of this assignment is to modify the values only through the constructor and ++ operator.

You had the right idea with your first post. Go back to that. Add the constructor, it's fine.

Now you also need to add the ++ operator:
1
2
3
4
Worker &operator++()
{
    // add code here
}


With these changes you can define a Worker like this:
Worker dave(51, 29, "Software Architect");
print out his stats like this:
dave.printData();
and add a year of service like this:
++dave;

ok thanks for the input... I'll try that now and post what I end up with...
So it has now started to actually print the data I declared... but will not increment.

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
class Worker
{
public:
	void printData() const
	{
		cout << "Age: " << age << endl;
		cout << "Years of Service: " << yrsService << endl;
		cout << "Job Title: " << jobTitle << endl;
	}
	Worker(int theAge, int serviceYrs, string job)
	{
		age = theAge;
		yrsService = serviceYrs;
		jobTitle = job;
	}
	Worker &operator++(int)
	{
		age++;
		yrsService++;

		return *this;
	}
	
private:
	int age, yrsService;
	string jobTitle;
};

int main()
{
	Worker Jones(25, 3, "Sales");
	Jones.printData();
	Jones++;

	Worker Smith(37, 10, "President");
	Smith.printData();
	Smith++;
	
	return 0;
}


If I put ++Jones I get an error. I'm thinking my code isn't doing anything in my operator constructor or the return value is incorrect.
The code you posted doesn't print the worker data after incrementing. When I add Jones.printData(); after line 33 it works as expected.
Wow! LOL.... yeah... you're correct. That made it work perfectly. Thanks for all the help. I can't believe it took me this long to do this assignment. Again. I truly appreciate it. Here is the final working code to the exact specifications of what the output is supposed to look like:

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
#include<iostream>
#include<string>

using namespace std;

class Worker
{
public:
	void printData() const
	{
		cout << "Age: " << age << endl;
		cout << "Service: " << yrsService << endl;
		cout << "Job: " << jobTitle << endl;
	}
	Worker(int theAge, int serviceYrs, string job)
	{
		age = theAge;
		yrsService = serviceYrs;
		jobTitle = job;
	}
	Worker &operator++()
	{
		age++;
		yrsService++;

		return *this;
	}
	
private:
	int age, yrsService;
	string jobTitle;
};

int main()
{
	Worker Jones(25, 3, "Sales");
	Jones.printData();
	Jones++;

	Worker Smith(37, 10, "President");
	Smith.printData();
	++Smith;

	Jones.printData();
	Smith.printData();

	Jones++;
	Smith++;
	Jones.printData();
	Smith.printData();
	
	return 0;
}
Note that you could do it in a loop too. This leaves the workers incremented but the output is correct.
1
2
3
4
5
6
7
8
Worker Jones(25, 3, "Sales");
Worker Smith(37, 10, "President");
for (int i=0; i<3; ++i) {
	Jones.printData();
	Smith.printData();
	Jones++;
	Smith++;
}
This is true with the conditions to only loop two times. With your help, this project is now complete. I'm on to something else now. Populating an integer array from a .txt file, then doing a sequential search to let the user guess a number 1-500 with only 20 numbers (that have been pre-selected and wrote to the file already) and keeps looping until a number is guessed or -1 is entered. Its a radio station simulation and outputs the winning number, the location the number was found, and the number of callers. So far I've gotten the file to populate the array and my function to search the array for the number and return the location.

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
#include<iostream>
#include<fstream>

using namespace std;

int sequenSrch();

int main()
{
	cout << "This program simulates a radio station that asks the caller to guess a number.\n";
	cout << "The number is compared against a list of 20 numbers between 1 and 500 inclusive.\n";
	cout << "The contest is held until a number has been matched or a value of - 1 is entered.\n";
	cout << "A message is displayed containing the winning number, the location in the list of numbers,";
	cout << " the number of calls made, and the amount of the prize.";

	int num = 20;
	int size = 0;
	int first, x;
	int* prizeArray = new int[num];

	ifstream prizeList;
	
	prizeList.open("prizeList.txt");
	while (prizeList >> x)
	{
		if (size == num)
		{
			int* newPrizeArray = new int[2 * num];

			for (first = 0; first < size; first++)
			{
				newPrizeArray[first] = prizeArray[first];
			}
			delete[] prizeArray;
			prizeArray = newPrizeArray;
			num *= 2;
		}
		prizeArray[size] = x;
		size++;
	}
	prizeList.close();



	return 0;
}

int sequenSrch(int newPrizeArray[20], int value)
{
	int i = 1, location = 0;
	bool found = false;

	while (!found && i < newPrizeArray[20])
	{
		if (newPrizeArray[i] == value)
		{
			found = true;
			location = i;
		}
		i++;
	}
	return location;
}
Lookin' good so far.

Why are you using a dynamically expanding array? The size is fixed and your other code (like sequenSrch) assumes the array is 20 items.

sequenSrch() isn't quite right. After finding the entry it increments i before breaking out of the loop. Also what should it return if the item isn't found? Right now it will return size. And the terminating condition isn't right: you're ending when i < newPrizeArray[20]. I think you want to end when i<20.
well originally I was not sure of how many integers were in the file, so I was having the program find the number of integers in the file and making the array of that size then storing them in the array (in the order that they are in the file... I already did a program that sorts them in ascending order).

should it not increment i before breaking out of the loop? I added...

1
2
3
4
5
6
7
8
9
int guess()
{
	int guess;

	cout << "Hello caller. What number between 1 and 500 are you guessing?\n";
	cin >> guess;

	return guess;
}


obviously to get the guess of the "caller"
I also added...

 
cout << guess() << " is not in the list. Call again.\n";


just before i++ in sequenSrch()...
I was thinking that the loop would return 0 if integer is not found... is it not doing this?
http://www.cplusplus.com/forum/general/150964/

thats the link to my new post if you are interested....
Topic archived. No new replies allowed.