Question

In: Computer Science

I have an error on my code. the compiler says 'setLastName' was not declared in this...

I have an error on my code. the compiler says 'setLastName' was not declared in this scope.

this is my code :

#include <iostream>
using std::cout;
using std::cin;
using std::endl;

#include <string>
using std::string;


class Employee
{
   public :
      
       Employee(string fname, string lname, int salary)
       {
           setFirstName(fname);
           setLastName(lname);
           setMonthlySalary(salary);
       }
      
       void setFirstName(string fname)
       {
           last_name=lname;
       }
      
       void setMonthlySalary(int salary)
       {
           if ( salary <= 0 )
           salary = 0;
          
           if ( salary > 0 )
           monthly_salary=salary;
       }
      
       string getFirstName()
       {
           return first_name;
       }
      
       string getLastName()
       {
           return last_name;
       }
      
       int getMonthlySalary()
       {
           return monthly_salary;
       }
      
   private :
       string first_name;
       string last_name;
       int monthly_salary;
};

int main ()
{
   int yearly_salary;
   int salary_raise;
   int revised_salary;
  
   Employee employeeA("AAA","NNN",1000);
   Employee employeeB("SSS","QQQ",1500);
  
   cout << "Employee-A Full Name : " << employeeA.getFirstName();
       cout << " " << employeeA.getLastName() << endl;
   cout << "Employee-A Yearly Salary : " << employeeA.getMonthlySalary() << endl;
  
       cout << "\n\n\n";
   cout << "Employee-B Full Name : " << employeeB.getFirstName();
       cout << " " << employeeB.getLastName() << endl;
   cout << "employeeB Yearly Salary : " << employeeB.getMonthlySalary() << endl;
  
   cout << "\n\n\n";
      
       yearly_salary = 12*employeeA.getMonthlySalary();
   cout << "Employee-A Yearly Salary : " << yearly_salary << endl;
  
       yearly_salary = 12*employeeB.getMonthlySalary();
   cout << "Employee-B Yearly Salary : " << yearly_salary << endl;
  
   cout << "\n\n\n";
  
       salary_rise= (10*employeeA.getMonthlySalary()) / 100;
   revised_salary= (salary_rise+employeeA.getMonthlySalary())/12;
   employeeA.setMonthlySalary(revised_salary);
  
   cout << "Employee-A Revised Monthly Salary : " << employeeA.getMonthlySalary() << endl;
  
       salry_rise= (10*employeeB.getMonthlySalary())/ 100;
   revised_salary= (salary_rise+employeeB.getMonthlySalary());
   employeeB.setFirstName(revised_salary);
  
   cout << "Employee-B Revised Monthly Salary : " << yearly_salary;
  
   getch();
   return 0;
}

Solutions

Expert Solution

There are so many mistakes in this code :

1)The compiler says 'setLastName' was not declared in this scope because look at the code carefully 'setLastName' function is not defined,'setLastName' function has only been called from ‘Employee’ constructor.

2) ‘setFirstName’ function is defined wrongly.Look at the function ‘setFirstName’ where ‘fname’ is input of the function but you have written ‘last_name=lname;’ there ‘lname’ is not the global variable.

3) Inside the main function you defined ‘int salary_raise’ but further you have used this variable as ‘salary_rise’ name.

4) At the last of the main function you have written ‘employeeB.setFirstName(revised_salary);’ but look ‘revised_salary’ is integer but function ‘setFirstName’ takes string as an input.

I have solved all the above mistakes and I have fixed all the error.

Error free code :

#include <iostream>
using std::cout;
using std::cin;
using std::endl;

#include <string>
using std::string;

class Employee
{
private :
   string first_name;
   string last_name;
   int monthly_salary;
public :

   Employee(string fname, string lname, int salary)
   {
       setLastName(lname);
       setMonthlySalary(salary);
   }

   void setLastName(string lname)
   {
       last_name=lname;
   }
   void setFirstName(string fname)
   {
       first_name=fname;
   }

   void setMonthlySalary(int salary)
   {
       if ( salary <= 0 )
       salary = 0;
    
       if ( salary > 0 )
       monthly_salary=salary;
   }

   string getFirstName()
   {
       return first_name;
   }

   string getLastName()
   {
       return last_name;
   }

   int getMonthlySalary()
   {
       return monthly_salary;
   }
};

int main ()
{
   int yearly_salary;
   int salary_raise;
   int revised_salary;

   Employee employeeA("AAA","NNN",1000);
   Employee employeeB("SSS","QQQ",1500);

   cout << "Employee-A Full Name : " << employeeA.getFirstName();
   cout << " " << employeeA.getLastName() << endl;
   cout << "Employee-A Yearly Salary : " << employeeA.getMonthlySalary() << endl;

   cout << "\n\n\n";
   cout << "Employee-B Full Name : " << employeeB.getFirstName();
   cout << " " << employeeB.getLastName() << endl;
   cout << "employeeB Yearly Salary : " << employeeB.getMonthlySalary() << endl;

   cout << "\n\n\n";
    
   yearly_salary = 12*employeeA.getMonthlySalary();
   cout << "Employee-A Yearly Salary : " << yearly_salary << endl;

   yearly_salary = 12*employeeB.getMonthlySalary();
   cout << "Employee-B Yearly Salary : " << yearly_salary << endl;

   cout << "\n\n\n";

   salary_raise= (10*employeeA.getMonthlySalary()) / 100;
   revised_salary= (salary_raise+employeeA.getMonthlySalary())/12;
   employeeA.setMonthlySalary(revised_salary);

   cout << "Employee-A Revised Monthly Salary : " << employeeA.getMonthlySalary() << endl;

   salary_raise= (10*employeeB.getMonthlySalary())/ 100;
   revised_salary= (salary_raise+employeeB.getMonthlySalary());
   employeeB.getFirstName();

   cout << "Employee-B Revised Monthly Salary : " << yearly_salary;


   return 0;
}

