2

I created this class from what I understand, however it is giving many errors, what I'm trying to do is to create a employee class and basically learned how to create and manipulate objects with it. Any help is greatly appreciated. :)

#include <iostream>
#include <string>

using namespace std;

    class employeeClass
    {
private:
    int emplNumber;
    string empName;
    string stAddress;
    string phoneNumber;
    double hourlyWage;
    double hoursWorked;
public:
    employeeClass();//not parametized constructor
    employeeClass(int, string, string, string, double, double);
         ~employeeClass();//destructor to release memory if object assigned dynamic memory

    void setEmpnumber();
    int getEmpnumber();

    void setEmpname();
    string getEmpname();

    void setstAddress();
    string getstAddress();

    void setphoneNumber();
    string getphoneNumber();

    void setHourlywage();
    double getHourlywage();

    void sethoursWorked();
    double gethoursWorked();

     employeeClass::employeeClass()
     {
         int emplNumber;
         string empName;
         string stAddress;
         string phoneNumber;
         double hourlyWage = 0;
         double hoursWorked = 0;
     }
     employeeClass::employeeClass(int a, string b, string c, string d, double e,     double f)
     {
         emplNumber=a;
         empName=b;
         stAddress=c;
         phoneNumber=d;
         hourlyWage=e;
         hoursWorked=f;
     }
     void employeeClass::setEmpnumber(int a)
     {
         emplNumber=a;
     }
     int employeeClass::getEmpnumber()
     {
         return emplNumber;
     }
     void employeeClass::setEmpname(string b)
     {
         empName=b;
     }
     string employeeClass::getEmpname()
     {
         return emplNumber;
     }
     void employeeClass::setstAddress(string c)
     {
         stAddress=c;
     }
     string employeeClass::getstAddress()
     {
         return stAddress;
     }
     void employeeClass::setphoneNumber(string d)
     {
         phoneNumber=d;
     }
     string employeeClass::getphoneNumber()
     {
         return phoneNumber;
     }
     void employeeClass::setHourlywage(double e)
     {
         hourlyWage=e;
     }
     double employeeClass::getHourlywage()
     {
         return hourlyWage;
     }
     void employeeClass::sethoursWorked(double f)
     {
         hoursWorked=f;
     }
     double employeeClass::gethoursWorked()
     {

         return hoursWorked;
     }
};

int main ()
{
employeeClass gama, tito;
gama.setEmpname("Gamaliel tellez");
gama.setphoneNumber("8018839494");
gama.setEmpnumber('12');
gama.setHourlywage('345');
gama.sethoursWorked('23');


cout<<"Employee info: "<<endl;
cout<<"Name: "<<gama.getEmpname<<endl;
cout<<"Phone number: "<<gama.getphoneNumber<<endl;
cout<<"Emp number: "<<gama.getEmpnumber<<endl;
cout<<"Hourly wage: $"<<gama.getHourlywage<<endl;
cout<<"Hours worked: "<<gama.gethoursWorked<<endl;


system ("PAUSE");
return 0;

}

4 Answers4

2

You made methods prototypes and realization in the same class. You should separate them. Your .h file should contain:

class employeeClass
    {
private:
    int emplNumber;
    string empName;
    string stAddress;
    string phoneNumber;
    double hourlyWage;
    double hoursWorked;
public:
    employeeClass();//not parametized constructor
    employeeClass(int, string, string, string, double, double);
         ~employeeClass();//destructor to release memory if object assigned dynamic memory

    void setEmpnumber();
    int getEmpnumber();

    void setEmpname();
    string getEmpname();

    void setstAddress();
    string getstAddress();

    void setphoneNumber();
    string getphoneNumber();

    void setHourlywage();
    double getHourlywage();

    void sethoursWorked();
    double gethoursWorked();
};

And all realizations should go into separate .cpp file, in that you need to include header and implement methods, it should be like this:

void employeeClass::setEmpnumber(int a)
{
    emplNumber=a;
}

There is a way to make it work having all the stuff in header but it is better to separate, because changes in header file is a signal to recompile everything, so separation is time saving.


Also in your main() all methods must have brackets at the end, it doesn't matter if there are parameters to pass or not, so you need to change

cout<<"Name: "<<gama.getEmpname<<endl;

into

cout<<"Name: "<<gama.getEmpname()<<endl;

and some more.

BTW. it is not mistake, but advice, do not use using in global scope, latter then you will be working on bigger projects using may cause lots of mess, so better use std::string, std::cout and so on...

Also as you possibly new on C/C++ you may read about #pragma once and include guards, it is also needed for headers if you will have more headers that include each other.

Community
  • 1
  • 1
ST3
  • 8,050
  • 2
  • 62
  • 85
2

Several things:

First, anything that starts with employeeClass:: is an implementation, you should move them outside of the class definition. It is recommended to write a .h file with declarations and a .cpp file with imlementations.

