This program is supposed to prompt the user for two different employees and print their name, last name, ssn, payrate, and total pay for the week. Also it should determine if the worker worked any over time hours and recalculate their pay if they do.
I keep getting this error message:
No matching function call to Employee::Employee()
I will mark the lines which this message appears with a // *****.
The message appears 3 times. Please review my issues and help me fix them, and also explain what you did to fix them.
#include <cstdlib>
#include <iostream>
#include <math.h>
/*
Name: Employee2
Author: --------
Date: 20/10/14 20:36
Description: A program that prints workers info using classes
*/
using namespace std;
class Employee
{
private:
string firstname, lastname, ssn;
int payRate, hours;
public:
//A Four Parameter Constructor
Employee (string newFirst, string newLast, string newSsn, int newpayRate, int newHours)
{
firstname = newFirst;
lastname = newLast;
ssn = newSsn;
payRate = newpayRate;
hours = newHours;
}
//Setter or Mutator Functions
void setnewFirst(string newFirst)
{
firstname = newFirst;
}
void setnewLast(string newLast)
{
lastname = newLast;
}
void setnewSsn(string newSsn)
{
ssn = newSsn;
}
void setnewpayRate(int newpayRate)
{
payRate = newpayRate;
}
void setnewHours(int newHours)
{
hours = newHours;
}
void setEmployee(string newFirst, string newLast, string newSsn, int newpayRate, int newHours)
{
firstname = newFirst;
lastname = newLast;
ssn = newSsn;
payRate = newpayRate;
hours = newHours;
}
//Accessor Functions
string getfirstname ()
{
return firstname;
}
string getlastname ()
{
return lastname;
}
string getssn ()
{
return ssn;
}
int getpayRate ()
{
return payRate;
}
int gethours ()
{
return hours;
}
//Output Functions
void printEmployee ()
{
cout << firstname << " " << lastname << endl << ssn << endl << payRate << endl << hours << endl;
}
//Functions to use employee info
Employee newEmployee ()
{
Employee e1; //**************
string newFirst;
string newLast;
string newSsn;
int newpayRate;
int newHours;
cout << "Enter First Name: " ;
cin >> newFirst;
cout << "Enter Last Name: " ;
cin >> newLast;
cout << "Enter SSN: " ;
cin >> newSsn;
cout << "Enter Payrate: " ;
cin >> newpayRate;
cout << "Enter Hours Worked: " ;
cin >> newHours;
e1.setnewFirst(newFirst);
e1.setnewLast(newLast);
e1.setnewSsn(newSsn);
e1.setnewpayRate(newpayRate);
e1.setnewHours(newHours);
return e1;
}
//Function to Calculate Weekly Pay
int calculatePay (int hours)
{
double result;
if ( hours > 40 )
{
result = (hours - 40) * (payRate * 1.5) + (40 * payRate);
}
else
{
result = (hours * payRate);
}
}
};
Employee newEmployee();
//Main
int main(int argc, char *argv[])
{
Employee firstEmployee; // *********************
Employee secondEmployee; // *********************
double result;
firstEmployee = firstEmployee.newEmployee();
secondEmployee = secondEmployee.newEmployee();
cout << "First Employee Pay: " ;
firstEmployee.printEmployee();
cout << endl;
cout << "Secnod Employee Pay: " ;
secondEmployee.printEmployee();
system("PAUSE");
return EXIT_SUCCESS;
}
A constructor is intended to operate on the space in which the new object is being created. This means you don't need to complicate things by creating the additional object e1. Remove the problematic line:
Employee e1; //**************
Remove all e1 references from your code; for example change
e1.setnewFirst(newFirst);
e1.setnewLast(newLast);
e1.setnewSsn(newSsn);
e1.setnewpayRate(newpayRate);
e1.setnewHours(newHours);
to:
setnewFirst(newFirst);
setnewLast(newLast);
setnewSsn(newSsn);
setnewpayRate(newpayRate);
setnewHours(newHours);
Constructors do not return values; remove:
return e1;
Compile, test, debug, rinse, and repeat.
Related
I'm trying to write a C++ code for a course I'm enrolled in, where I keep the information of the students enrolled in the course.
I should be able to add a student to the classrrom in the user interface written in main , by calling the function void addNewStudent(int ID, string name, string surname), where I create my object instances, Student, and Course inside the function.
I should also be able to search by given ID by calling the function void showStudent(int ID) in the main, where the function uses the getStudent(ID) method of the object of the classCourse
I did not write all the methods, but when I try to debug this code, I got the error " Exception has occured, unknown signal error."
My questions are:
What is the reason of this error? How can I fix it?
Suppose that the user interface in the main is necessary to use as well as the functions it calls. Do I have to create a class object again inside each function as I wrote?
Can a more effective implementation be made in accordance with the object oriented principles I have defined above?
#include <iostream>
using namespace std;
#define MAX 10
class Student {
private:
int ID;
string name;
string surname;
public:
Student()
{
ID = 0;
string name = "" ;
string surname = "";
}
void setID(int ID_set);
int getID();
void setName(string name_set);
string getName();
void setSurName(string surname_set);
string getSurName();
};
class Course {
private:
Student students[MAX];
int num =0 ; // The current number of students in the course, initially 0.
float weightQ;
float weightHW;
float weightF;
public:
Course()
{
students[num] = {};
weightQ = 0.3;
weightHW = 0.3;
weightF = 0.4;
}
int getNum(); // Returns how many students are in the course
void addNewStudent(Student new_student);
void updateWeights(float weightQ_update, float weightHW_update, float weightF_update);
void getStudent(int ID_given);
};
// Method declerations for the class Student
void Student :: setID(int ID_set){
ID = ID_set;
}
int Student :: getID(){
return ID;
}
void Student :: setName(string name_set){
name = name_set;
}
string Student :: getName(){
return name;
}
void Student :: setSurName(string surname_set){
surname = surname_set;
}
string Student :: getSurName(){
return surname;
}
// Method declerations for the class Course
int Course :: getNum(){
return num;
}
void Course :: addNewStudent(Student new_student){
students[num] = new_student ;
num = num + 1;
}
void Course :: updateWeights(float weightQ_update, float weightHW_update, float weightF_update){
weightQ = weightQ_update;
weightHW = weightHW_update;
weightF = weightF_update;
}
void Course :: getStudent(int ID_given){
for(int i = 0; i<MAX; i++){
if(ID_given == students[i].getID()){
cout << "Student Name & Surname : " << students[i].getName() << " " << students[i].getSurName()<<"\n";
}
}
}
void addNewStudent(int ID, string name, string surname){
Student student;
Course ECE101;
student.setID(ID);
student.setName(name);
student.setSurName(surname);
ECE101.addNewStudent(student);
}
void showStudent(int ID){
Course ECE101;
ECE101.getStudent(ID);
}
int main(){
Course ECE101;
cout << "Welcome to the ECE101 Classroom Interface"<<"\n";
cout << "Choose your option\n";
string option_1 = "1) Add a student ";
string option_2 = "2) Search a student by ID";
cout << "Enter your option: ";
int x;
int ID;
string name, surname;
cin >> x;
if (x == 1)
cout << "Enter the student ID ";
cin >> ID;
cout << endl;
cout << "Enter the student name ";
cin >> name;
cout << endl;
cout << "Enter the student surname " ;
cin >> surname;
addNewStudent(ID, name, surname);
return 0;
}
To make the menu more interactive you could add a do while statement that would accept 3 options:
register
show data
exit
int main(){
Course ECE101;
int x;
int ID;
string name, surname;
string option_1 = "1) Add a student\n";
string option_2 = "2) Search a student by ID\n";
cout << "Welcome to the ECE101 Classroom Interface\n";
cout << "Choose your option\n";
cout << option_1 << option_2;
cin >> x;
do {
if (x == 1) {
cout << "Enter the student ID ";
cin >> ID;
cout << endl;
cout << "Enter the student name ";
cin >> name;
cout << endl;
cout << "Enter the student surname " ;
cin >> surname;
addNewStudent(ID, name, surname, ECE101);
}
else {
cout << "Enter the student ID\n";
cin >> ID;
showStudent(ID, ECE101);
}
cout << "Choose your option\n";
cin >> x;
} while(x != 3);
return 0;
}
addnewStudent() and showStudent() methods now accepts an instance of Course as an argument to be able to add students.
void addNewStudent(int ID, string name, string surname, Course &course) {
Student student;
student.setID(ID);
student.setName(name);
student.setSurName(surname);
course.addNewStudent(student);
}
void showStudent(int ID, Course &course) {
course.getStudent(ID, course);
}
the function is modified from the same class as well.
void Course::getStudent(int ID_given, Course &course) {
for(int i = 0; i<MAX; i++){
if(ID_given == students[i].getID()){
cout << "Student Name & Surname : " << students[i].getName() << " " << students[i].getSurName()<<"\n";
}
}
}
Demo
Your addNewStudent function creates a new course everytime it is called. You could pass a reference to the course as a parameter into the function and call Course.addNewStudent(student). You'll want to make sure you specify it's a reference though when you define your function or you'll just create a copy of the course.
I have been tasked with creating a program that takes a users name / date of birth, and prints back the persons name / date of birth, age and their target heart rate.
It is driving me crazy as I can get the program to execute, but it gives me crazy answers, and I've no indication as to why.
Any advise would be greatly appreciated. Please find the code for my main.cpp / HeartRates.cpp and HeartRates.h (also attached is a screenshot of my current output).
main.cpp:
#include <iostream>
#include "HeartRates.h"
using namespace std;
int main() {
int d, m, y;
string firstName, lastName;
HeartRates heart;
cout << "Please enter your first name: ";
cin >> firstName;
heart.setFirstName(firstName);
cout << "Please enter your last name: ";
cin >> lastName;
heart.setLastName(lastName);
cout << "What year were you born: ";
cin >> y;
heart.setYearOfBirth(y);
cout << "What month were you born on: ";
cin >> m;
heart.setMonthOfBirth(m);
cout << "What day were you born on: ";
cin >> d;
heart.setDayOfBirth(d);
int age = heart.getAge();
int maxHR = heart.getMaxHeartRate(age);
heart.displayHeartRates(age);
heart.getTargetHeartRate(maxHR);
}
HeartRates.h:
#include <string>
class HeartRates {
public:
explicit HeartRates();
HeartRates(std::string firstname, std::string lastname, int day, int month, int year);
void setFirstName(std::string); //set 1st name
void setLastName(std::string); //set 2nd name
void setDayOfBirth(int); //set 'day' of birth
void setMonthOfBirth(int); //set 'month' of birth
void setYearOfBirth(int); //set 'year' of birth
std::string getFirstName() const; //get 1st name
std::string getLastName() const; //get last name
int getDayOfBirth() const; //get Day of Birth
int getMonthOfBirth() const; //get Month of Birth
int getYearOfBirth() const; //get Year of Birth
int getAge(); //Function to get and return age
int getMaxHeartRate(int); //Function to get max heart rate
void getTargetHeartRate(int); //Function to get target heart rate
void displayHeartRates(int); //Function to display heart rate
public:
std::string firstName;
std::string lastName;
int dayOfBirth;
int monthOfBirth;
int yearOfBirth;
};// terminate
HeartRates.cpp:
#include <iostream>
#include "HeartRates.h"
using namespace std;
// default constructor
HeartRates::HeartRates() {
std::string firstName = "unknown";
std::string lastName = "unknown";
int dayOfBirth = 0;
int monthOfBirth = 0;
int yearOfBirth = 0;
}// end default constructor
//2nd constructor
HeartRates::HeartRates(std::string firstname, std::string lastname, int day, int month, int year) {
string setFirstName(firstname);
string setLastName(lastname);
int setDayOfBirth(day);
int setMonthOfBirth(month);
int setYearOfBirth(year);
}// end 2nd constructor
//1st name function
void HeartRates::setFirstName(string firstname) {
string firstName = firstname;
}//end 1st name function
//last name function
void HeartRates::setLastName(string lastname) {
string lastName = lastname;
}//end last name function
// set day of birth
void HeartRates::setDayOfBirth(int day) {
int dayOfBirth = day;
}//end day of birth function
//set month of birth
void HeartRates::setMonthOfBirth(int month) {
int monthOfBirth = month;
}//end month of birth function
//set year of birth
void HeartRates::setYearOfBirth(int year) {
int yearOfBirth = year;
}//end year of birth
// get 1st name function
string HeartRates::getFirstName() const {
return firstName;
}// end 1st name function
// get last name function
string HeartRates::getLastName() const {
return lastName;
}// end last name function
// get day of birth function
int HeartRates::getDayOfBirth() const {
return dayOfBirth;
}//end day of birth function
// get month of birth function
int HeartRates::getMonthOfBirth() const {
return monthOfBirth;
}//end month of birth function
// get year of birth function
int HeartRates::getYearOfBirth() const {
return yearOfBirth;
}//end year of birth function
//Calculate age function
int HeartRates::getAge() {
int age;
int d;
int m;
int y;
cout << "enter current year: \n" << endl;
cin >> y;
cout << "enter current month: \n" << endl;
cin >> m;
cout << "Enter current day: \n" << endl;
cin >> d;
if (getMonthOfBirth() < m) {
m = m - getMonthOfBirth();
} else {
m = getMonthOfBirth() - m;
}
if (getDayOfBirth() < d) {
d = d - getDayOfBirth();
} else {
d = getDayOfBirth() - d;
}
age = y - getYearOfBirth();
return age;
} //end calculate age function
//calculate max heart rate function
int HeartRates::getMaxHeartRate(int age) {
//220 - age
int maxHR = 220 - age;
return maxHR;
}//end max heart rate function
//calculate target heart rate function
void HeartRates::getTargetHeartRate(int maxHR) {
// target heart rate is between 50% & 85% of maximum heart rate
cout << "Your target heart rate is between " << maxHR * 0.5 << " and " << maxHR * 0.85 << "bpm. ";
}//end of target heart rate function
// display info function
void HeartRates::displayHeartRates(int age)
{
int a = age;
cout << "Hello " << getFirstName() << " " << getLastName() << endl;
cout << "Your DOB is " << getDayOfBirth() << "/" << getMonthOfBirth() << "/" << getYearOfBirth() <<
endl;
cout << "Your age is " << a << "years old" << endl;
} // end display info function
Remove the type with this in the setters.
for example:
void HeartRates::setDayOfBirth(int day) {
int dayOfBirth = day;
}//end day of birth function
int dayOfBirth = day; make a new variable with the name of dayOfBirth and not update the field of the class.
To update the field of the class you don't need to write the type of the variable.
like that: (It is fine to add this)
void HeartRates::setDayOfBirth(int day) {
this->dayOfBirth = day;
}//end day of birth function
Accessing member variables
You seem to confuse variable declaration with assigning values to member variables.
void HeartRates::setMonthOfBirth(int month) {
int monthOfBirth = month;
}
In the above, you declare a local variable named monthOfBirth, assign the value of the variable month to it and at the end of the method scope, it gets destroyed and nothing else happens. You need the following instead:
void HeartRates::setMonthOfBirth(int month) {
monthOfBirth = month;
// or
this->monthOfBirth = month;
}
Constructing your object
Your constructor does not initialize the member variables at all. It should be like this:
HeartRates::HeartRates(): firstName("unknown"), lastName("unknown"), dayOfBirth(0), monthOfBirth(0), yearOfBirth(0)
{}
For my project, my program has to read a file that looks like this: "Mary", "000111222", "Junior", 12, 4.0
In my main code it can read it, but only as strings only. I want it to read it as string,string, string, float, float. The getLine() method only works with strings. I tried other ways but it did not work. Any suggestions? The fields I want to be a float is gpa and credit. Any advice is appreciated! Thank you!
#include <iostream>
#include <string>
#include <iterator>
#include <iomanip>
#include <fstream>
#include <vector>
include <sstream>
#include <algorithm>
using namespace std;
class Student {
//declare local variables
protected:
string name; //people with names longer than 21 characters will just have
to make do
string ssn; // Social Secturity Number.
string gpa; //Most up to date gpa for the student
string credits; //Number of student's credit hours
//build public methods
public:
//Default Constructor
Student() {}
//Student constructor. Besides the character arrays, everything else is
passed by reference.
Student(const string n, const string s, string sGPA, string sCredits) {
name = n;
ssn = s;
gpa = sGPA;
credits = sCredits;
}
string getName() {
return name;
}
string getSSN() {
return ssn;
}
string getGPA() {
return gpa;
}
string getCredit() {
return credits;
}
//a function that is expected to be implemented and overridden by subclasses
virtual void print() const {
cout << '\n' << endl;
cout << "Student's name: " << name << endl;
cout << "Student SSN: " << ssn << endl;
cout << "Student's current GPA: " << gpa << endl;
cout << "Student's credit hours: " << credits << endl;
}
// a pure virtual function for implementation later. Makes whole class
Abstract
virtual float tuition() const = 0;
};
class Undergrad : public Student {
//declare local variables
protected:
float undergrad_rate = 380.0;
string year;
//build public methods
public:
//Default Constructor
Undergrad() {}
//Undergrad Constructor
Undergrad(const string n, const string s, string uGPA, string uCredits,
string y) :
Student(n, s, uGPA, uCredits), year(y) {}
//Display the contents of undergrad
void print() const {
Student::print();
cout << "Undergrad Rate: " << undergrad_rate << endl;
cout << "Year: " << year << endl;
}
//Display undergrad's current year
string get_year() {
return year;
}
//Display the undergrad's current rate
float get_rate() {
return undergrad_rate;
}
//Set a undergrad's current year
void set_year(string y) {
year = y;
}
//Display the cost for an undergrad to attend university
float tuition() const {
return 1000000;
}
};
int main() {
ifstream ip("data.txt");
if (!ip.is_open()) std::cout << "ERROR: File not found" << '/n';
string name;
string ssn;
string year;
string credit;
string gpa;
vector<Undergrad> file;
//Undergrad g(name, ssn, year, credit, gpa);
while (ip.good()) {
getline(ip, name, ',');
getline(ip, ssn, ',');
getline(ip, gpa, ',');
getline(ip, credit, ',');
getline(ip, year, '\n');
// float number = stoi(gpa);
//float number1 = stoi(credit);
Undergrad g(name, ssn, year, credit, gpa);
file.push_back(g);
}
ip.close();
Undergrad g = file.back();
file.pop_back();
file.insert(file.begin(),g);
for (int i = 0; i < file.size(); i++) {
cout << "Name: " << file[i].getName() << endl;
cout << "SSN: " << file[i].getSSN() << endl;
cout << "Year: " << file[i].get_year() << endl;
cout << "Credit: " << file[i].getCredit() << endl;
cout << "GPA " << file[i].getGPA() << endl;
cout << " " << endl;
}
system("pause");
return 0;
}
You can cast the string to a float using atof. See reference here. Be sure to include <cstdlib>. Your constructor would look like:
Student(const string n, const string s, string sGPA, string sCredits) {
name = n;
ssn = s;
gpa = (float)atof(sGPA.c_str());
credits = (float)atof(sCredits.c_str());
}
Throwing this out there first I'm a still learning how to program in school. I'm having an issue reading in to a dynamically created array with a pointer to one of my classes. The function readClassArray() isn't getting the variable back from student.getCreditNumber. The program complies fine in Visual Studio but when I get the the readClassArray it just skips over the function because s.getCreditNumber returns 0.
#ifndef STUDENT_H
#define STUDENT_H
#include <iostream>
using namespace std;
class Courses{
private:
int courseNumber;
double hours;
string courseName;
char grade;
public:
void setCourseNumber(int n){courseNumber = n; }
void setCreditHours(double c) { hours = c; }
void setCourseName(string n) { courseName = n; }
void setGrade(char g) { grade = g; }
int getCourseNumber() { return courseNumber; }
double getCreditHours() { return hours; }
string getCourseName() { return courseName; }
char getGrade() { return grade; }
};
class Student : public Courses{
private:
string firstName;
string lastName;
string studentNumber;
int creditNumber;
double gpa;
public:
Courses * courses;
Student() {
firstName = " ";
lastName = " ";
studentNumber = " ";
creditNumber = 0;
gpa = 0.0;
courses = NULL;
}
~Student() {
delete[] courses;
};
void setFirstName(string n) { firstName = n; }
void setLastName(string l) { lastName = l; }
void setStudentNumber(string a) { studentNumber = a; }
void setCreditNumber(int num) { creditNumber = num; }
string getFirstName() { return firstName; }
string getLastName() { return lastName; }
string getStudentNumber() { return studentNumber; }
int getCreditNumber() { return creditNumber; }
};
#endif
Student.cpp
#include "Student.h"
#include <iostream>
#include <string>
using namespace std;
void readStudent();
void readCourseArray();
void computeGPA();
void printSummary();
void readStudent() {
Student a;
string number;
string firstName;
string lastName;
int courses;
cout << "Enter student number: ";
cin >> number;
a.setStudentNumber(number);
cout << "Enter student first name: ";
cin >> firstName;
a.setFirstName(firstName);
cout << "Enter student last name: ";
cin >> lastName;
a.setLastName(lastName);
cout << "Enter student number of courses: ";
cin >> courses;
a.setCreditNumber(courses);
cout << "\n"; }
void readCourseArray(){
Student s;
s.courses = new Courses[s.getCreditNumber()];
int num;
double cHours;
string cName;
char grade;
cout << "test" << endl;
for (int i = 0; i < s.getCreditNumber(); i++){
cout << "Enter class " << i + 1 << " number: ";
cin >> num;
s.courses[i].setCourseNumber(num);
cout << "Enter class " << i + 1 << " name: ";
cin >> cName;
s.courses[i].setCourseName(cName);
cout << "Enter class " << i + 1 << " hours: ";
cin >> cHours;
s.courses[i].setCreditHours(cHours);
cout << "Enter class " << i + 1 << " grade: ";
cin >> grade;
s.courses[i].setGrade(grade);
cout << "\n";
}
}
At the start of readCourseArray you've created s. When that happens the value of the creditNumber member is 0 as set by the default constructor. You need to do something to set it to a non-zero value. If you're expecting the value set in readStudent to carry over you need to plumb the two functions together. Either pass in a Student object as a reference to each function, or have readStudent return a Student object and pass that to readCourseArray.
For class I have to adapt a program I wrote last week for polymorphism. Last week it used a specific set of information for the employees but now I have to make it work with polymorphism as well as read/write data from a file, I am completely lost with what I am supposed to be doing, If someone could even point me in the right direction it would be so much help. I can post my current .h and .cpp file for a look at what I have as well as the instructions of what I am supposed to be doing.
.h
#pragma once
#include <string>
using namespace std;
class Employee {
private:
int employeeNumber; // Employee's employee number
string employeeName; //Employee's name
string streetAddress; //Employee's street address
string phoneNumber; //Employee's phone number
double hourlyWage; //Employee's hourly wage
double hoursWorked; //Employee's hours worked
double netPay; //Net pay
double grossPay; //Gross pay
public:
Employee();
Employee(int, string, string, string, double, double);
int getEmployeeNumber();
void setEmployeeNumber(int);
string getEmployeeName();
void setEmployeeName(string);
string getStreetAddress();
void setStreetAddress(string);
string getPhoneNumber();
void setPhoneNumber(string);
double getHourlyWage();
void setHourlyWage(double);
double getHoursWorked();
void setHoursWorked(double);
double calcPay()
{
const int OVER = 40;
double federal = 0.20;
double state = 0.075;
double timeHalf = 1.5;
double grossPay;
double netPay;
if (getHoursWorked() < OVER)
{
grossPay = getHoursWorked() * getHourlyWage();
netPay = grossPay - (grossPay * federal) - (grossPay * state);
}
if (getHoursWorked() >= OVER)
{
grossPay = getHoursWorked() * ((getHourlyWage() * timeHalf));
netPay = grossPay - (grossPay * federal) - (grossPay * state);
}
return netPay;
}
};
.cpp
#include <iostream>
#include <string>
#include <fstream>
#include "Employee.h"
#include <iomanip>
using namespace std;
Employee::Employee()
{
employeeNumber = 0; // Employee's employee number
employeeName = ""; //Employee's name
streetAddress = ""; //Employee's street address
phoneNumber = ""; //Employee's phone number
hourlyWage = 0; //Employee's hourly wage
hoursWorked = 0;
grossPay = 0;
netPay = 0;
}
Employee::Employee(int empNum, string empName, string streetAddress,
string phoneNumber, double hourlyWage, double hoursWorked)
{
employeeNumber = empNum;
employeeName = empName;
this->streetAddress = streetAddress;
this->phoneNumber = phoneNumber;
this->hourlyWage = hourlyWage;
this->hoursWorked = hoursWorked;
grossPay = 0;
netPay = 0;
}
int Employee::getEmployeeNumber()
{
return employeeNumber;
}
void Employee::setEmployeeNumber(int empNum)
{
employeeNumber = empNum;
}
string Employee::getEmployeeName()
{
return employeeName;
}
void Employee::setEmployeeName(string empName)
{
employeeName = empName;
}
string Employee::getStreetAddress()
{
return streetAddress;
}
void Employee::setStreetAddress(string strtAddrs)
{
streetAddress = strtAddrs;
}
string Employee::getPhoneNumber()
{
return phoneNumber;
}
void Employee::setPhoneNumber(string phnNum)
{
phoneNumber = phnNum;
}
double Employee::getHourlyWage()
{
return hourlyWage;
}
void Employee::setHourlyWage(double hrWage)
{
hourlyWage = hrWage;
}
double Employee::getHoursWorked()
{
return hoursWorked;
}
void Employee::setHoursWorked(double hrWorked)
{
hoursWorked = hrWorked;
}
void printCheck(Employee ee)
{
cout << "\n\n--------------------- Fluff Shuffle Electronics -------------------------------- \n";
cout << " Pay to the order of " << ee.getEmployeeName() << "...........................$" << ee.calcPay();
cout << "\n\n United Bank of Eastern Orem \n";
cout << "------------------------------------------------------------------------------- \n";
cout << " Hours Worked: " << ee.getHoursWorked();
cout << "\n Hourly Wage: " << ee.getHourlyWage();
cout << endl << endl;
}//End of function
void read(ifstream &in)
{
Employee employees[10];
int counter = 0;
while (in.read((char *)&employees[counter++], sizeof(Employee)))
for (int i = 0; i<counter; i++)
{
printCheck(employees[i]);
}
in.close();
}
void write(ofstream &out)
{
Instantiate your employees here first, then call their functions.
Employee joe(37, "Joe Brown", "123 Main St.", "123-6788", 10.00,
45.00);
printCheck(joe);
Employee sam(21, "Sam Jones", "45 East State", "661-9000", 12.00,
30.00);
printCheck(sam);
Employee mary(15, "Mary Smith", "12 High Street", "401-8900", 15.00, 40.00);
printCheck(mary);
out.write((char *)(&joe), sizeof(Employee));
out.write((char *)(&sam), sizeof(Employee));
out.write((char *)(&mary), sizeof(Employee));
out.close();
}
//Main function
int main()
{
int choice;
string filename;
while (true)
{
cout << "\nThis program has two options:\n";
cout << "1 - Create a data file, or\n";
cout << "2 - Read data from a file and print paychecks\n";
cout << "\n Press any other key to quit..........\n";
cout << "Please enter <1> to create a file or <2> to print
checks: ";
cin >> choice;
if (choice == 1)
{
cout << "Enter the file name: ";
cin >> filename;
ofstream out(filename);
out.open(filename.c_str(), ios::binary);
write(out);
}
else if (choice == 2)
{
cout << "Enter the file name: ";
cin >> filename;
ifstream in(filename);
in.open(filename.c_str(), ios::binary);
read(in);
}
else break;
//Calls function to displays information
}
}//End of main
These are the instructions for the project.
This is the diagram it refers to
To start: create two classes derived from Employee:
class HourlyEmployee: public Employee
{
};
class SalariedEmployee: public Employee
{
}
and move members related to Hourly working from Employee to HourlyEmployee, then add members related to Salary to SalariedEmployee (WeeklySalary).
This way (removing attributes related to hourly working) you make Employee class more general that can be a base for other kind of employees to (SalariedEmployee).
When you derive HourlyEmployee or SalariedEmployee from Employee, you mean they are kind of Employee, so members that Employee has, they will inherit automatically.