How to send variables to class methods [closed] - c++

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
I have some variables in my program that are actually in a case statement. I have tried to get them to go to my class functions but I keep getting an error. I am suppose to get the variables to go to the SalariedEmployee and the Administrator class.
//Lynette Wilkins
//Week 12
#include <iostream>
#include <cstdlib>
#include <string>
#include <iomanip>
#include <cmath>
using namespace std;
class SalariedEmployee
{
private:
double wageRate;
int hours;
protected:
string name;
string ssn;
double netPay;
string department;
public:
SalariedEmployee(string n, string s, double np, double w, int h, string d);
~SalariedEmployee() {cout<<endl;}
string Getname(); //returns name
string Getssn(); // returns social security number
double GetnetPay(); //returns netPay
string Getdepartment(); // returns department
double GetwageRate(); //returns wage rate
int Gethours(); //returns hours
void Setname(string); //sets name
void Setssn(string); //sets ssn
void SetnetPay(double); //sets net pay
void Setdepartment(string); //sets department
void SetwageRate(double); //sets wage rate
void Sethours(int); //sets hours
};
string SalariedEmployee::Getname()
{
return name;
}
string SalariedEmployee::Getssn()
{
return ssn;
}
double SalariedEmployee::GetnetPay()
{
return netPay;
}
double SalariedEmployee::GetwageRate()
{
return wageRate;
}
int SalariedEmployee::Gethours()
{
return hours;
}
void SalariedEmployee::Setname(string n)
{
name = n;
}
void SalariedEmployee::Setssn(string s)
{
ssn = s;
}
void SalariedEmployee::SetnetPay(double np)
{
netPay = np;
}
void SalariedEmployee::Setdepartment(string d)
{
department = d;
}
void SalariedEmployee::SetwageRate(double w)
{
wageRate = w;
}
void SalariedEmployee::Sethours(int h)
{
hours = h;
}
class Administrator : public SalariedEmployee
{
protected:
string title;
string responsi;
string super;
double salary;
public:
Administrator(string t, string r, string s, double sa);
~Administrator();
string Gettitle();
string Getresponsi();
string Getsuper();
double Getsalary();
void Settitle(string);
void Setresponsi(string);
void Setsuper(string);
void Setsalary(double);
void print();
};
Administrator::~Administrator()
{
cout<<endl;
}
string Administrator::Gettitle()
{
return title;
}
string Administrator::Getresponsi()
{
return responsi;
}
string Administrator::Getsuper()
{
return super;
}
double Administrator::Getsalary()
{
return salary;
}
void Administrator::Settitle(string ti)
{
title = ti;
}
void Administrator::Setresponsi(string re)
{
responsi = re;
}
void Administrator::Setsuper(string su)
{
super=su;
}
void Administrator::Setsalary(double sa)
{
salary= sa;
}
void Administrator::print( )
{
cout << "\n_______________________________________________\n";
cout << "Pay to the order of " << name<< endl;
cout << "The sum of " << netPay << " Dollars\n";
cout << "_________________________________________________\n";
cout <<endl<<endl;
cout << "Employee Number: " << ssn << endl;
cout << "Salaried Employee. Regular Pay: "
<< salary << endl;
cout << "_________________________________________________\n";
}
int main()
{
string name;
string soc;
double net = 0;
double wage = 0;
int hrs = 0;
string dept;
string admtitle;
string resp;
string sup;
double sal = 0;
int response;
string date = "January 12, 2013";
cout<<setprecision(2)
<<setiosflags(ios::fixed)
<<setiosflags(ios::showpoint);
SalariedEmployee emp1(name, soc,net, wage, hrs, dept);
while(response != 4){
cout<<"Employee and Administrator Salary Program "<<endl;
cout<<"(You will have to enter data first before you do anything else)"<<endl<<endl;
cout<<"Enter Employee Data, Enter 1"<<endl;
cout<<"Change data, Enter 2"<<endl;
cout<<"Print Check, Enter 3"<<endl;
cout<<"End Program, Enter 4"<<endl<<endl;
cout<<"Please make your selection"<<endl;
cin>> response;
switch (response)
{
case 1:
cout <<"The employee's data will be entered here: "<<endl<<endl;
cout<<"Enter the employees name: ";
cin.ignore();
getline(cin, name);
cout<<"Enter the employees social security number: ";
cin.ignore();
getline(cin, soc);
cout<<"Enter the employees net pay: ";
cin>>net;
cout<<"Enter the employees wage rate: ";
cin>>wage;
cout<<"Enter the number of hours the employer worked: ";
cin>>hrs;
cout<<"Enter the employees title: ";
cin.ignore();
getline(cin,admtitle);
cout<<"Enter the employees area responsibility: ";
cin.ignore();
getline(cin, resp);
cout<<"Enter the employees salary: ";
cin>>sal;
cout<<endl<<endl<<endl;
break;
case 2:
cout<<"Please change the data you entered previously here. " <<endl<<endl;
cout<<"Enter the employees name: ";
cin.ignore();
getline(cin, name);
cout<<"Enter the employees social security number: ";
cin.ignore();
getline(cin, soc);
cout<<"Enter the employees net pay: ";
cin>>net;
cout<<"Enter the employees wage rate: ";
cin>>wage;
cout<<"Enter the number of hours the employer worked: ";
cin>>hrs;
cout<<"Enter the employees title: ";
cin.ignore();
getline(cin,admtitle);
cout<<"Enter the employees area responsibility: ";
cin.ignore();
getline(cin, resp);
cout<<"Enter the employees salary: ";
cin>>sal;
cout<<endl<<endl<<endl;
break;
case 3:
cout <<"Information Printed"<<endl<<endl;
cout<<"_____________________________"<<date<<endl;
&Administrator::print;
break;
default:
cout<<endl<<endl
<<"Invalid Selection! Try Again"<<endl;
exit(1);
}
}
system("PAUSE");
return 0;
}

