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.