Second, your declarations should also include arguments, for instance, void setEmpname(); should become void void setEmpname(string b);

You were also missing the implementation of the destructor. It would be something like employeeClass::~employeeClass(){}

Every time you call a function, even if it has no arguments, you should use parenthesis. For instance, in main, gama.getEmpname becomes gama.getEmpname().

Using system ("PAUSE"); is not recommended, but if you want to use it, you should add #include <cstdlib>

Also, while using namespace std; would not cause you any problems with small pieces of code like this, it can cause conflicts with larger projects that use lots of libraries. It is good to be aware of this.

A working version of your code with these corrections is included below:

#include <iostream>
#include <string>
#include <cstdlib>

using namespace std;

class employeeClass {
private:
    int emplNumber;
    string empName;
    string stAddress;
    string phoneNumber;
    double hourlyWage;
    double hoursWorked;
public:
    employeeClass();//not parametized constructor
    employeeClass(int, string, string, string, double, double);
    ~employeeClass();//destructor to release memory if object assigned dynamic memory

    void setEmpnumber(int a);
    int getEmpnumber();

    void setEmpname(string b);
    string getEmpname();

    void setstAddress(string c);
    string getstAddress();

    void setphoneNumber(string d);
    string getphoneNumber();

    void setHourlywage(double e);
    double getHourlywage();

    void sethoursWorked(double f);
    double gethoursWorked();


};
employeeClass::employeeClass() {
    int emplNumber;
    string empName;
    string stAddress;
    string phoneNumber;
    double hourlyWage = 0;
    double hoursWorked = 0;
}
employeeClass::employeeClass(int a, string b, string c, string d, double e,     double f) {
    emplNumber=a;
    empName=b;
    stAddress=c;
    phoneNumber=d;
    hourlyWage=e;
    hoursWorked=f;
}
employeeClass::~employeeClass() {}
void employeeClass::setEmpnumber(int a) {
    emplNumber=a;
}
int employeeClass::getEmpnumber() {
    return emplNumber;
}
void employeeClass::setEmpname(string b) {
    empName=b;
}
string employeeClass::getEmpname() {
    return empName;
}
void employeeClass::setstAddress(string c) {
    stAddress=c;
}
string employeeClass::getstAddress() {
    return stAddress;
}
void employeeClass::setphoneNumber(string d) {
    phoneNumber=d;
}
string employeeClass::getphoneNumber() {
    return phoneNumber;
}
void employeeClass::setHourlywage(double e) {
    hourlyWage=e;
}
double employeeClass::getHourlywage() {
    return hourlyWage;
}
void employeeClass::sethoursWorked(double f) {
    hoursWorked=f;
}
double employeeClass::gethoursWorked() {

    return hoursWorked;
}
int main () {
    employeeClass gama, tito;
    gama.setEmpname("Gamaliel tellez");
    gama.setphoneNumber("8018839494");
    gama.setEmpnumber('12');
    gama.setHourlywage('345');
    gama.sethoursWorked('23');


    cout<<"Employee info: "<<endl;
    cout<<"Name: "<<gama.getEmpname()<<endl;
    cout<<"Phone number: "<<gama.getphoneNumber()<<endl;
    cout<<"Emp number: "<<gama.getEmpnumber()<<endl;
    cout<<"Hourly wage: $"<<gama.getHourlywage()<<endl;
    cout<<"Hours worked: "<<gama.gethoursWorked()<<endl;


    system ("PAUSE");
    return 0;

}
memo1288
  • 748
  • 1
  • 6
  • 12
  • thanks for the help, I'm trying to rewrite it my else using the suggestions given. And man, people here motivate by thinking how good I can become. Thanks again – Gamaliel Tellez Ortiz Oct 11 '13 at 20:56
1

Let the definition of your class contain only declarations such as:

public:
    void setEmpnumber(int);

And implementations of these members, such as:

void employeeClass::setEmpnumber(int a)
{
    emplNumber=a;
}

should be placed outside of a class. Also note that you are missing () when calling these methods:

cout<<"Emp number: "<<gama.getEmpnumber()<<endl;
                                       ^^
LihO
  • 37,789
  • 9
  • 89
  • 156
1
void employeeClass::setEmpnumber(int a)
//   ^^^^^^^^^^^^^^^

You're qualifying your methods inside the class when you should be doing this outside the body of the class.

You also need to use () to call your methods:

cout << "Name: "         << gama.getEmpname()     << endl;
cout << "Phone number: " << gama.getphoneNumber() << endl;
cout << "Emp number: "   << gama.getEmpnumber()   << endl;
cout << "Hourly wage: $" << gama.getHourlywage()  << endl;
cout << "Hours worked: " << gama.gethoursWorked() << endl;
0x499602D2
  • 87,005
  • 36
  • 149
  • 233