You never defined the constructors for SalariedEmployee or Administrator. Another answerer showed how you might define an implementation of a constructor that matches the signature in your existing definition, but in your code, the values of those variables are meaningless when you instantiate the emp1 object anyway, so you'd do better to just use a default constructor where you simply initialize most variables to 0:
SalariedEmployee::SalariedEmployee() :
wageRate(0), hours(0), netPay(0) {}
Note that I didn't bother initializing the string members; they automatically initialize themselves to "" (nothing).
Also, when you input data with getline(), you're not doing anything with it. Call one of your setter functions to pass the value that you read from getline(cin,...) to your emp1 object. Your option '3' looks like it's supposed to print whatever you entered previously, but you're not calling any "print" function. Your code has &Administrator::print; but that doesn't print anything. That statement evaluates to the address of the print method of the Administrator class, but you don't do anything with that address, so that statement does nothing. You might want to call emp1.print(), but emp1 is an object of type SalariedEmployee, not Administrator, and there's no print() method in the SalariedEmployee class.
Has your class talked about virtual inheritance (polymorphism)? If so, then you're probably supposed to be declaring a print() method in your SalariedEmployee class, and then defining an implementation of it in Administrator. So in class SalariedEmployee, you'll want this:
void print() = 0;
Then, in class Administrator, define it just as you've done. But when you create your emp1 object, be sure to make its type be Administrator because SalariedEmployee is just an abstract base class (since you only declared that objects of types inherited from SalariedEmployee should have a print() method, but print() isn't actually defined in the SalariedEmployee class).

You didn't implement the constructor of SalariedEmployee. You need something like:
SalariedEmployee::SalariedEmployee(string n, string s, double np,
double w, int h, string d)
: name(n),
ssn(s),
netPay(np),
wageRate(w),
hours(h),
department(d)
{
}

Related

Exception has occured, unknown signal error when using class object again inside each function

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.

PrintInfo() Giving Me A Hard Time

