C++ .... CLASS inheritance.... why am I getting this one error in my code???? it
ID: 3571257 • Letter: C
Question
C++ .... CLASS inheritance.... why am I getting this one error in my code???? it is driving me CRAZY!!!!!! PLEASE HELP....going loco!!!
=======ERROR MESSAGE FROM THE COMPILER ========
||=== Build file: "no target" in "no project" (compiler: unknown) ===|
C: clases.cpp|22|error: expected unqualified-id before 'public'|
||=== Build failed: 1 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|
======= C++ CODE FOLLOWS ========
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include <cstdlib>
using namespace std;
#ifndef FIREEMPLOYEE_H
#define FIREEMPLOYEE_H
class employeeType public:personType
{
private:
int id;
string first;
string last;
char gender;
double payrate;
string jobRole;
int years;
public:
virtual void print() const = 0;
//Function to output employee's data.
virtual double calculatePay() const = 0;
//Function to calculate and return the wages.
//Postcondition: Pay is calculated and returned
void setId(int personId)
{
personId = id;
}
//Function to set the salary.
//Postcondition: personId = id;
int getId() const
{
return id;
}
//Function to retrieve the id.
//Postcondition: returns personId
employeeType(string first = "", string last = "", long id = 0);
//Constructor with parameters
//Sets the first name, last name, payRate, and
//hoursWorked according to the parameters. If
//no value is specified, the default values are
//assumed.
//Postcondition: firstName = first;
//lastName = last; personId = id;
};
#endif
//The definitions of the constructor and functions of the class
//employeeType that are not pure virtual are
/*void employeeType::setId(int personId)
{
personId = id;
} */
/*int employeeType::getId() const
{
return personId;
} */
employeeType::employeeType(string firstName, string lastName, long personId)
//:personType(first, last)
{
firstName = first;
lastName = last;
personId = id;
}
//#include "employeeType.h"
///============================================================================
class HireEmployee: public employeeType
{
public:
void set(string first, string last, long id,
double salary, double bonus);
//Function to set the first name, last name,
//id, and salary according to the parameters.
//Postcondition: firstName = first; lastName = last;
// personId = id; empSalary = salary;
//empBonus = bonus;
void setSalary(double salary);
//Function to set the salary.
//Postcondition: empSalary = salary;
double getSalary();
//Function to retrieve the salary.
//Postcondition: returns empSalary
void setBonus(double bonus);
//Function to set the bonus.
//Postcondition: empBonus = bonus;
double getBonus();
//Function to retrieve the bonus.
//Postcondition: returns empBonus;
void print() const;
//Function to output the first name, last name,
//and the wages.
//Postcondition: Outputs
//Id:
//Name: firstName lastName
//Wages: $$$$.$$
double calculatePay() const;
//Function to calculate and return the wages.
//Postcondition: Pay is calculated and returned
HireEmployee(string first = "", string last = "",
long id = 0, double salary = 0, double bonus = 0);
//Constructor with default parameters.
//Sets the first name, last name, id, salary, and
//bonus according to the parameters. If
//no value is specified, the default values are
//assumed.
//Postcondition: firstName = first; lastName = last;
//personId = id; empSalary = salary;
//empBonus = bonus;
private:
double empSalary;
double empBonus;
};
//The definitions of the constructor and functions of the class HireEmployee are:
void HireEmployee::set(string first, string last,
long id,
double salary, double bonus)
{
string setName;
setName = first + last;
setId(id);
empSalary = salary;
empBonus = bonus;
}
void HireEmployee::setSalary(double salary)
{
empSalary = salary;
}
double HireEmployee::getSalary()
{
return empSalary;
}
void HireEmployee::setBonus(double bonus)
{
empBonus = bonus;
}
double HireEmployee::getBonus()
{
return empBonus;
}
void HireEmployee::print() const
{
cout << "Id: " << getId() << endl;
cout << "Name: "; personType::print();
cout << endl;
cout << "Wages: $" << calculatePay() << endl;
}
double HireEmployee::calculatePay() const
{
return empSalary + empBonus;
}
HireEmployee::HireEmployee(string first, string last,
long id, double salary,
double bonus)
:employeeType(first, last, id)
{
empSalary = salary;
empBonus = bonus;
}
///============================================================================
//The definition of the class FireEmployee is:
//#include "employeeType.h"
class FireEmployee: public employeeType
{
public:
void set(string first, string last, long id, ///void is good
double rate, double hours);
//Function to set the first name, last name,
//id, payRate, and hoursWorked according to the parameters.
//Postcondition: firstName = first; lastName = last;
// personId = id;
//payRate = rate; hoursWorked = hours;
void calculatePay const
{
return FirepayRate
}
//Function to calculate and return the wages.
//Postcondition: Pay is calculated and returne
void setFirepayRate(double FirepayRate)
{
FirepayRate = rate;
}
//Function to set the salary.
//Postcondition: payRate = rate;
double getFirepayRate();
//Function to retrieve the salary.
//Postcondition: returns payRate;
void setHoursWorked(double hours);
//Function to set the bonus.
//Postcondition: hoursWorked = hours
double getHoursWorked();
//Function to retrieve the bonus.
//Postcondition: returns empBonus
void print() const;
//Function to output the id, first name, last name,
//and the wages.
//Postcondition: Outputs
//Id:
//Name: firstName lastName
//Wages: $$$$.$$
FireEmployee(string first = "", string last = "",
long id = 0,
double rate = 0, double hours = 0);
//Constructor with parameters.
//Sets the first name, last name, payrate and
//hoursWorked according to the parameters. If
//no value is specified, the default values are
//assumed.
//Postcondition: firstName = first; lastName = last;
//personId = id; payRate = rate;
//hoursWorked = hours;
private:
double FirepayRate;
double hoursWorked;
};
//The definitions of the constructor and functions of the class FireEmployee are:
void FireEmployee::set(string first, string last,
long id,
double rate, double hours)
{
string setName;
setName = first + last;
setId(id);
FirepayRate = rate;
hoursWorked = hours;
}
void FireEmployee::setFirePayRate(double rate)
{
FirepayRate = rate;
}
double FireEmployee::getFirePayRate()
{
return FirepayRate;
}
void FireEmployee::setHoursWorked(double hours)
{
hoursWorked = hours;
}
double FireEmployee::getHoursWorked()
{
return hoursWorked;
}
void FireEmployee::print() const
{
cout << "Id: " << getId() << endl;
cout << "Name: ";
personType::print();
cout << endl;
cout << "Wages: $" << calculatePay() << endl;
}
double FireEmployee::calculatePay() const
{
return (FirepayRate * hoursWorked);
}
///constructor
FireEmployee::FireEmployee(string first, string last,
long id,
double rate, double hours)
:employeeType(first, last, id)
{
FirepayRate = rate;
hoursWorked = hours;
}
///============================================================================
//The following function main tests these classes
#include <iostream>
//#include "FireEmployee.h"
//#include "HireEmployee.h"
int main()
{
HireEmployee newEmp("John", "Smith", 75, 56000, 5700);
FireEmployee firepEmp("Bill", "Nielson", 275, 15.50, 57);
newEmp.print();
cout << endl;
firepEmp.print();
return 0;
}
Explanation / Answer
There are a number of errors present in the program. Most are syntactical errors. Eg, of one is given below.
class employeeType public:personType
It should be
class employeeType //as there is no parent class defined as personType.
Line 142: as the class personType does not exist, and there is an in-between semicolon. Infact, you cannot display the full name by directly accessing a parent class' private member. You need a public getter method for this. Also, you haven't saved the full name of the employee anywhere, so you need another variable for that in the parent class. I have changed the constructor definition of the parent class as shown below.
line 171 -> line should be void calculatePay() const
line 173 -> semicolon missing
The working code for the HireEmployee class only is given below. You can likewise extend it for the FireEmployee class.
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include <cstdlib>
using namespace std;
#ifndef FIREEMPLOYEE_H
#define FIREEMPLOYEE_H
class employeeType
{
private:
int id;
string first;
string last;
string fullName;
char gender;
double payrate;
string jobRole;
int years;
public:
virtual void print() const = 0;
//Function to output employee's data.
virtual double calculatePay() const = 0;
//Function to calculate and return the wages.
//Postcondition: Pay is calculated and returned
void setId(int personId)
{
personId = id;
}
//Function to set the salary.
//Postcondition: personId = id;
int getId() const
{
return id;
}
string getFullName() const
{
return fullName;
}
//Function to retrieve the id.
//Postcondition: returns personId
employeeType(string first = "", string last = "", long id = 0)
{
this->first = first;
this->last = last;
fullName = first + last;
this->id = id;
}
//Constructor with parameters
//Sets the first name, last name, payRate, and
//hoursWorked according to the parameters. If
//no value is specified, the default values are
//assumed.
//Postcondition: firstName = first;
//lastName = last; personId = id;
};
#endif
//The definitions of the constructor and functions of the class
//employeeType that are not pure virtual are
/*void employeeType::setId(int personId)
{
personId = id;
} */
/*int employeeType::getId() const
{
return personId;
} */
///============================================================================
class HireEmployee: public employeeType
{
public:
void set(string first, string last, long id,
double salary, double bonus);
//Function to set the first name, last name,
//id, and salary according to the parameters.
//Postcondition: firstName = first; lastName = last;
// personId = id; empSalary = salary;
//empBonus = bonus;
void setSalary(double salary);
//Function to set the salary.
//Postcondition: empSalary = salary;
double getSalary();
//Function to retrieve the salary.
//Postcondition: returns empSalary
void setBonus(double bonus);
//Function to set the bonus.
//Postcondition: empBonus = bonus;
double getBonus();
//Function to retrieve the bonus.
//Postcondition: returns empBonus;
void print() const;
//Function to output the first name, last name,
//and the wages.
//Postcondition: Outputs
//Id:
//Name: firstName lastName
//Wages: $$$$.$$
double calculatePay() const;
//Function to calculate and return the wages.
//Postcondition: Pay is calculated and returned
HireEmployee(string first = "", string last = "",
long id = 0, double salary = 0, double bonus = 0);
//Constructor with default parameters.
//Sets the first name, last name, id, salary, and
//bonus according to the parameters. If
//no value is specified, the default values are
//assumed.
//Postcondition: firstName = first; lastName = last;
//personId = id; empSalary = salary;
//empBonus = bonus;
private:
double empSalary;
double empBonus;
};
//The definitions of the constructor and functions of the class HireEmployee are:
void HireEmployee::set(string first, string last,
long id,
double salary, double bonus)
{
string setName;
setName = first + last;
setId(id);
empSalary = salary;
empBonus = bonus;
}
void HireEmployee::setSalary(double salary)
{
empSalary = salary;
}
double HireEmployee::getSalary()
{
return empSalary;
}
void HireEmployee::setBonus(double bonus)
{
empBonus = bonus;
}
double HireEmployee::getBonus()
{
return empBonus;
}
void HireEmployee::print() const
{
cout << "Id: " << getId() << endl;
cout << "Name: "<< getFullName() << endl;
cout << endl;
cout << "Wages: $" << calculatePay() << endl;
}
double HireEmployee::calculatePay() const
{
return empSalary + empBonus;
}
HireEmployee::HireEmployee(string first, string last,
long id, double salary,
double bonus)
:employeeType(first, last, id)
{
empSalary = salary;
empBonus = bonus;
}
#include <iostream>
int main()
{
HireEmployee newEmp("John", "Smith", 75, 56000, 5700);
//FireEmployee firepEmp("Bill", "Nielson", 275, 15.50, 57);
newEmp.print();
cout << endl;
//firepEmp.print();
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.