über Noob

Just trying to complete an assignment by tomorrow without knowing anything. I have copy pasted a similar program from a previous assignment and tried to alter it. I have got pretty close I think but my trial and error method seems to be no match for this damn overloaded function.

The question is:

Consider the following structure used to keep employee records:
struct Employee
{
string firstName;
string lastName;
float salary;
}
Turn the employee record into a class type rather than a structure type. The employee record
class should have private member variables for all the data. Include public member functions for
each of the following:
• a default constructor that sets the employee‘s first name and last name to a blank string, and
his annual salary to 0;
• an overloaded constructor that sets the member variables to specified values;
• member functions to set each of the member variables to a value given as an argument to
the function (i.e. mutators);
• member functions to retrieve the data from each of the member variables (i.e accessors);
Embed your class definition in a test program. The test program should create two Employee
objects and display each object’s annual salary. Use the overloaded constructor to initialise one of
the Employee records as Joe Soap with a annual salary of R1456.00. Obtain the following input
values for the other Employee record from the keyboard:
Joanne Soape
R15446.66
Now give each employee a 10% raise and display each Employee object’s annual salary again.

What I have 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#include <iostream>
#include <string>
using namespace std;

class Employee
{
public:
   Employee();
   Employee(string firstName, string lastName, float salary);
   void setFirstName(string n1);
   void setLastName(string n2);
   void setSalary(float s);
   string getFirstName()const;
   string getLastName()const;
   float getSalary()const;
   float calcSalary()const;
   private:
    string firstName;
    string lastName;
    float salary;
    string n1;
    string n2;
    float s;
};

Employee::Employee()
{
    firstName = "";
    lastName = "";
    salary = 0;
} 

Employee Employee2;
    string n1 = "Joe", n2 = "Soap";
    float s = 1456.00;
    
    Employee::Employee(string firstName, string lastName, float salary)
{
    firstName = n1;
    lastName = n2;
    salary = s;
} 

void Employee::setFirstName(string n1)
{
    firstName = n1;
}

void Employee::setLastName(string n2)
{
    lastName = n2;
}

void Employee::setSalary(float s)
{
    salary = s;
}

string Employee::getFirstName()const 
{
    return firstName;
}

string Employee::getLastName()const 
{
    return lastName;
}

float Employee::getSalary()const 
{
    return salary;
}

float Employee::calcSalary()const 
{
    return float((salary*0.1)+salary);
}
     
    int main()

{ 
    
    cout << "Employee name: " << Employee2.getFirstName() << endl;
    cout << "Employee salary: " << Employee2.getSalary() << endl;

    Employee Employee1;
    
    cout << "Please enter employee's name: ";
    getline(cin, n1);
    getline(cin, n2);
    Employee1.setFirstName(n1);
    Employee1.setLastName(n2);
    cout << "Enter salary: ";
    cin >> s;
         
    cout << "Employee name: " << Employee1.getFirstName() << " " << Employee1.getLastName() << endl;
    cout << "Salary including raise: " << Employee1.calcSalary() << endl;
    cout << "Employee name: " << Employee2.getFirstName() << " " << Employee2.getLastName() << endl;
    cout << "Salary including raise: " << Employee2.calcSalary() << endl;
    return 0;
}


Extra info:
If you are planning on slating me for not doing this properly please read on...
I am doing Intro to Programming I and II concurrently and have only managed to get through 3/4 of I but am already having to do Assignment 2 of II. All I have covered so far is basic stuff like int, char, string, if loops and not much more. It is a miracle this thing compiles at all.
Last edited on
Do you have a question?
Your overloaded function needs to look something like this:
1
2
3
4
5
Employee::Employee(string firstName, string lastName, float salary) {
	this->firstName=firstName;
	this->lastName=lastName;
	this->salary=salary;
}


Moschops wrote:
Do you have a question?

I also thought the same thing at first then saw:
RegionX wrote:
I have got pretty close I think but my trial and error method seems to be no match for this damn overloaded function

@RegionX: Please in future make your questions clearer and use code tags

Last edited on
Sorry, if you compile and run you will see that the output is wrong. It is supposed to list Employee1 name and salary then Employee2 Name and salary are entered by the user and the new calcSalary should be displayed.

My output is:

Employee name:
Employee salary:0
Please enter employee's name: Joanne
Soap
Please enter employees salary: 15446.66
Employee name: Joanne Soap
Salary including raise:0
Employee name:
Salary including raise:0



