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"
-------------------
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 Payroll:public 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);
};
Re: C++ Newbie - Derived Class Issue
1) Please use code tags so that your post is readable.
2) Given the unreadable nature of your current post, can you at least share the EXACT SPECIFIC questions/problems you are having.
Re: C++ Newbie - Derived Class Issue
I'm sorry, this is my first post and I didn't realize that there were code tags.
Here is the class declaration posted again. The issue that I'm having is that something seems to be wrong with my derived class declaration and I can't figure out what it is. I have other examples that I've done before with smaller programs that look to me to be the exact same syntax...but they worked there and not here. I can't figure out what is different though.
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 Payroll:public 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);
};
Re: C++ Newbie - Derived Class Issue
Looks fine to my (except that putting the opening brace at the end of a line is a Java convention, not C++. Braces should line up and bee on their own lint).
Again I ask, what EXACT SPECIFIC question/problem to You have??
Re: C++ Newbie - Derived Class Issue
The issue is that I'm getting the 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
when I try to compile the project. I'm not sure what I'm doing wrong but I've tried it about 20 different ways and nothing is working.
I'm calling the Payroll class using the following:
Code:
Payroll stevesCheck(10,15,10);
Re: C++ Newbie - Derived Class Issue
That is not a declaration problem, it is am implementation problem. In some .cpp file your need
Code:
Employee::Employee()
{ // What to do for a default employee
}
Re: C++ Newbie - Derived Class Issue
The employee class works fine with the inline constructor that was in my header file. It isn't until I introduce the Payroll class that the error pops up. Do I need another employee constuctor for the payroll class?
Re: C++ Newbie - Derived Class Issue
Quote:
Originally Posted by Gipionocheiyort
The employee class works fine with the inline constructor that was in my header file. It isn't until I introduce the Payroll class that the error pops up. Do I need another employee constuctor for the payroll class?
PRobably because you were calling the parameterized version of the Employee class. Simply put a call to "Employee test;" in your code, and you will see it fail. Even if you totally remove the payroll class.
This problem has nothing to do with the interface.
Re: C++ Newbie - Derived Class Issue
Ah I get it now. I had actually tried to fix that but I made a syntax error. When I switched
to [code]Employee(){}code]
it worked.
Thanks for your help!
Re: C++ Newbie - Derived Class Issue
Quote:
Originally Posted by Gipionocheiyort
Ah I get it now. I had actually tried to fix that but I made a syntax error. When I switched
to
it worked.
Thanks for your help!
Of course none of the fields of the employee are going to be initialized.... :rolleyes:
Re: C++ Newbie - Derived Class Issue
Quote:
Originally Posted by TheCPUWizard
Looks fine to my (except that putting the opening brace at the end of a line is a Java convention, not C++. Braces should line up and bee on their own lint).
and all this time i thought it was a personal preference thing, hmmmm :blush:
Re: C++ Newbie - Derived Class Issue
Quote:
Originally Posted by ka3ak
and all this time i thought it was a personal preference thing, hmmmm :blush:
So is the following:
Code:
#include<istrream>
class X {int a;int b;X():a(1),b(2){};int f(){return(a+b);}void g(int n){a-n;);void h(int m){b=m}};void main(){X x; x.f(4);x.g(10);cout<<x.f();}
Re: C++ Newbie - Derived Class Issue
Quote:
Originally Posted by TheCPUWizard
So is the following:
Code:
#include<istrream>
class X {int a;int b;X():a(1),b(2){};int f(){return(a+b);}void g(int n){a-n;);void h(int m){b=m}};void main(){X x; x.f(4);x.g(10);cout<<x.f();}
and this:
Code:
class X
{
int a;
int b;
X() : a(1) , b(1)
{
}
int f( )
{
return a + b;
}
...
};
Re: C++ Newbie - Derived Class Issue
Just an observation, but I'm always surprise to see code from students learning programming (specially C++) to use char/char[] rather then std::string. Why do professors still teach that in Universities?
Last question was more rhetorical one.
Re: C++ Newbie - Derived Class Issue
Quote:
Originally Posted by ka3ak
and all this time i thought it was a personal preference thing, hmmmm :blush:
It is, but I agree with the wizard that code where braces don't line up is very difficult to read to me.
Re: C++ Newbie - Derived Class Issue
Quote:
Originally Posted by STLDude
Just an observation, but I'm always surprise to see code from students learning programming (specially C++) to use char/char[] rather then std::string. Why do professors still teach that in Universities?
Last question was more rhetorical one.
I can tell you why. Usually, the academic programming courses are about C, and OOP courses teach Java. When one starts using C++, nobody teaches him about the STL and the containers, so he goes on using null-terminated char arrays and pointers to arrays.
Re: C++ Newbie - Derived Class Issue
1. private is spelt P-R-I-V-A-T-E.
2. In C++ strings are stored using std::string not arrays of char. They are also often passed around as const std::string & although they may be passed by value.
3. Methods that do not change the state of the class are declared const.
so
Code:
#include <string>
class Employee
{
private:
std::string name;
std::string ssn;
std::string empId;
std::string hireDate;
public:
Employee();
Employee
(
const std::string & social, const std::string & EmpId,
const std::string & HireDate, const std::string & Name
);
bool setSocial( const std::string & ]);
void printSocial() const;
bool setEmpId(const std::string &);
void printEmpId() const;
bool setHireDate( const std::string &);
void printHireDate() const;
bool setName( const std::string & );
void printName() const;
};
Even now all you have is a few strings grouped together. Also printName() etc is not a good idea, preferable is getName() and then the caller can print the string if they wish.