?
Solved

C++ Classes/Inheritance

Posted on 2006-04-16
6
Medium Priority
?
276 Views
Last Modified: 2013-12-14
I have to create a class (Employee) and a second class hourly that will inherit from the employee class.  Then write code to test the hourly class.  I have reached about as far as I can go.  I am getting an error and not sure why.  Please help.


#ifndef H_employee
#define H_employee

#include <fstream>
#include <string>

using namespace std;

class employee
{
private:
      string fName;
      string lName;
      string ID;

public:
      employee();
      employee(string FN, string LN, string ID);
      string getName() const;
      string getID() const;
      void setName(string FN, string LN);
      void setID(string Ident);
      ~employee();
};

#endif

#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include "employee.h"

using namespace std;

employee::employee(string FN, string LN, string ID)
{
      fName = FN;
      lName = LN;
      ID = ID;
}

void employee::setName(string FN, string LN)
{
      fName = FN;
      lName = LN;
}

void employee::setID(string Ident)
{
      ID = Ident;
}

#ifndef H_hourlyEmployee
#define H_hourlyEmployee

#include <fstream>
#include <string>

using namespace std;

class HourlyEmployee : public employee
{
private:
      double Rate;
      double Hours;

public:
      HourlyEmployee();
      HourlyEmployee(string FN, string LN, string ID,
      double R, double H);
      double getRate() const;
      double getHours() const;
      ~HourlyEmployee();
};

#endif

#include <iostream>
#include <string>
#include "hourlyEmployee.h"

using namespace std;

HourlyEmployee::HourlyEmployee()
{
      setName("LLLLLLLLLLL", "FFFFFFFFFF");
      setID("000-00-0000");
      Rate = 0.0;
      Hours = 0.0;
}

double getRate() const
{
      return rate;
}

double getHours() const
{
      return hours;
}


#include "employee.h"
#include "HourlyEmployee.h"

void PrintEmployee(Employee toPrint, ostream& Out);
void PrintHourlyEmployee(HourlyEmployee toPrint, ostream& Out);

void main()
{
      employee Me("Tony", "Stewar", "202-20-2020");
      PrintEmployee(Me, cout);
      HourlyEmployee Steve("Steve", "Simpson", "000-00-0001",
      13.42, 7.5);
      PrintHourlyEmployee(Steve, cout);
      PrintEmployee(Steve, cout);
}


void PrintEmployee(employee toPrint, ostream& Out)
{
      Out << toPrint.getID();
      Out << '\t';
      Out << toPrint.getName();
      Out << '\n';
}

void PrintHourlyEmployee(HourlyEmployee toPrint, ostream& Out)
{
      Out.setf(ios::floatfield, ios::fixed);
      Out.setf(ios::showpoint);
      Out << toPrint.getID();
      Out << '\t';
      string Name = toPrint.getName();
      Out << Name;
      Out << setw(30 - Name.length())
      << setprecision(2) << toPrint.getRate();
      Out << setw(10) << setprecision(2) << toPrint.getHours();
      Out << '\n';
}


HourlyEmployee::HourlyEmployee()
: Employee("Anonymous", "Hourly", "777-77-7777") {
FName = "Anonymous";
LName = "Person";
ID = "000-00-0000";
Rate = 0.0;
Hours = 0.0;
}


HourlyEmployee::HourlyEmployee(string FN, string LN, string ID,
double R, double H)
: Employee(FN, LN, ID), Rate(R), Hours(H) {
}

0
Comment
Question by:joew08
6 Comments
 
LVL 30

Expert Comment

by:Axter
ID: 16466462
>>I am getting an error and not sure why.

What exactly is your error?
0
 
LVL 2

Expert Comment

by:bachra04
ID: 16466601
are yu using Visual C when you get the error ?
0
 
LVL 2

Expert Comment

by:seet82
ID: 16466848
what you are doing is like going to the clinic and tell the doc that you are not feeling well but not telling him how/where you are not feeling well

no one can help u if you don't tell other ppl what your error is

anyway, browse thru a bit of your code. the below code will not work
employee::employee(string FN, string LN, string ID)
{
     fName = FN;
     lName = LN;
     ID = ID; //should be replaced by this->ID=ID;
}
ID=ID is setting ID to ID, ie, setting the passed in ID to ID(which does nothing)
the correct way to do it is this->ID=ID;
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 1

Expert Comment

by:pcgabe
ID: 16467295
You're getting AN error?  You should be getting DOZENS of errors.  Well, that's why you come to Experts-Echange, right?  ^_^

OK, this code needs a LOT of work.  A LOT A LOT.  It has so many inconsistencies....are you sure this was all written by a single person?

Here's a list of things to do:

#1) Remember: CASE MATTERS in C/C++.  Make sure that, once you define a class or variable (such as 'employee') you don't later refer to it by a different capitalization (such as "Employee").
#2) Spelling matters.  'fName' is not 'FName' is not 'FN'.
#3) If you declare it, you MUST define it.  I see a 'public: employee();' constructor declaration, but it is never implemented anywhere.
#4) When you implement the functions in a class, you have to tell the compiler that that is what you are doing.  'double getRate() const', for example, seems a bit vague to the poor confused compiler.  'double HourlyEmployee::getRate() const' would clear that up.

Finally, if you are going to post multiple source files, give us their names and where they begin and end.

Now, this doesn't list every bug in the above code (look at the 'HourlyEmployee::HourlyEmployee()' constructor), but it's a start.  ^_^  Fix these, and come back and post your updated code, and we'll help you some more.
0
 
LVL 9

Accepted Solution

by:
DrAske earned 1200 total points
ID: 16467636
class employee
{
private:
     string fName;
     string lName;
     string ID;

public:
     employee(); <-------- Must Be Defined
     employee(string FN, string LN, string ID);
     string getName() const; <---------  Must Be Defined
     string getID() const;      <---------  Must Be Defined
     void setName(string FN, string LN);
     void setID(string Ident);
     ~employee();     <---------- Must Be Defined
};

employee::employee(string FN, string LN, string Id)
{
     fName = FN;
     lName = LN;
     ID = Id; <-------- MODIFEID or use this->ID = ID; as seet82 said
}


class HourlyEmployee : public employee
{
private:
     double Rate;
     double Hours;

public:
     HourlyEmployee();
     HourlyEmployee(string FN, string LN, string ID,
     double R, double H);
     double getRate() const;
     double getHours() const;
     ~HourlyEmployee();<------ Remember the member functions must be defined
};
// When defining a class's member function outside the class, omiiting the class NAME and scope resolution operator on
//the function name is an ERROR!!
double HourlyEmployee::getRate() const // MODIFIED
{
      //return rate; MODIFIED
      return Rate;
}
double HourlyEmployee::getHours() const // MODIFIED
{
     //return hours; MODIFIED
      return Hours;
}

void PrintEmployee(Employee toPrint, ostream& Out); <-- first parameter Employee should be employee (case sensitive)

// Omit this function .. you have already defined HourlyEmployee constuctor
HourlyEmployee::HourlyEmployee()
: Employee("Anonymous", "Hourly", "777-77-7777") {
FName = "Anonymous";
LName = "Person";
ID = "000-00-0000";
Rate = 0.0;
Hours = 0.0;
}
// instead of Employee(FN, LN, ID) ---> employee(FN, LN, ID)
HourlyEmployee::HourlyEmployee(string FN, string LN, string ID,
double R, double H)
: Employee(FN, LN, ID), Rate(R), Hours(H) {
}

regards, ahmad;
0
 
LVL 30

Expert Comment

by:Axter
ID: 16468798
Please copy and paste the error, instead of trying to translate it.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question