It should be:

Employee name: Joe Soap
Employee salary: 1456.00
Please enter employee's name: Joanne
Soap
Please enter employees salary: 15446.66
Employee name: Joanne Soap
Salary including raise: 16991.326
Employee name: Joe Soap
Salary including raise: 1601.6


Script Coder wrote:

Your overloaded function needs to look something like this:
1
2
3
4
5
Employee::Employee(string firstName, string lastName, float salary) {
	this->firstName=firstName;
	this->lastName=lastName;
	this->salary=salary;
}



Are you saying this is incorrrect?

1
2
3
4
5
6
Employee::Employee(string firstName, string lastName, float salary)
{
    firstName = n1;
    lastName = n2;
    salary = s;
} 


Basically it wont assign any values to firstName, lastName or salary and I'm not sure what else to try. I assume it has something to do with the overloaded function but it looks like it has the same format as your example.
Last edited on
RegionX wrote:
Are you saying this is incorrrect?

Yes, I am saying it is very incorrect, and I would recommend that you re-read or find another tutorial. (All of the above is not intended in any way to be rude)
OPs assignment wrote:
• member functions to set each of the member variables to a value given as an argument to
the function (i.e. mutators);
• member functions to retrieve the data from each of the member variables (i.e accessors);


It makes me cry a little that this is how they teach OOP in schools.

No wonder so many people struggle with it.
Last edited on
Well I now get the correct output for first and last name as well as the salary for Employee1 or Joanne Soap which means I have mutators and accesssors right for this... right? I tried the overloaded function as you suggested with absolutely no change to the output.

I guess I will just move on and hope to get a few marks. Thanks
Great Success, I got it working. final code below:
Just to give you some further insight in to my troubles you should know that for a long time I have a report window, I worked out how to get it. Unfortunately in the line column it just says error so there is no way to know exactly where I have gone wrong. I think it is because I have W7 Starter edition. Any thoughts on why it works even with as Script Coder put it:

Script Coder wrote:
Yes, I am saying it is very incorrect, and I would recommend that you re-read or find another tutorial. (All of the above is not intended in any way to be rude)


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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#include <iostream>
#include <string>
using namespace std;

class Employee
{
public:
   Employee();
   Employee(string n1, string n2, float s);
   void setFirstName(string n1);
   void setLastName(string n2);
   void setSalary(float s);
   string getFirstName()const;
   string getLastName()const;
   float getSalary()const;
   float calcSalary()const;
   string n1;
   string n2;
   float s;
private:
    string firstName;
    string lastName;
    float salary;
    };

Employee::Employee()
{
    firstName = "";
    lastName = "";
    salary = 0.00;
} 

 Employee::Employee(string n1, string n2, float s)
{
    firstName = n1;
    lastName = n2;
    salary = s;
} 

    
void Employee::setFirstName(string n1)
{
    firstName = n1;
}

void Employee::setLastName(string n2)
{
    lastName = n2;
}

void Employee::setSalary(float s)
{
    salary = s;
}

string Employee::getFirstName()const 
{
    return firstName;
}

string Employee::getLastName()const 
{
    return lastName;
}

float Employee::getSalary()const 
{
    return salary;
}

float Employee::calcSalary()const 
{
    return float((salary*0.1)+salary);
}
     
    int main()

{ 
   Employee Employee2;
   
         string n1 = "Joe";    
         string n2 = "Soap";    
         float s = 1456.00;
         Employee2.setFirstName(n1);
         Employee2.setLastName(n2);
         Employee2.setSalary(s);
          
    cout << "Employee name: " << Employee2.getFirstName() << endl;
    cout << "Employee salary: " << Employee2.getSalary() << endl;

    Employee Employee1;
    
    cout << "Please enter employee's name: ";
    getline(cin, n1);
    getline(cin, n2);
    Employee1.setFirstName(n1);
    Employee1.setLastName(n2);
    cout << "Enter salary: ";
    cin >> s;
    Employee1.setSalary(s);
         
    cout << "Employee name: " << Employee1.getFirstName() << " " << Employee1.getLastName() << endl;
    cout << "Salary including raise: " << Employee1.calcSalary() << endl;
    cout << "Employee name: " << Employee2.getFirstName() << " " << Employee2.getLastName() << endl;
    cout << "Salary including raise: " << Employee2.calcSalary() << endl;
    return 0;
}
Last edited on
Topic archived. No new replies allowed.