So I'm studying with classes and and I'm making a program that prints user entered data, however I can't seem to get past this one compiler error. The gist of the program is to take student user data and output it.
#include<iostream>
#include<string>
using namespace std;
class Student {
public:
Student();
~Student();
void SetName(string studentname); //mutator for name
void SetId(int studentid); //mutator for id
void SetGender(string studentgender); //mutator for gender
void SetEthnicity(string studentethnicity); // mutator for ethnicity
void SetMajor(string studentmajor); //mutator for major
void SetMinor(string studentminor); //mutator for minor
void SetGPA(float studentgpa); // mutator for gpa
string GetName(); //accessor for name
int GetId(); //accessor for id
string GetGender(); //accesor for gender
string GetEthnicity(); // accessor for ethnicity
string GetMajor(); // accessor for major
string GetMinor(); //accessor for minor
float GetGPA(); // accessor for GPA
void PrintInfo(void); // print student information
private:
string Name;
int id_number;
string gender;
string ethnicity;
string major;
string minor;
float gpa;
};
Student::Student()
{
Name = " ";
id_number = 0;
gender = " ";
ethnicity = " ";
major = " ";
minor = "None";
gpa = 0;
}
//mutator functions
void Student::SetName(string studentname)
{
Name = studentname;
};
void Student::SetId(int studentid)
{
id_number = studentid;
}
void Student::SetGender(string studentgender)
{
gender = studentgender;
}
void Student::SetEthnicity(string studentethnicity)
{
ethnicity = studentethnicity;
}
void Student::SetMajor(string studentmajor)
{
major = studentmajor;
}
void Student::SetMinor(string studentminor)
{
minor = studentminor;
}
void Student::SetGPA(float studentgpa)
{
gpa = studentgpa;
}
//accessor functions
string Student::GetName()
{
return Name;
}
int Student::GetId()
{
return id_number;
}
string Student::GetGender()
{
return gender;
}
string Student::GetEthnicity()
{
return ethnicity;
}
string Student::GetMajor()
{
return major;
}
string Student::GetMinor()
{
return minor;
}
float Student::GetGPA()
{
return gpa;
}
void Student::PrintInfo(void)
{
cout<<"Name: "<<myStudent.GetName()<<endl;
cout<<"ID #: "<<myStudent.GetId()<<endl;
cout<<"Gender: "<<myStudent.GetGender()<<endl;
cout<<"Ethnicity: "<<myStudent.GetEthnicity()<<endl;
cout<<"Major: "<<myStudent.GetMajor()<<endl;
cout<<"Minor: "<<myStudent.GetMinor()<<endl;
cout<<"GPA: "<<myStudent.GetGPA()<<endl;
}
int main()
{
int* studentarray;
studentarray = new int[5];
Student myStudent;
string names;
int ids;
string genders;
string ethnicities;
string majors;
string minors;
float gpas;
cout<<"Student Information."<<endl;
cout<<"Name: "<<endl;
getline (cin, names);
cout<<"ID Number: "<<endl;
cin>> ids;
cout<<"Gender: "<<endl;
getline(cin, genders);
cout<<"Ethnicity: "<<endl;
getline(cin, ethnicities);
cout<<"Major: "<<endl;
getline(cin, majors);
cout<<"Minor: "<<endl;
getline(cin, minors);
cout<<"Enter GPA: "<<endl;
cin>> gpas;
myStudent.SetName(names);
myStudent.SetId(ids);
myStudent.SetGender(genders);
myStudent.SetEthnicity(ethnicities);
myStudent.SetMajor(majors);
myStudent.SetMinor(minors);
myStudent.SetGPA(gpas);
myStudent.PrintInfo();
return 0;
}
Then this is my compiler error
In member function 'void Student::PrintInfo()':
117:18: error: 'myStudent' was not declared in this scope
myStudent does not exist inside Student::PrintInfo().
PrintInfo() is actually a member function, so it should look simply like this.
void Student::PrintInfo(void)
{
cout<<"Name: "<< GetName()<<endl;
cout<<"ID #: "<< GetId()<<endl;
cout<<"Gender: "<< GetGender()<<endl;
cout<<"Ethnicity: "<< GetEthnicity()<<endl;
cout<<"Major: "<< GetMajor()<<endl;
cout<<"Minor: "<< GetMinor()<<endl;
cout<<"GPA: "<< GetGPA()<<endl;
}
This one is even better (Edit: Better in this case. If the getter methods were to perform a more complex task than just retrieve the value, you would use them):
void Student::PrintInfo(void)
{
cout<<"Name: "<< Name<<endl;
cout<<"ID #: "<< id_number<<endl;
cout<<"Gender: "<< gender<<endl;
cout<<"Ethnicity: "<< ethnicity<<endl;
cout<<"Major: "<< major<<endl;
cout<<"Minor: "<< minor<<endl;
cout<<"GPA: "<< gpa<<endl;
}
Or, for clarity, you might prefer this format:
void Student::PrintInfo(void)
{
cout<<"Name: "<< this->Name<<endl;
cout<<"ID #: "<< this->id_number<<endl;
cout<<"Gender: "<< this->gender<<endl;
cout<<"Ethnicity: "<< this->ethnicity<<endl;
cout<<"Major: "<< this->major<<endl;
cout<<"Minor: "<< this->minor<<endl;
cout<<"GPA: "<< this->gpa<<endl;
}
As a side note, you should specify that your getter methods (and PrintInfo as well) don't modify the object.
int GetWhatever() const {} // <-- The const keyword right there