After compilation Output :


Related Solutions

I'm getting an error with my code on my EvenDemo class. I am supposed to have...
I'm getting an error with my code on my EvenDemo class. I am supposed to have two classes, Event and Event Demo. Below is my code.  What is a better way for me to write this? //******************************************************** // Event Class code //******************************************************** package java1; import java.util.Scanner; public class Event {    public final static double lowerPricePerGuest = 32.00;    public final static double higherPricePerGuest = 35.00;    public final static int cutOffValue = 50;    public boolean largeEvent;    private String...
Write a line of code that indicates to the compiler that the method being declared overrides...
Write a line of code that indicates to the compiler that the method being declared overrides a superclass method. Write a line of code that specifies that class Fly inherits from class Insect. Call superclass Insect's toString method from subclass Fly's tostring method. Call superclass Insect's constructor from subclass Fly's constructor, assume that the constructor receives an integer for number of legs and a string for the ability it has.
I cannot get this code to run on my python compiler. It gives me an expected...
I cannot get this code to run on my python compiler. It gives me an expected an indent block message. I do not know what is going on. #ask why this is now happenning. (Create employee description) class employee: def__init__(self, name, employee_id, department, title): self.name = name self.employee_id = employee_id self.department = department self.title = title def __str__(self): return '{} , id={}, is in {} and is a {}.'.format(self.name, self.employee_id, self.department, self.title)    def main(): # Create employee list emp1...
HI. I have been trying to run my code but I keep getting the following error....
HI. I have been trying to run my code but I keep getting the following error. I can't figure out what I'm doing wrong. I also tried to use else if to run the area of the other shapes but it gave me an error and I created the private method. Exception in thread "main" java.util.InputMismatchException at java.base/java.util.Scanner.throwFor(Scanner.java:939) at java.base/java.util.Scanner.next(Scanner.java:1594) at java.base/java.util.Scanner.nextInt(Scanner.java:2258) at java.base/java.util.Scanner.nextInt(Scanner.java:2212) at project2.areacalculation.main(areacalculation.java:26) My code is below package project2; import java.util.Scanner; public class areacalculation { private static...
I don't know why my java code is not running this error code pops up -->...
I don't know why my java code is not running this error code pops up --> Error: Could not find or load main class DieRoll Caused by: java.lang.ClassNotFoundException: DieRoll. Code below:    import java.util.Random;    import java.util.Scanner;    public class Assignment {    public static void combineStrings() {    String school = "Harvard";    String city = "Boston, MA";    int stringLen;       String upper, changeChar, combine;       stringLen = school.length();    System.out.println(school+" contains "+stringLen+" characters." );   ...
My code works in eclipse, but not in Zybooks. I keep getting this error. Exception in...
My code works in eclipse, but not in Zybooks. I keep getting this error. Exception in thread "main" java.util.NoSuchElementException at java.base/java.util.Scanner.throwFor(Scanner.java:937) at java.base/java.util.Scanner.next(Scanner.java:1478) at Main.main(Main.java:34) Your output Welcome to the food festival! Would you like to place an order? Expected output This test case should produce no output in java import java.util.Scanner; public class Main {    public static void display(String menu[])    {        for(int i=0; i<menu.length; i++)        {            System.out.println (i + " - " + menu[i]);...
I'm having trouble with validating this html code. Whenever I used my validator, it says I...
I'm having trouble with validating this html code. Whenever I used my validator, it says I have a stray end tag: (tbody) from line 122 to 123. It's the last few lines. Thanks and Ill thumbs up whoever can help solve my problem. Here's my code: <!DOCTYPE html> <html lang="en"> <head> <title>L7 Temperatures Fields</title> <!--    Name:    BlackBoard Username:    Filename: (items left blank for bb reasons    Class Section: (blank)    Purpose: Making a table to demonstrate my...
I have a error code, for my C++ class, using Putty include <iostream> using namespace std;...
I have a error code, for my C++ class, using Putty include <iostream> using namespace std; int main() { char answer; char grade; cout << "Are you taking a class (enter y, Y, N or N): " << endl; cin >> answer; if (answer == 'N' || 'n'); { cout << "Thanks for using the system" << endl; } else if (answer == 'Y' || 'y'); { cout << "Enter a letter grade (A, B, C, D, or F): "...
In Python I have a code: here's my problem, and below it is my code. Below...
In Python I have a code: here's my problem, and below it is my code. Below that is the error I received. Please assist. Complete the swapCaps() function to change all lowercase letters in string to uppercase letters and all uppercase letters to lowercase letters. Anything else remains the same. Examples: swapCaps( 'Hope you are all enjoying October' ) returns 'hOPE YOU ARE ALL ENJOYING oCTOBER' swapCaps( 'i hope my caps lock does not get stuck on' ) returns 'I...
This is the code I have. My problem is my output includes ", 0" at the...
This is the code I have. My problem is my output includes ", 0" at the end and I want to exclude that. // File: main.cpp /*---------- BEGIN - DO NOT EDIT CODE ----------*/ #include <iostream> #include <fstream> #include <sstream> #include <iomanip> using namespace std; using index_t = int; using num_count_t = int; using isConnected_t = bool; using sum_t = int; const int MAX_SIZE = 100; // Global variable to be used to count the recursive calls. int recursiveCount =...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT