C++ Newbie - Derived Class Issue

This is a discussion on C++ Newbie - Derived Class Issue within the C++ Programming forums, part of the General Programming Boards category; I'm doing an assignment for a C++ class and I'm having some serious issues understanding the topic. The assignment was ...

  1. #1
    Registered User
    Join Date
    Jul 2007
    Posts
    13

    C++ Newbie - Derived Class Issue

    I'm doing an assignment for a C++ class and I'm having some serious issues understanding the topic. The assignment was to make a class called Employee..then make a class called Payroll that is derived from Employee. I think I am doing something wrong with my syntax but I can't figure out what it is. I have the employee class working.....but when I try to do anything with the derived class it gives a compile error. What am I doing wrong?

    I know that the employee class is working and it doesn't seem to matter what I put into the payroll class (constructor or otherwise...it all gives the same error).

    Here are my class declarations...

    Thanks in advance!

    Phil

    p.s. The error I'm getting says "Unresolved External Symbol"
    -------------------

    Code:
    class Employee{
    
    protected:
    char name[50];
    char ssn[11];
    char empId[5];
    char hireDate[10];
    
    public:
    Employee();
    Employee(char social[], char EmpId[], char HireDate[], char Name[]){
    setSocial(social);
    setEmpId(EmpId);
    setHireDate(HireDate);
    setName(Name);
    }
    bool setSocial(char[]);
    void printSocial();
    bool setEmpId(char[]);
    void printEmpId();
    bool setHireDate(char[]);
    void printHireDate();
    bool setName(char[]);
    void printName();
    };
    
    class Payrollublic Employee{
    
    protected:
    float payRate;
    float overtimeRate;
    int hoursworked;
    
    public:
    Payroll(float rate,float overtime,int hours){
    setPayRate(rate);
    setovertime(overtime);
    sethours(hours);
    }
    
    bool setPayRate(float);
    bool setovertime(float);
    bool sethours(int);
    
    };

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    Post the full error. It tells you which symbol is unresolved. It generally means that you didn't implement one of the functions you declared in the class. It could also mean that your source file wasn't compiled and linked in the project/makefile. Sometimes the error occurs when you forget to prefix the function definition with the class name.

    BTW, publicly deriving Payroll from Employee sounds like poor design. Obviously you should do the assignment if that is what is asked, but in general you want to only publicly derive one class from another if it works like the base class. In this case, a Payroll is implemented in terms of an Employee, so it would normally use an Employee member variable rather than deriving from it.

  3. #3
    Registered User
    Join Date
    Jul 2007
    Posts
    13
    Thanks for the reply!

    Here is the full error:

    error LNK2019: unresolved external symbol "public: __thiscall Employee::Employee(void)" (??0Employee@@QAE@XZ) referenced in function "public: __thiscall Payroll::Payroll(float,float,int)" (??0Payroll@@QAE@MMH@Z) project5.obj

    I have all three of the functions that I declared implemented in their own file. The Employee class that works is implemented in the same way and works so I'm assuming that that isn't the issue.

    When I call the Payroll class I'm using the following

    Code:
    Payroll stevesCheck(10,15,10);

  4. #4
    Registered User
    Join Date
    Feb 2006
    Posts
    312
    Quote Originally Posted by Gipionocheiyort View Post
    Code:
    class Payrollublic Employee{
    This looks like a typing error -

    did you mean
    Code:
    class Payroll : public Employee {

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    The problem is in your Employee class. Notice how you have a constructor that takes no arguments in the Employee class declaration:
    Code:
    Employee();
    Did you implement this function? The compiler is complaining that it cannot find the implementation for it.

    The reason it is being used is because a derived class constructor always calls a base class constructor. Your Payroll class constructor doesn't tell the compiler which Employee constructor to use, so by default it uses the one with no parameters.

    If it doesn't make sense to create an employee without specifying a name and other things, then you should specify the name, EmpId, hireDate, and social when creating the Payroll. Otherwise, just implement the default constructor and initialize those values to empty strings.


    >> class Payrollublic Employee{
    I'm assuming this is just because of the smiley that appears when you have : followed by p. There is a "Disable Smilies in this post" option in the advanced editor to remove the smilies and leave the code as is.

  6. #6
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,598
    My two cents:

    Now why would they have you derive a class from Employee called Payroll?

    Is payroll an employee???

    You would only derive payroll from employee if this were true.

    This is a very sad way to demonstrate inheritance and flies in the face of object oriented design.

    Derive - Is?
    Code:
    class Polygon { };
    class Triangle:public Polygon { };
    class Rectangle:public Polygon { };
    • A triangle is a polygon. TRUE
    • A triangle has a polygon. FALSE
    • A rectangle is a polygon. TRUE
    • A rectangle has a polygon. FALSE


    Don't derive - Has?
    Code:
    class Wheel { };
    class Engine  { };
    class Car 
    {
      Wheel *pMyWheels; 
      Engine *pMyEngine;
    }
    • A car has wheels. TRUE
    • A car is a wheel. FALSE
    • A car has an engine. TRUE
    • A car is an engine. FALSE


    Applied to your instance:

    • An employee is a payroll. FALSE
    • An employee has a payroll. FALSE (a business has a payroll)
    • A payroll is an employee. FALSE (in the truest sense - A!=B)
    • A payroll has an employee. TRUE (could have 1 or more employees)


    So your structure based on these questions is:
    Code:
    class Employee { };
    
    class Payroll 
    { 
      Employee *pEmployees;
    };
    Last edited by VirtualAce; 08-01-2007 at 12:46 AM.

  7. #7
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    907
    So then, after having understood Bubba:
    Code:
    class Employee { };       
    class Manager : public Employee { };     //a Manager IS an employee
    
    
    class Payroll     // a payroll CONTAINS employees (some of which may be managers)
    { 
      Employee *pEmployees;
    };
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  8. #8
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,598
    IS or CONTAINS?
    IS or HAS?

    Same thing. Hehe.

  9. #9
    Registered User
    Join Date
    Jul 2007
    Location
    sargodha, pakistam
    Posts
    1
    If you had a constructor with no orguments in the class "Employee" then you should add a constructor in class "payroll". constructor with orguments work but how no orgument constructor will work in class "Employee"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. derived class can not access base class protected member?
    By George2 in forum C++ Programming
    Replies: 2
    Last Post: 10-21-2007, 07:32 PM
  2. deriving classes
    By l2u in forum C++ Programming
    Replies: 12
    Last Post: 01-15-2007, 05:01 PM
  3. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  4. My Window Class
    By Epo in forum Game Programming
    Replies: 2
    Last Post: 07-10-2005, 03:33 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 07:49 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21