-5

I am new to this. Basically I just learnt how to use class in C++. When I try to print out, the values just seem to be 0. Can anyone help me out? Its supposed to print out:

Susan Myers 47899 Accounting Vice President Mark Jones 39119 IT Position Joy Rogers 81774 Manufacturing Engineer

#include <iostream>
#include <string>
#include <iomanip>

using namespace std;

class Employee
{
    private:
        string name; 
        int idNumber; 
        string department; 
        string position; 

    public:
        Employee()
        {
            name=" "; 
            idNumber=0; 
            department=" "; 
            position=" "; 
        }

        Employee(string, int, string, string)
        {
            int id; 
            string n,d,p; 

            name=n; 
            idNumber=id; 
            department=d; 
            position=p; 
        }

        Employee(string, int)
        {
            string n; 
            int id; 

            name=n; 
            idNumber=id; 
        }
    void setName(string)
    {
        string n; 
        name=n; 
    }
    void setId(int)
    {
        int id;
        idNumber=id; 
    }
    void setDepartment(string)
    {
        string d; 
        department=d; 
    }
    void setPosition(string)
    {
        string p; 
        position=p; 
    }
    string getName() const
    {
        return name; 
    }
    int getId() const
    {
        return idNumber; 
    }
    string getDepartment() const
    {
        return department; 
    }
    string getPosition() const
    {
        return position; 
    }

}; 


int main()
{

    Employee e1; 
    Employee e2; 
    Employee e3; 

    e1.setName("Susan Meyers"); 
    e2.setName("Mark Jones"); 
    e3.setName("Joy Rogers"); 


    e1.setId(47899); 
    e2.setId(39119); 
    e3.setId(81744); 

    e1.setDepartment("Accounting"); 
    e2.setDepartment("IT"); 
    e3.setDepartment("Manufacturing"); 

    e1.setPosition("Vice President"); 
    e2.setPosition("Programmer"); 
    e3.setPosition("Engineer"); 

    cout<<"---------------------------------------"<<endl; 
    cout<<"Name"<<setw(6)<<"ID Number"<<setw(10)<<"Department"<<setw(12)<<"Position"<<endl; 

    cout<<e1.getName()<<setw(6)<<e1.getId()<<setw(10)<<e1.getDepartment()<<setw(12)<<e1.getDepartment()<<endl; 
    cout<<e2.getName()<<setw(6)<<e2.getId()<<setw(10)<<e2.getDepartment()<<setw(12)<<e2.getDepartment()<<endl; 
    cout<<e3.getName()<<setw(6)<<e3.getId()<<setw(10)<<e3.getDepartment()<<setw(12)<<e3.getDepartment()<<endl; 


    return 0; 
}
QuIcKmAtHs
  • 297
  • 4
  • 18
HR71
  • 1
  • 1
  • 2
    Why not re-read the book? Parameters usually have names – Ed Heal Dec 25 '17 at 09:16
  • I think you should throw what you have into the bin and read [a good C++ book](https://stackoverflow.com/q/388242/5958455) – iBug Dec 25 '17 at 09:25

2 Answers2

4

This is what you get when you rely on guesswork rather than properly reading an introductory textbook on C++

A constructor of the Employee class which (apart from a blank line that I've removed) you define as

Employee(string, int, string, string)
  {
        int id; 
        string n,d,p; 
        name=n; 
        idNumber=id; 
        department=d; 
        position=p; 
  }

has the following effects.

  • The four arguments passed by the caller are ignored, since they are not named.
  • Four default-initialised variables (id, n, d, and p) are defined local to the constructor body. id will be uninitialised. The others, since they are std::string, are default-initialised (to an empty string)
  • The next four statements copy those variables into class members. The result is that initialising idNumber has undefined behaviour (since id is uninitialised) and the three strings are initialised to empty strings.

To get the effect that (I assume) you intend, change this to;

Employee(std::string n, int id, std::string d, std::string p)
{
    name=n; 
    idNumber=id; 
    department=d; 
    position=p; 
}

Note that I'm calling string by its full name std::string. That allows removing the using namespace std which (among other things) is BAD practice in header files.

Even better, change this to

Employee(const std::string &n, int id, const std::string &d, const std::string &p) : 
     name(n), idNumber(id), department(d), position(p)
{
}

which passes the strings by const reference (avoids additional copies of std::strings) and uses an initialiser list instead of assigning to members in the constructor.

Similar comments apply to ALL of the member functions of Employee, except that only constructors can have initialiser lists.

Peter
  • 35,646
  • 4
  • 32
  • 74
0

Errors made

  1. Presentation

Your code is extremely cluttered, and has much irrelevant stuff.

  1. Syntax

void setPosition(string){ Here your function has no argument! What is string?

Code

#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
class Employee{
    public:
        string name; 
        int idNumber; 
        string department; 
        string position; 

    void setName(string n){ 
        name=n; 
    }
    void setId(int k){
        int id;
        idNumber=id; 
    }
    void setDepartment(string d){
        department=d; 
    }
    void setPosition(string p){
        position=p; 
    }
    string getName(){
        return name; 
    }
    int getId(){
        return idNumber; 
    }
    string getDepartment(){
        return department; 
    }
    string getPosition(){
        return position; 
    }
}; 
int main(){
    Employee e1; 
    Employee e2; 
    Employee e3; 
    e1.setName("Susan Meyers"); 
    e2.setName("Mark Jones"); 
    e3.setName("Joy Rogers"); 
    e1.setId(47899); 
    e2.setId(39119); 
    e3.setId(81744); 
    e1.setDepartment("Accounting"); 
    e2.setDepartment("IT"); 
    e3.setDepartment("Manufacturing"); 
    e1.setPosition("Vice President"); 
    e2.setPosition("Programmer"); 
    e3.setPosition("Engineer"); 
    cout<<"---------------------------------------"<<endl; 
    cout<<"Name"<<" "<<"ID Number"<<" "<<"Department"<<" "<<"Position"<<endl; 
    cout<<e1.getName()<<" "<<e1.getId()<<" "<<e1.getDepartment()<<" "<<e1.getPosition()<<endl; 
    cout<<e2.getName()<<" "<<e2.getId()<<" "<<e2.getDepartment()<<" "<<e2.getPosition()<<endl; 
    cout<<e3.getName()<<" "<<e3.getId()<<" "<<e3.getDepartment()<<" "<<e3.getPosition()<<endl; 
}

Output

---------------------------------------
Name ID Number Department Position
Susan Meyers 32767 Accounting Vice President
Mark Jones 32767 IT Programmer
Joy Rogers 32767 Manufacturing Engineer

Explanation

I have shortened your code by 50%(shows how much redundant stuff you had), and here is a working code.

void setDepartment(string d){
     department=d; 
}

Here, string d is defined IN the function as an argument. Note that your code also cout<< department twice, and I have corrected that for you in my above code. Hope this helps.

QuIcKmAtHs
  • 297
  • 4
  • 18