Sign in to follow this  

Help with Abstract classes and Pure virtual functions!

This topic is 4033 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I thought that I would come up with a C++ program to use for work. What I am trying to do is be able to enter the First and last name of employees, their ID number, hours worked and any bonus pay. I tried to tinker with an example from a book at my local bookstore, but not sure what is wrong with it. The code is below and I'm not sure what I need to do. I tried using it in bloodshed, but I will try it in Visual Basic once I get home from work.
#include "personType.h"

class employeeType: public personType
{
public:
        virtual void print() const = 0; //Used to output data for the employees
        virtual double calculatePay() const = 0; //This is for the calculation and return of pay
        void setId(long id); //This is used for salary of full time employees
        long getId() const; //For identifying employees types
        employeeType(string first = "", string last = "" ,
                                 long id = 0;
//This should display the employee's names and payRate with ours worked.

private:
       Long personId; //This will store the employee's Id
};
void employeeType::setId(long id)
{
     personId = id;
}

long employeeType::getId() const
{
     return personID;
}

employeeType::employeeType(string first, string last, long id)
                                  :personType(first, last)
{
     person = id;
}

#include "employeeType.h"

class fullTimeEmployee: public employeeType
{
public:
       void set(string first, string last, long id, double salary, double bonus);
       void setSalary(double salary); //used to set the salary
       double getSalary(); //used to get the salary
       void setBonus(double bonus); //used to set the bonus
       double getBonus(); //used to get the bonus
       void print() const; // used to output first and last name along with money earned
       double calculatePay() const; //calculates and returns wages
       fullTimeEmployee(string first = "", string last = "",
                        long id = 0, double salary = 0,
                        double bonus = 0);
            
private:
        double empSalary;
        double empBonus;
};

void fullTimeEmployee::set(string first, string last,
                           lost id,
                           double salary, double bonus)
{

        setName(first, last);
        setId(id);
        empSalary = salary
        empBonus = bonus;
}

void fullTimeEmployee::setSalary(double salary)
{
     empSalary;
}

double fullTimeEmployee::getSalary
{
       return empSalary;
}

void fullTimeEmployee::setBonus(double bonus)
{
     empBonus = bonus;
}

void fullTimeEmployee::print() const
{
     cout << "Id: " << getId() << endl;
     cout << "Name: ";
     personType::print();
     cout << endl;
     cout << "Wages: $" << calculatePay() << endl;
}

double fullTimeEmployee::calculatePay() const
{
       return empSalary + empBonus;
}

fullTimeEmployee::fullTimeEmployee(string first, string last,
                                   long id, double salary,
                                   double bonus)
                : employeeType(first, last, id)
{
     empSalary = salary;
     empBonus = bonus;
}

#include "employeeType.h"

class partTimeEmployee: public employeeType
{
public:
    void set(string first, string last, long id, double rate,
             double hours);
    double calculatePay() const;
    void setPayRate(double rate); //sets the salary
    double getPayRate(); //retrieves the salary
    void setHoursWorked (double hours); //to set the bonus
    double getHoursWorked(); //retrieves the bonus
    void print() const;
    
    partTimeEmployee(string first = "", string last = "",
                     long id = 0,
                     double rate = 0, double hours = 0);

private:
    double payRate; //stores the pay rate
    double hoursWorked;
};

void partTimeEmployee:;set(string first, string last, long id,
                           double rate, double hours)
{
    setName(first, last);
    setId(id);
    payRate = rate;
    hoursWorked = hours;
}

void partTimeEmployee::setPayRate(double rate)
{
     payRate = rate;
}

double partTimeEmployee::getPayRate()
{
     return payRate;
}

void partTimeEmployee::setHoursWorked(double hours)
{
     hoursWorked = hours;
}

double partTimeEmployee::getHoursWorked()
{
       return hoursWorked;
}

void partTimeEmployee::print() const
{
     cout << "Id: " << getId() << endl;
     cout << "Name: ";
     personType::print();
     cout << endl;
     cout << "Wages: $" << calculatePay() << endl;
}

double partTimeEmployee::calculatePay() const
{
       return (payRate * hoursworked);
}

partTimeEmployee::partTimeEmployee(string first, string last,
                                   long id,
                                   double rate, double hours
                 : employeeType(first, last, id)
{
       payRate = rate;
       hoursWorked = hours;
}

#include <iostream>
#include "partTimeEmployee.h"
#include "fullTimeEmployee.h"

int main()
{
    fullTimeEmployee newEmp(" First", "Last", ID, Pay, Bonus);
    partTimeEmployee tempEmp("First1", "Last1", ID1, Pay1, Bonus1);
    
    newEmp.print();
    cout << endl;
    tempEmp.print();
    
    return 0;
}
[\source]

Share this post


Link to post
Share on other sites
So, what exactly is the problem? (What are the compile errors you're getting, if any, what parts are you confused about, etc.)

I just very quickly skimmed your code, and I saw one potential problem - a wayward semicolon in a member function definition:
void partTimeEmployee:;set(string first, string last, long id,
double rate, double hours)
{
// ....
}

Didn't see any other obvious problems, but I don't have the time to just take your entire code and tinker with it, especially since I don't have the definitions in personType.h.

Aside from actual problems, I would take stylistic issue with ending all your class names with "Type" (think about it - an instance of fullTimeEmployeeType isn't a type, it's an actual employee), and beginning them with lowercase letters, but that has nothing to do with whether or not it works. [smile]

Share this post


Link to post
Share on other sites
Without a description of what your problem actually is, it is significantly harder help you. However, you do need something to keep the window open after the program finishes. system("PAUSE"); works but is not cross platform, and is almost forbidden on these forums. Some form of cin works pretty well, to just wait for input before continuing.

Share this post


Link to post
Share on other sites
thanks guys, sorry I did not expect such quick replies! I will try to compile it again tomorrow and will post the errors that come up. I am still at work right now, but I will post that stuff up tomorrow and hopefully you guys can help me out.

Share this post


Link to post
Share on other sites

This topic is 4033 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this