To find max salary using friend function of a class having employee details(name,ID, position,salary) and print max salary details

#include <iostream.h>
#include <conio.h>
class Employee{
int Id;
string Name;
string Post;
public:
long int Salary;
void GetDetails();
void DisplayDetails();
friend void MaxSalary();
};
void Employee::GetDetails(){
cout << "\nEnter Employee Id : ";
cin >> Id;
cout << "\nEnter Employee Name : ";
cin.ignore();
getline(cin,Name);
cout << "\nEnter Employee Post : ";
cin.ignore();
getline(cin,Post);
cout << "\nEnter Employee Salary : ";
cin >> Salary;
}
void Employee::DisplayDetails(){
cout << "\nEmployee Id : " << Id;
cout << "\nEmployee Name : " << Name;
cout << "\nEmployee Post : " << Post;
cout << "\nEmployee Salary : " << Salary;
}
void MaxSalary(Employee a[], int x){
long int max;
for(int j=0; j<x; j++){
if(a[j].Salary>a[j+1].Salary)
max=a[j].Salary;
}
cout<<"Maximum Salary = "<<max<<endl;
}
int main()
{
int n, i;
cout<<"Enter Number of Employees : ";
cin>>n;
Employee E[n];
cout<<"\n\n----------ENTER DETAILS OF EMPLOYEES----------\n\n";
for(i=0;i<n;i++){
cout<<"\n\n Enter details of Employee "<<i+1<<endl;
E[i].GetDetails();
}
cout<<"\n\n----------DETAILS OF EMPLOYEES----------\n\n";
for(i=0;i<n;i++){
cout<<"\n\n Details of Employee "<<i+1<<endl;
E[i].DisplayDetails();
}
MaxSalary(E[n], n );
return 0;
}
There are lots of defects in the given code:
The variable length arrays (VLAs) are not allowed in C++. In other words, the syntax:
cin >> x;
Employee emp[x];
is invalid. Note that C99 is an exception in this case.
In the For loop of MaxSalary(), a[j + 1] is given, it will access out-of-bounds array, which invokes an undefined behavior.
Declaring a variable in a class as public breaks the rule of OOP:
public:
long int Salary; // Not a good idea
You do not need to pass [n] as the function argument:
MaxSalary(E[n], n);
The refined code would look like:
#include <iostream>
#include <limits>
#include <vector>
// Using this statement in order to reduce the spam of 'std::' here
using namespace std;
class Employee {
int ID;
string name;
string post;
long int salary;
public:
Employee() {}
// Initializing the class object
void initEmployee(int id, string varName, string varPost, long sal) {
ID = id, \
name = varName, \
post = varPost, \
salary = sal;
}
// 'salary' getter
long getSalary() {
return salary;
}
// The friend function
friend void maxSalary(vector<Employee> data) {
int max = 0;
for (size_t i{1}, len = data.size(); i < len; i++)
// Comparison
if (data[i].getSalary() > data[i - 1].getSalary())
max = data[i].getSalary();
// After the loop execution, prints the maximum salary
std::cout << "Maximum salary: " << max << endl;
}
};
int main(void) {
int total;
cout << "Enter the total number of employees: ";
cin >> total;
vector<Employee> emp(total);
for (int i{}; i < total; i++) {
// Temporary variables to store data in each iteration
Employee temp;
string tempName;
string tempPost;
int tempID;
long int tempSal;
cout << "Name of the employee " << i + 1 << ": ";
getline(cin, tempName);
// Clearing the 'cin' in order to prevent the getline skips
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "Post of the employee: ";
getline(cin, tempPost);
cout << "Employee ID: ";
cin >> tempID;
cout << "Total salary: ";
cin >> tempSal;
// Initializing the temporary object
temp.initEmployee(tempID, tempName, tempPost, tempSal);
// Pushing the object into the main vector
emp.push_back(temp);
}
// Comparing the vector elements (note: it is a friend function)
maxSalary(emp);
return 0;
}
As a test case:
Enter the total number of employees: 2 // Number of employees
Name of the employee 1: John Doe // Employee 1
Post of the employee: Manager
Employee ID: 100
Total salary: 15000
Name of the employee 2: Max Ville // Employee 2
Post of the employee: Assitant Manager
Employee ID: 102
Total salary: 50000
Maximum salary: 50000 // Maximum salary

