Question

C++ question The code below has five errors.  The errors may be syntax errors or logic errors,...

C++ question

The code below has five errors.  The errors may be syntax errors or logic errors, and there may be more than one per line; examine the code carefully to find them.  Indicate each of the errors you find by writing the line number and correction in the space provided below. You must find and correct all five of the errors.

If there were no errors, this code would output:

Paycheck for 123-45-678 : $690.625

Paycheck for 876-54-321 : $818.125

1

#include <iostream>
2 #include <string>
3 using namespace std;
4 class Employee
5 {
6 public:
7   Employee(string theSSN){m_ssn=theSSN;}
8 void printCheck() = 0;
9 string getSSN(){return m_ssn;}
10 void setPay(double pay){m_pay=pay;}
11 double getPay(){return m_pay;}
12 private:
13 string m_ssn;
14 double m_pay;
15 };
16 class HourlyEmployee : public Employee
17 {
18 public:
19   HourlyEmployee(string theSSN, double rate, double hours):
20   Employee(),m_rate(rate),m_hours(hours){}
21 void printCheck()
22 {
23     setPay(m_hours*m_rate);
24     cout<<"Paycheck for "<< m_ssn << " : $" << getPay() << endl;
25 }
26 double getRate(){return m_rate;}
27 private:
28 double m_hours;
29 double m_rate;
30 };
31 int main()
32 {
33   HourlyEmployee anEmployee("123-45-678",21.25,32.5);
34   HourlyEmployee* secondEmployee = HourlyEmployee("876-54-321", anEmployee.getRate(), 38.5);
35   anEmployee->printCheck();
36   secondEmployee->printCheck();
37 delete secondEmployee;
38 return 0;
39 }
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Final Code after correcting errors
*******************************************************************************************************
#include <iostream>
#include <string>
using namespace std;
class Employee
{
       public:
       Employee(string theSSN){m_ssn=theSSN;}
       void printCheck();-------------------------------------------1
       string getSSN(){return m_ssn;}
       void setPay(double pay){m_pay=pay;}
       double getPay(){return m_pay;}
       private:
       string m_ssn;
       double m_pay;
       };
       class HourlyEmployee : public Employee
       {
       public:
       HourlyEmployee(string theSSN, double rate, double hours):
       m_rate(rate),m_hours(hours),Employee(theSSN){}--------------------------2
       void printCheck()
       {
       setPay(m_hours*m_rate);
       cout<<"Paycheck for "<< getSSN() << " : $" << getPay() << endl;---------------3
       }
       double getRate(){return m_rate;}
       private:
       double m_hours;
       double m_rate;
       };
       int main()
       {
       HourlyEmployee anEmployee("123-45-678",21.25,32.5);
       HourlyEmployee* secondEmployee = new HourlyEmployee("876-54-321", anEmployee.getRate(), 38.5);------------------4
       anEmployee.printCheck();----------------------------------5
       secondEmployee->printCheck();
       delete secondEmployee;
       return 0;
       }

Explaination:

1. line no 8 i.e void printCheck() = 0; syntax is wrong method can't be intialized with 0. So after correction it is void printCheck();

2. line no 20 i.e Employee(),m_rate(rate),m_hours(hours){} syntax is wrong and also not passing any argument in parent class constructor i.e Employee().

So after correction it is m_rate(rate),m_hours(hours),Employee(theSSN){}

3. line no 24 i.e cout<<"Paycheck for "<< m_ssn << " : $" << getPay() << endl; here m_ssn is in private scope we can't access it directly by calling its name.

So after correction it is cout<<"Paycheck for "<< getSSN() << " : $" << getPay() << endl;

4. line no 34 i.e HourlyEmployee* secondEmployee = HourlyEmployee("876-54-321", anEmployee.getRate(), 38.5); syntax is wrong new keyword is missing.

So after correction it is HourlyEmployee* secondEmployee = new HourlyEmployee("876-54-321", anEmployee.getRate(), 38.5);

5. line no 35 i.e anEmployee->printCheck(); syntax is wrong anEmployee is an object it can access any function of a class using (.) operator only.

So after correction it is anEmployee.printCheck();


 #include <iostream> #include <string> using namespace std; class Employee {                 public:                  Employee(string theSSN){m_ssn=theSSN;}                 void printCheck();              string getSSN(){return m_ssn;}          void setPay(double pay){m_pay=pay;}             double getPay(){return m_pay;}          private:                string m_ssn;           double m_pay;           };              class HourlyEmployee : public Employee          {               public:                  HourlyEmployee(string theSSN, double rate, double hours):               m_rate(rate),m_hours(hours),Employee(theSSN){}                 void printCheck()               {                setPay(m_hours*m_rate);                 cout<<"Paycheck for "<< getSSN() << " : $" << getPay() << endl;          }               double getRate(){return m_rate;}                private:                double m_hours;                 double m_rate;          };              int main()              {                HourlyEmployee anEmployee("123-45-678",21.25,32.5);             HourlyEmployee* secondEmployee = new HourlyEmployee("876-54-321", anEmployee.getRate(), 38.5);                  anEmployee.printCheck();                secondEmployee->printCheck();               delete secondEmployee;          return 0;               } 

