I need help with Unary Operators (overloading)
Dec 11, 2014 at 7:53pm UTC
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?
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;
}
Dec 12, 2014 at 2:09am UTC
nobody has any suggestions?
Dec 12, 2014 at 3:56am UTC
getting errors... but here is what I have come up with so far...
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;
}
Dec 12, 2014 at 7:42am UTC
Dec 12, 2014 at 6:56pm UTC
I had read this previously but I'm still confused on how to use the operater++ and constructor to accomplish an increment of age and yrsService.
Topic archived. No new replies allowed.