Question about virtual functions and...

Hi, I am taking a C++ class. The subject matter has been classes, pointers, inheritance, polymorphism, streams, recursion and operator overloading. The last three I had little problem with. The problem below is an inheritance problem. But, I couldn't get it to compile. I was hoping someone might look at it and help me get it to compile. The deadline for submittal has already passed so you won't really be doing to for me. I just need to find why I am getting errors. I am hopelessly confused inheritance and polymorphism and with the final 1.5 weeks hence I need to get this stuff right. Thank you very much. I have also pasted the compile errors.


// Problem P8.6

#include <string>
#include <iostream>
#include <stdlib.h>
#include <stdio.h>

using namespace std;

class Worker
{
public:
Worker(string name, double salary, double hours_worked);
string get_salary() const;
string get_hours() const;
private:
double salary;
double hours_worked;
};
class HourlyWorker : public Worker
{
public:
HourlyWorker();
virtual double ComputePay() const;
private:
};
double HourlyWorker::ComputePay() const
{
double OT = 0; // Overtime hours
double RT = 40;// Regular hours
if (double hours_worked > 40.) OT = Worker::get_hours() - 40.;
if (hours_worked <= 40.) RT = Worker::get_hours();
return OT * Worker::get_salary() * 1.5 + RT * Worker::get_salary();
};
class SalariedWorker : public Worker
{
public:
SalariedWorker();
virtual double ComputePay();
private:
};
double SalariedWorker::ComputePay() const
{
return Worker.get_salary();
};


Error 1 error C2059: syntax error : '>' c:\users\mark\documents\master's degree\c++\week 02\assignment\chapter 8\p8.6\p8.6\p8.6.cpp 36 P8.6
Error 2 error C2248: 'Worker::hours_worked' : cannot access private member declared in class 'Worker' c:\users\mark\documents\master's degree\c++\week 02\assignment\chapter 8\p8.6\p8.6\p8.6.cpp 37 P8.6
Error 3 error C2440: '=' : cannot convert from 'std::string' to 'double' c:\users\mark\documents\master's degree\c++\week 02\assignment\chapter 8\p8.6\p8.6\p8.6.cpp 37 P8.6
Error 4 error C2677: binary '*' : no global operator found which takes type 'std::string' (or there is no acceptable conversion) c:\users\mark\documents\master's degree\c++\week 02\assignment\chapter 8\p8.6\p8.6\p8.6.cpp 38 P8.6
Error 5 error C2677: binary '*' : no global operator found which takes type 'std::string' (or there is no acceptable conversion) c:\users\mark\documents\master's degree\c++\week 02\assignment\chapter 8\p8.6\p8.6\p8.6.cpp 38 P8.6
Error 6 error C2511: 'double SalariedWorker::ComputePay(void) const' : overloaded member function not found in 'SalariedWorker' c:\users\mark\documents\master's degree\c++\week 02\assignment\chapter 8\p8.6\p8.6\p8.6.cpp 48 P8.6
Insane greetings to the insane forum of insane geeks are distributed, insanely.

Please use code tags.

Warning: your doubles in HourlyWorker will be set to zero unless you put in .0 after the values. All of them.

http://cplusplus.com/doc/tutorial/inheritance/
This is why you can't access hours_worked from HourlyWorker.

I'd set get_hours() and get_salary() to return double if I were you.

http://cplusplus.com/doc/tutorial/polymorphism/
I'd also ditch the const to the right of your virtual function and the virtual to the left of your computerPay() in SalariedWorker().

-Albatross

EDIT: Insane Welcomings.
Last edited on
Topic archived. No new replies allowed.