Nertre ( CDébug Stop G Share Save {} Beautify Language C++ main.cpp 1 #include <iostream> 2 #include <string> 3 using namespa► Run Debug Stop Share Save {} Beautify Language C++ i main.cpp { public: HourlyEmployee(string theSSN, double rate, double hRun Debug Stop Share Save {} Beautify Language C++ { } main.cpp 22 23 setPay(m_hours*m_rate); 24 cout<<Paycheck for << getS

Add a comment
Know the answer?
Add Answer to:
C++ question The code below has five errors.  The errors may be syntax errors or logic errors,...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • In the processLineOfData, write the code to handle case "H" of the switch statement such that:...

    In the processLineOfData, write the code to handle case "H" of the switch statement such that: An HourlyEmployee object is created using the firstName, lastName, rate, and hours local variables. Notice that rate and hours need to be converted from String to double. You may use parseDouble method of the Double class as follows:               Double.parseDouble(rate) Call the parsePaychecks method in this class passing the HourlyEmployee object created in the previous step and the checks variable. Call the findDepartment method...

  • What is wrong with this Code? Fix the code errors to run correctly without error. There...

    What is wrong with this Code? Fix the code errors to run correctly without error. There are two parts for one code (one question) the two parts branch off, one part has 2 sheets of code, the other has 3 sheets of code, all are small code segments for one question. Pt.1 - 2 sheets of code: public class Computer { private String make; private String type; private String modelNumber; private double cpuSpeed_GHz; private int ramSize_GB; private int hardDriveSize_GB; private...

  • In the processLineOfData method, write the code to handle case "H" of the switch statement such...

    In the processLineOfData method, write the code to handle case "H" of the switch statement such that: • An HourlyEmployee object is created using the firstName, lastName, rate, and hours local variables. Notice that rate and hours need to be converted from String to double. You may use parseDouble method of the Double class as follows: Double.parseDouble(rate) Call the parsePaychecks method in this class passing the Hourly Employee object created in the previous step and the checks variable. Call the...

  • The files provided in the code editor to the right contain syntax and/or logic errors. In...

    The files provided in the code editor to the right contain syntax and/or logic errors. In each case, determine and fix the problem, remove all syntax and coding errors, and run the program to ensure it works properly. // Start with a penny // double it every day // how much do you have in a 30-day month? public class DebugSix1 { public static void main(String args[]) { final int DAYS = 30; double money = 0.01; int day =...

  • Previous class code: 1) Employee.java ------------------------------------------------------------------------------- /** * */ package main.webapp; /** * @author sargade *...

    Previous class code: 1) Employee.java ------------------------------------------------------------------------------- /** * */ package main.webapp; /** * @author sargade * */ public class Employee { private int empId; private String empName; private Vehicle vehicle; public Double calculatePay() { return null; } /** * @return the empId */ public int getEmpId() { return empId; } /** * @param empId * the empId to set */ public void setEmpId(int empId) { this.empId = empId; } /** * @return the empName */ public String getEmpName() { return...

  • Debugging: The code below has six errors. The errors may be syntax errors or logic errors,...

    Debugging: The code below has six errors. The errors may be syntax errors or logic errors, and there may be more than one per line; examine the code carefully to find them. Indicate each of the errors you find by writing the line number and correction in the space provided below. This program is designed to take a constant number of scores (3 in this case) and store them into an array. Then the array is passed to a function...

  • departmentstore: package departmentstorepkg; import java.util.ArrayList; public class DepartmentStore {    private static final int DEFAULT_SIZE =...

    departmentstore: package departmentstorepkg; import java.util.ArrayList; public class DepartmentStore {    private static final int DEFAULT_SIZE = 10; private StaffMember [] myEmployees; private int myNumberEmployees; private String myFileName; private StaffMember[] employee; public DepartmentStore (String filename){ myFileName = filename; myEmployees = employee;    } public String toString(){ return this.getClass().toString() + ": " + myFileName; } public void addEmployee(Employee emp){ } /** * prints out all the employees in the array list held in this class */ public void print(){ for(int i =...

  • The files provided in the code editor to the right contain syntax and/or logic errors. In...

    The files provided in the code editor to the right contain syntax and/or logic errors. In each case, determine and fix the problem, remove all syntax and coding errors, and run the program to ensure it works properly. // DebugFive2.java // Decides if two numbers are evenly divisible import java.util.Scanner; public class DebugFive2 { public static void main(String args[]) { int num; int num2; Scanner input = new Scanner(System.in); System.out.print("Enter a number "); num = input.nextInteger() System.out.print("Enter another number ");...

  • Programming: Java: Fixing errors in code help: The errors are in square.java and rectangle.java and they...

    Programming: Java: Fixing errors in code help: The errors are in square.java and rectangle.java and they both say: constructor Shape in class Shape cannot be applied to given types; required: no arguments found: String reason: actual and formal argument lists differ in length The directions say: The files Shape.java and TestArea.java have been finished already, YOU DONT ADD ANYTHING TO THESE TWO. According to the requirement, you need to modify in Square.java and Rectangle.java, respectively a. Implementing constructor with no...

  • Please answer the following question with Java Modify the Employee and ProductionWorker classes provided below so...

    Please answer the following question with Java Modify the Employee and ProductionWorker classes provided below so they throw exceptions when the following errors occur. - The Employee class should throw an exception named InvalidEmployeeNumber when it receives an employee number that is less than 0 or greater than 9999. - The ProductionWorker class should throw an exception named InvalidShift when it receives an invalid shift. - The ProductionWorker class should thrown anexception named InvalidPayRate when it receives a negative number...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT