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.
Related
I have 2 separate classes in C++,which are Algorithms class and Student class, each of have the following members and methods.
#include <iostream>
using namespace std;
#define MAX 10
class Student {
private:
int ID;
string name;
string surname;
int quiz_scores[4];
public:
Student()
{
ID = 0;
name = "" ;
surname = "";
quiz_scores[4] = {0};
}
void setID(int ID_set);
int getID();
void setName(string name_set);
string getName();
void setSurName(string surname_set);
string getSurName();
void setQuizScores(int* quizscores);
const int* getQuizScores() const;
};
and Algorithms class as follows:
class Algorithms{
private:
Student students[MAX];
int num =0 ; // The current number of students in the course, initially 0.
float weightQ;
float weightHW;
float weightF;
public:
Algorithms()
{
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);
And here is the methods declerations of Student class and Algorithms class respectively.
// 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;
}
void Student :: setQuizScores(int* quizscores){
for(int i = 0; i<4; i++){
quiz_scores[i] = quizscores[i];
}
}
const int* Student :: getQuizScores() const {
return quiz_scores; }
// Method declerations for the class Algorithms
int Algorithms:: getNum(){
return num;
}
void Algorithms :: addNewStudent(Student new_student){
students[num] = new_student ;
num = num + 1;
}
void Algorithms :: updateWeights(float weightQ_update, float weightHW_update, float weightF_update){
weightQ = weightQ_update;
weightHW = weightHW_update;
weightF = weightF_update;
}
void Algorithms :: getStudentInfo(int ID_given, Algorithms &algorithms){
for(int i = 0; i<MAX; i++){
if(ID_given == students[i].getID()){
cout << "Student Name & Surname : " << students[i].getName() << " " << students[i].getSurName()<<"\n";
cout << "Quiz results :" << students[i].getQuizScores();
}
}
}
I have also user-interface in the main. It calls the following functions
void addNewStudent(int ID, string name, string surname, Algorithms &algorithms){
Student student;
student.setID(ID);
student.setName(name);
student.setSurName(surname);
algorithms.addNewStudent(student);
}
void showStudent(int ID, Algorithms &algorithms){
algorithms.getStudentInfo(ID, algorithms);
}
Both work properly with the code in main as below
int main(){
Algorithms ECE101;
int x;
int ID;
string name, surname;
string option_1 = "1) Add a student ";
string option_2 = "2) Search a student by ID";
string option_3 = "3) Change a student’s score";
cout << "Welcome to the ECE101 Classroom Interface"<<"\n";
cout << "Choose your option\n";
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 if (x==2){
cout << "Enter the student ID\n";
cin >> ID;
showStudent(ID, ECE101);
}
else {
int quiz_grades[4];
cout << "Enter the student ID";
cin >> ID;
cout << endl;
cout << "Enter the quiz grades" << endl;
for (int i = 0 ; i<4 ; i++) {
cin >> quiz_grades[i];
}
changeStudentScores(ID, quiz_grades);
}
The problem here comes from changeStudentScores(ID, quiz_grades, ECE101)
What I want to do is the program should take the array of 4 numbers(corresponds to the quiz grades) and set to the student, whose ID's given by the user. (Of course, firstly the student should be added to the course by the option 1) BUT, I could not pass the array to the changeStudentScores , where it is implemented by
void changeStudentScores(int ID, int* quizscores ){
// Problem occurs here, creating new students object is not so logical, but I tried.
Student student;
student.setID(ID);
student.setQuizScores();
// I just try to pass the quiz grades writing by the user to the set method, which set quiz grades to the students.
After choose the option 2 which shows the information about the students given the ID, I see the correct name and surname, but I could not update the quiz grades.
RESTRICTIONS:
There should be no other method and data members other than the written methods and data members.
But we can implement additional methods and functions (but not class data
members)
getStudentInfo(int ID_given, Algorithms &algorithms) function must return student information given ID, but my implementation I could use with void
QUESTIONS:
How can I implement correctly the changeStudentScores function so that the user can update the existing student quiz score?
My getStudentInfo(int ID_given, Algorithms &algorithms) function returns nothing that I can use for getting information. So need to be returned the student information, so the return type must not be void.
What is the general idea behind updating the members of the objects, which are used in another class with some data structure(here is array.)
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.
I wanna ask you quick question. How can get the name, surname, id and age as an input? These are must be taken as input and variables must be private. How should I code about it?
#include <iostream>
using namespace std;
class Employee{
private:
string Name;
string Surname;
int IdNumber;
int age;
public:
Employee(string isim,string soyisim,int idnumarasi,int yas){
Name = isim;
Surname = soyisim;
IdNumber = idnumarasi;
age = yas;
}
void printEmployee(){
cout << Name << endl;
cout << Surname << endl;
cout << IdNumber <<endl;
cout << age << endl;
}
};
int main() {
Employee employee("John","Lares",12,25);
employee.printEmployee();
return 0;
}
Edited version. I used getline but still cant reach the private variables. How can reach the private variables. Is the only way getter setter functions?
#include <iostream>
using namespace std;
class Employee{
private:
string Name;
string Surname;
int IdNumber;
int age;
public:
Employee(){
Name = isim;
Surname = soyisim;
IdNumber = idnumarasi;
age = yas;
}
void printEmployee(){
cout << Name << endl;
cout << Surname << endl;
cout << IdNumber <<endl;
cout << age << endl;
}
};
int main() {
Employee employee();
getline(cin,employee.Name);
getline(cin,employee.Surname);
getline(cin,employee.IdNumber);
getline(cin,employee.age);
employee.printEmployee();
return 0;
}
One simple way is to use the constructor you have defined
int main()
{
string name, surname;
int id, age;
cin >> name >> surname >> id >> age; // read user input
Employee someone(name, surname, id, age); // create employee from user input
...
}
Using a constructor is the normal way to give class member variables their initial values.
Another more complicated way is to overload operator>>
For class I have to adapt a program I wrote last week for polymorphism. Last week it used a specific set of information for the employees but now I have to make it work with polymorphism as well as read/write data from a file, I am completely lost with what I am supposed to be doing, If someone could even point me in the right direction it would be so much help. I can post my current .h and .cpp file for a look at what I have as well as the instructions of what I am supposed to be doing.
.h
#pragma once
#include <string>
using namespace std;
class Employee {
private:
int employeeNumber; // Employee's employee number
string employeeName; //Employee's name
string streetAddress; //Employee's street address
string phoneNumber; //Employee's phone number
double hourlyWage; //Employee's hourly wage
double hoursWorked; //Employee's hours worked
double netPay; //Net pay
double grossPay; //Gross pay
public:
Employee();
Employee(int, string, string, string, double, double);
int getEmployeeNumber();
void setEmployeeNumber(int);
string getEmployeeName();
void setEmployeeName(string);
string getStreetAddress();
void setStreetAddress(string);
string getPhoneNumber();
void setPhoneNumber(string);
double getHourlyWage();
void setHourlyWage(double);
double getHoursWorked();
void setHoursWorked(double);
double calcPay()
{
const int OVER = 40;
double federal = 0.20;
double state = 0.075;
double timeHalf = 1.5;
double grossPay;
double netPay;
if (getHoursWorked() < OVER)
{
grossPay = getHoursWorked() * getHourlyWage();
netPay = grossPay - (grossPay * federal) - (grossPay * state);
}
if (getHoursWorked() >= OVER)
{
grossPay = getHoursWorked() * ((getHourlyWage() * timeHalf));
netPay = grossPay - (grossPay * federal) - (grossPay * state);
}
return netPay;
}
};
.cpp
#include <iostream>
#include <string>
#include <fstream>
#include "Employee.h"
#include <iomanip>
using namespace std;
Employee::Employee()
{
employeeNumber = 0; // Employee's employee number
employeeName = ""; //Employee's name
streetAddress = ""; //Employee's street address
phoneNumber = ""; //Employee's phone number
hourlyWage = 0; //Employee's hourly wage
hoursWorked = 0;
grossPay = 0;
netPay = 0;
}
Employee::Employee(int empNum, string empName, string streetAddress,
string phoneNumber, double hourlyWage, double hoursWorked)
{
employeeNumber = empNum;
employeeName = empName;
this->streetAddress = streetAddress;
this->phoneNumber = phoneNumber;
this->hourlyWage = hourlyWage;
this->hoursWorked = hoursWorked;
grossPay = 0;
netPay = 0;
}
int Employee::getEmployeeNumber()
{
return employeeNumber;
}
void Employee::setEmployeeNumber(int empNum)
{
employeeNumber = empNum;
}
string Employee::getEmployeeName()
{
return employeeName;
}
void Employee::setEmployeeName(string empName)
{
employeeName = empName;
}
string Employee::getStreetAddress()
{
return streetAddress;
}
void Employee::setStreetAddress(string strtAddrs)
{
streetAddress = strtAddrs;
}
string Employee::getPhoneNumber()
{
return phoneNumber;
}
void Employee::setPhoneNumber(string phnNum)
{
phoneNumber = phnNum;
}
double Employee::getHourlyWage()
{
return hourlyWage;
}
void Employee::setHourlyWage(double hrWage)
{
hourlyWage = hrWage;
}
double Employee::getHoursWorked()
{
return hoursWorked;
}
void Employee::setHoursWorked(double hrWorked)
{
hoursWorked = hrWorked;
}
void printCheck(Employee ee)
{
cout << "\n\n--------------------- Fluff Shuffle Electronics -------------------------------- \n";
cout << " Pay to the order of " << ee.getEmployeeName() << "...........................$" << ee.calcPay();
cout << "\n\n United Bank of Eastern Orem \n";
cout << "------------------------------------------------------------------------------- \n";
cout << " Hours Worked: " << ee.getHoursWorked();
cout << "\n Hourly Wage: " << ee.getHourlyWage();
cout << endl << endl;
}//End of function
void read(ifstream &in)
{
Employee employees[10];
int counter = 0;
while (in.read((char *)&employees[counter++], sizeof(Employee)))
for (int i = 0; i<counter; i++)
{
printCheck(employees[i]);
}
in.close();
}
void write(ofstream &out)
{
Instantiate your employees here first, then call their functions.
Employee joe(37, "Joe Brown", "123 Main St.", "123-6788", 10.00,
45.00);
printCheck(joe);
Employee sam(21, "Sam Jones", "45 East State", "661-9000", 12.00,
30.00);
printCheck(sam);
Employee mary(15, "Mary Smith", "12 High Street", "401-8900", 15.00, 40.00);
printCheck(mary);
out.write((char *)(&joe), sizeof(Employee));
out.write((char *)(&sam), sizeof(Employee));
out.write((char *)(&mary), sizeof(Employee));
out.close();
}
//Main function
int main()
{
int choice;
string filename;
while (true)
{
cout << "\nThis program has two options:\n";
cout << "1 - Create a data file, or\n";
cout << "2 - Read data from a file and print paychecks\n";
cout << "\n Press any other key to quit..........\n";
cout << "Please enter <1> to create a file or <2> to print
checks: ";
cin >> choice;
if (choice == 1)
{
cout << "Enter the file name: ";
cin >> filename;
ofstream out(filename);
out.open(filename.c_str(), ios::binary);
write(out);
}
else if (choice == 2)
{
cout << "Enter the file name: ";
cin >> filename;
ifstream in(filename);
in.open(filename.c_str(), ios::binary);
read(in);
}
else break;
//Calls function to displays information
}
}//End of main
These are the instructions for the project.
This is the diagram it refers to
To start: create two classes derived from Employee:
class HourlyEmployee: public Employee
{
};
class SalariedEmployee: public Employee
{
}
and move members related to Hourly working from Employee to HourlyEmployee, then add members related to Salary to SalariedEmployee (WeeklySalary).
This way (removing attributes related to hourly working) you make Employee class more general that can be a base for other kind of employees to (SalariedEmployee).
When you derive HourlyEmployee or SalariedEmployee from Employee, you mean they are kind of Employee, so members that Employee has, they will inherit automatically.
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)
{
}