7 Replies - 423 Views - Last Post: 24 February 2013 - 09:58 AM Rate Topic: -----

#1 mocards120  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 20-February 13

Classes & Constructors

Posted 20 February 2013 - 07:15 PM

I'm trying to write a program. This program is two files. One is a class with constructors. The other file creates three objects to hold employee data. Here is my code so far.

#ifndef EmployeeClass
#define EmployeeClass
#include <string>
using namespace std;

class Employee{

private:
	string empName, department, position;
	int idNumber;

public:
	void setName(string name){ empName = name; }
	void setIdNumber(int id) { idNumber = id; }
	void setDepartment(string dept) { department = dept; }   
	void setPosition(string pos) { position = pos; }

	Employee(string name, int id, string dept, string pos)
	{
	 	empName = "";
		idNumber = 0;
		department = "";
		position = "";
	}

	string Employee::getName()
	{return empName;}

	int Employee::getId()
	{return idNumber;}

	string Employee::getDept()
	{return department;}

	string Employee::getPos()
	{return position;}
};
#endif 



#include "EmployeeClass"
#include <iostream>
#include <string>

using namespace std;

const int NUM_WORKERS = 3;

int main()
{
	Employee emp[NUM_WORKERS] = {
		Employee("Susan Meyers", 47899, "Accounting", "Vice President"),
		Employee("Mark Jones", 39119, "IT", "Programmer"),
		Employee("Joy Rogers", 81774, "Manufacturing", "Engineer")
	};

	for(int i = 0; i < NUM_WORKERS; i++)
	{
		cout << emp[i].getName() << " ";
		cout << emp[i].getId() << " ";
		cout << emp[i].getDept() << " ";
		cout << emp[i].getPos() << endl;

	}

	cout << endl;
	system("pause");
	return 0;
}



When I run the program, it runs without errors, but it only displays three 0's. I'm guessing the program is reading the "0" for the idNumber three times and printing it, but it doesn't read the info I'm trying to pass into it.

Could anyone point me in the direction to get this program to run correctly? Thanks!

Is This A Good Question/Topic? 0
  • +

Replies To: Classes & Constructors

#2 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 3034
  • View blog
  • Posts: 10,584
  • Joined: 08-August 08

Re: Classes & Constructors

Posted 20 February 2013 - 08:14 PM

Look at your constructor:
	Employee(string name, int id, string dept, string pos)
	{
	 	empName = "";
		idNumber = 0;
		department = "";
		position = "";
	}


Does it use name, id, dept, or pos? If so, how?
Was This Post Helpful? 3
  • +
  • -

#3 mocards120  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 20-February 13

Re: Classes & Constructors

Posted 23 February 2013 - 05:56 PM

Yes, empName = name, idNumber = id, department = dept, and position = pos. I don't have a very helpful teacher and this is one rough chapter. It's hard to understand everything. I can't find any examples like this in the book.

Also, I'm using a different PC now (same version of Visual C++ though) and now my program doesn't ask for input, it just spits out three 0's. I'm very confused on this problem. Anyone have any tips of either problem?
Was This Post Helpful? 0
  • +
  • -

#4 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 3034
  • View blog
  • Posts: 10,584
  • Joined: 08-August 08

Re: Classes & Constructors

Posted 23 February 2013 - 06:33 PM

You may want them to be set that way, but your code is setting them to an empty string, zero, an empty string, and an empty string.
Was This Post Helpful? 0
  • +
  • -

#5 mocards120  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 20-February 13

Re: Classes & Constructors

Posted 24 February 2013 - 09:15 AM

Reading through my book, it looks like I should be using something like this:
void Employee::empName(string name)
	{
	empName = name;
	}



But that's not working for me. I'm putting it under the default constructor, but I'm getting an error saying "redefinition; previous definition was 'data member.'"

I've also tried:

Employee(string name, int id, string dept, string pos)
	{
	 	empName = name;
		idNumber = id;
		department = dept;
		position = pos;
	}



But I still get the same three 0's as output. This program is just not clicking for me.
Was This Post Helpful? 0
  • +
  • -

#6 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 3034
  • View blog
  • Posts: 10,584
  • Joined: 08-August 08

Re: Classes & Constructors

Posted 24 February 2013 - 09:42 AM

And your current code is???
Was This Post Helpful? 0
  • +
  • -

#7 mocards120  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 20-February 13

Re: Classes & Constructors

Posted 24 February 2013 - 09:50 AM

Ok, I redid my class, following the book as close as possible, and it doesn't run at all. Here's my code:

#ifndef EmployeeClass
#define EmployeeClass
#include <string>
using namespace std;

class Employee
{
	private:
		string empName, department, position;
		int idNumber;

	public:
	Employee()
	{//Initialize empName, department, position, and idNumber
	empName = " ";
	department = " ";
	position = " ";
	idNumber = 0.0;
	}

	Employee(string name, string dept, string pos, int id)
	{//Assign values to empName, department, position, and idNumber
	empName = name;
	department = dept;
	position = pos;
	idNumber = id;
	}

	//Mutator functions
	void setEmpName(string e)
	{empName = e;}

	void setDepartment(string d)
	{department = d}

	void setPosition(string p)
	{position = p}

	void setIdNumber(double i)
	{idNumber = i}

	//Accessor functions
	string getName() const
	{return name;}

	string getDept() const
	{return dept;}

	string getPos() const
	{return pos;}

	int getId() const
	{return id;}
};
#endif 



#include "EmployeeClass"
#include <iostream>
#include <string>

using namespace std;

const int NUM_WORKERS = 3;

int main()
{
	Employee emp[NUM_WORKERS] = 
	{
		Employee("Susan Meyers", 47899, "Accounting", "Vice President"),
		Employee("Mark Jones", 39119, "IT", "Programmer"),
		Employee("Joy Rogers", 81774, "Manufacturing", "Engineer")
	};

	for(int i = 0; i < NUM_WORKERS; i++)
	{
		cout << emp[i].getName() << " ";
		cout << emp[i].getId() << " ";
		cout << emp[i].getDept() << " ";
		cout << emp[i].getPos() << endl;

	}

	system("pause");
	return 0;
}


Was This Post Helpful? 0
  • +
  • -

#8 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 3034
  • View blog
  • Posts: 10,584
  • Joined: 08-August 08

Re: Classes & Constructors

Posted 24 February 2013 - 09:58 AM

I see missing semicolons in your class. I also see you attempting to use class variables that don't exist, like name and dept.

Is there a file called EmployeeClass? It should probably be: EmployeeClass.h

BTW, You should write a little code, then see if it compiles. That way you only have a small section to debug if it doesn't. If you write everything and then try to compile it can be overwhelming.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1