Calling out the public member variables

This is my class function
class Employee
{
private:
string ename;
double esalary;
public:
Employee(string nm = "", double sal = 0.0)
{
ename = nm;
esalary = sal;
}
string getName()
{ return ename;}
double getSalary()
{ return esalary;}
};
#endif
and now my incomplete body...
#include "employee.h"
using namespace std;
Employee read_employee()
{
string name;
cout << "Please enter the name: ";
getline(cin, name);
double salary;
cout << "Please enter the salary: ";
cin >> salary;
Employee r(name, salary);
return r;
}
int main()
{
Employee emp(string name,double salary);
read_employee();
}
I am wondering how do i call the "getName or getSalary" functions from the class. I am used to the class objects without parameters.
Try this:
Employee emp = read_employee();
Instead of:
Employee emp(string name,double salary);
read_employee();
And then you can say:
emp.getName() and emp.getSalary()
Your problem is that your read_employee() function is a global function. That is, it's not necessarily attached to any specific Employee instance. If you added it to your Employee class, things might work a little better:
class Employee
{
private:
string ename;
double esalary;
public:
Employee(string nm = "", double sal = 0.0)
{
ename = nm;
esalary = sal;
}
string getName()
{ return ename;}
double getSalary()
{ return esalary;}
void read_employee()
{
cout << "Please enter the name: ";
getline(cin, ename);
cout << "Please enter the salary: ";
cin >> esalary;
}
};
And then in main:
#include "employee.h"
using namespace std;
int main()
{
Employee emp;
emp.read_employee();
cout << emp.getName() << endl;
cout << emp.getSalary() << endl;
}
Disclaimer: I'm not doing any error checking at all, so we just have to assume the user will play nice with his/her inputs.
As Chris mentions, you are not assigning the return values of your functions to anything. You want something like:
Employee emp = read_employee("Johnny", 45000);
Which will create an employee object in your function, then return it. That returned object is then assigned to emp.

How to pass variables to a class

