In: Computer Science
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;
}
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 :