I have been working on this code for quite some time now and I had posted it before but then after fixing that problem another problem arose so I created a new post with the name of this problem. Ok the problem is that I am obviously not passing the variables to the Administrator class the right way. I have tried two ways which is all my book shows and both have given me an error that says error C2512: 'SalariedEmployee' : no appropriate default constructor available". I have tried
//Lynette Wilkins
//Week 12
#include <iostream>
#include <cstdlib>
#include <string>
#include <iomanip>
#include <cmath>
using namespace std;
class SalariedEmployee
{
private:
double wageRate;
int hours;
protected:
string name;
string ssn;
double netPay;
string department;
public:
SalariedEmployee(string n, string s, double np, double w, int h, string d);
~SalariedEmployee() {cout<<endl;}
string Getname(); //returns name
string Getssn(); // returns social security number
double GetnetPay(); //returns netPay
string Getdepartment(); // returns department
double GetwageRate(); //returns wage rate
int Gethours(); //returns hours
void Setname(string); //sets name
void Setssn(string); //sets ssn
void SetnetPay(double); //sets net pay
void Setdepartment(string); //sets department
void SetwageRate(double); //sets wage rate
void Sethours(int); //sets hours
};
SalariedEmployee::SalariedEmployee(string n, string s, double np, double w, int h, string d) : name(n),
ssn(s),
netPay(np),
wageRate(w),
hours(h),
department(d)
{}
string SalariedEmployee::Getname()
{
return name;
}
string SalariedEmployee::Getssn()
{
return ssn;
}
double SalariedEmployee::GetnetPay()
{
return netPay;
}
double SalariedEmployee::GetwageRate()
{
return wageRate;
}
int SalariedEmployee::Gethours()
{
return hours;
}
void SalariedEmployee::Setname(string n)
{
name = n;
}
void SalariedEmployee::Setssn(string s)
{
ssn = s;
}
void SalariedEmployee::SetnetPay(double np)
{
netPay = np;
}
void SalariedEmployee::Setdepartment(string d)
{
department = d;
}
void SalariedEmployee::SetwageRate(double w)
{
wageRate = w;
}
void SalariedEmployee::Sethours(int h)
{
hours = h;
}
class Administrator : public SalariedEmployee
{
protected:
string title;
string responsi;
string super;
double salary;
public:
Administrator(string t, string r, string s, double sa);
~Administrator();
string Gettitle();
string Getresponsi();
string Getsuper();
double Getsalary();
void Settitle(string);
void Setresponsi(string);
void Setsuper(string);
void Setsalary(double);
void print();
};
Administrator::Administrator(string t, string r, string s, double sa) : title(t), responsi(r), super(s), salary(sa)
{
}
Administrator::~Administrator()
{
cout<<endl;
}
string Administrator::Gettitle()
{
return title;
}
string Administrator::Getresponsi()
{
return responsi;
}
string Administrator::Getsuper()
{
return super;
}
double Administrator::Getsalary()
{
return salary;
}
void Administrator::Settitle(string ti)
{
title = ti;
}
void Administrator::Setresponsi(string re)
{
responsi = re;
}
void Administrator::Setsuper(string su)
{
super=su;
}
void Administrator::Setsalary(double sa)
{
salary= sa;
}
void Administrator::print( )
{
cout << "\n_______________________________________________\n";
cout << "Pay to the order of " << name<< endl;
cout << "The sum of " << netPay << " Dollars\n";
cout << "_________________________________________________\n";
cout <<endl<<endl;
cout << "Employee Number: " << ssn << endl;
cout << "Salaried Employee. Regular Pay: "
<< salary << endl;
cout << "_________________________________________________\n";
}
int main()
{
string name;
string soc;
double net = 0;
double wage = 0;
int hrs = 0;
string dept;
string admtitle;
string resp;
string sup;
double sal = 0;
int response = 0;
string date = "January 12, 2013";
cout<<setprecision(2)
<<setiosflags(ios::fixed)
<<setiosflags(ios::showpoint);
SalariedEmployee emp1(name, soc,net, wage, hrs, dept);
Administrator adm1(admtitle, resp, sup, sal);
while(response != 4){
cout<<"Employee and Administrator Salary Program "<<endl;
cout<<"(You will have to enter data first before you do anything else)"<<endl<<endl;
cout<<"Enter Employee Data, Enter 1"<<endl;
cout<<"Change data, Enter 2"<<endl;
cout<<"Print Check, Enter 3"<<endl;
cout<<"End Program, Enter 4"<<endl<<endl;
cout<<"Please make your selection"<<endl;
cin>> response;
switch (response)
{
case 1:
cout <<"The employee's data will be entered here: "<<endl<<endl;
cout<<"Enter the employees name: ";
cin.ignore();
getline(cin, name);
cout<<"Enter the employees social security number: ";
cin.ignore();
getline(cin, soc);
cout<<"Enter the employees net pay: ";
cin>>net;
cout<<"Enter the employees wage rate: ";
cin>>wage;
cout<<"Enter the number of hours the employer worked: ";
cin>>hrs;
cout<<"Enter the employees title: ";
cin.ignore();
getline(cin,admtitle);
cout<<"Enter the employees area responsibility: ";
cin.ignore();
getline(cin, resp);
cout<<"Enter the employees salary: ";
cin>>sal;
cout<<endl<<endl<<endl;
break;
case 2:
cout<<"Please change the data you entered previously here. " <<endl<<endl;
cout<<"Enter the employees name: ";
cin.ignore();
getline(cin, name);
cout<<"Enter the employees social security number: ";
cin.ignore();
getline(cin, soc);
cout<<"Enter the employees net pay: ";
cin>>net;
cout<<"Enter the employees wage rate: ";
cin>>wage;
cout<<"Enter the number of hours the employer worked: ";
cin>>hrs;
cout<<"Enter the employees title: ";
cin.ignore();
getline(cin,admtitle);
cout<<"Enter the employees area responsibility: ";
cin.ignore();
getline(cin, resp);
cout<<"Enter the employees salary: ";
cin>>sal;
cout<<endl<<endl<<endl;
break;
case 3:
cout <<"Information Printed"<<endl<<endl;
cout<<"_____________________________"<<date<<endl;
&Administrator::print;
break;
default:
cout<<endl<<endl
<<"Invalid Selection! Try Again"<<endl;
exit(1);
}
}
system("PAUSE");
return 0;
}
Administrator(string t, string r, string s, double sa); will attempt to call the default constructor of the base class if you don't specify another. (a default constructor is one that can be called without any arguments)
The base class doesn't have a default constructor, ergo the error.
To call another constructor of the base class:
Administrator::Administrator(string t, string r, string s, double sa) :
SalariedEmployee(<args>), //base constructor call
title(t), responsi(r), super(s), salary(sa) //members
{
}