5 Replies - 424 Views - Last Post: 04 December 2012 - 04:57 PM Rate Topic: -----

#1 shadok  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 36
  • Joined: 08-October 12

Using Functions w/ Classes && Saving Data to Class

Posted 03 December 2012 - 08:45 PM

I've having a decent amount of trouble with classes and objects at the moment. I've looked all around, and I can't really find any sample code that applies directly to what I'm doing. Here is my code:

#include <iostream>
#include <string>
using namespace std;

class employee{
private:
	string _name;
	int _idNumber;
	string _department,
		   _position;
	
public:
	employee()
	{
		_name = " ";
		_department = " ";
		_position = " ";
		_idNumber = NULL;
	}

	void setName(string name)
	{
		_name = name;
	}
	string getName(string name)
	{
		name = _name;
		return name;
	}
	void setIdNumber(int idNumber)
	{
		_idNumber = idNumber;
	}
	int getIdNumber(int idNumber)
	{
		idNumber = _idNumber;
		return idNumber;
	}
	void setDepartment(string department)
	{
		_department = department;
	}
	string getDepartment(string department)
	{
		department = _department;
		return department;
	}
	void setPosition(string position)
	{
		_position = position;
	}
	string getPosition(string position)
	{
		position = _position;
		return position;
	}
};
	
 void displayEmployee(employee);

// Driver program to demonstrate the class
int main()
{
   // Create an Employee object to test constructor #1.
   //employee susan("Susan Meyers", 47899, "Accounting", "Vice President");
   
   // Create an Employee object to test constructor #2.
   //employee mark("Mark Jones", 39119);
   //mark.setDepartment("IT");
   //mark.setPosition("Programmer");

   // Create an Employee object to test constructor #3.
   employee joy;
   joy.setName("Joy Rogers");
   joy.setIdNumber(81774);
   joy.setDepartment("Manufacturing");
   joy.setPosition("Engineer");
   
   // Display each employee's data.
   //displayEmployee(susan);
   //displayEmployee(mark);
   displayEmployee(joy);

   system("pause");
   return 0;
}//end main

void displayEmployee()
{
	cout << "Name: " << name << endl << "ID Number: " << idNumber << endl
			<< "Department: " << department << endl << "Position: " << position;
}



The first issue I'm having is figuring out how exactly to store the data I have in comments (line 65 and lines 68-70). Can anyone give me a bit of clarification, or at least point me to some sample code?

The second issue I'm having is calling the information to the function at line 88. I've tried quite a few different things, and no matter how I configure it I can't properly call in the data. Once I get to that point, I'll need to figure out how to sort the data for each 'employee'.

I apologize for all of the questions, but I've had minimal instruction and maximum frustration with this lab, and I still need to finish another after this by tomorrow night. Thanks in advance for any help!

Is This A Good Question/Topic? 0
  • +

Replies To: Using Functions w/ Classes && Saving Data to Class

#2 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: Using Functions w/ Classes && Saving Data to Class

Posted 03 December 2012 - 09:00 PM

I don't see your issue at all. Let's take the first comment, which you say you have an issue with.

//employee susan("Susan Meyers", 47899, "Accounting", "Vice President");


What exactly confuses you here? Where are you stuck precisely?

Quote

The second issue I'm having is calling the information to the function at line 88.

Quote

I've tried quite a few different things, and no matter how I configure it I can't properly call in the data.
So, is the code above what you believe you should be correct? Writing code is not a matter of doing voodoo until the compiler passes (which, by the way, doesn't mean your code is correct).

Construct the code you believe is correct. If there's an issue in compiling, then look at the compiler error specifically and see what it is saying.
Was This Post Helpful? 0
  • +
  • -

#3 shadok  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 36
  • Joined: 08-October 12

Re: Using Functions w/ Classes && Saving Data to Class

Posted 03 December 2012 - 09:07 PM

I should have mentioned that everything in main was given to me to test my class. The issue that I'm having is my class, and my function. I need different constructors so that I can accept all of the data, and I BELIEVE I need a function to output the data. My issue here is really just setting up the other 2 constructors, and then passing the data to the function displayEmployee(). I apologize for neglecting to point that out.
Was This Post Helpful? 0
  • +
  • -

#4 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: Using Functions w/ Classes && Saving Data to Class

Posted 03 December 2012 - 09:16 PM

Quote

I need different constructors so that I can accept all of the data
This is correct.

Do you understand the syntax for a constructor? You probably do, as you already wrote one constructor.

So, going back to:

employee susan("Susan Meyers", 47899, "Accounting", "Vice President");


You need to write a constructor so that this is valid. What did your attempt look like?

Quote

and then passing the data to the function displayEmployee()
That's right. Passing the data.

void displayEmployee()
What data did you pass in here?
Was This Post Helpful? 0
  • +
  • -

#5 shadok  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 36
  • Joined: 08-October 12

Re: Using Functions w/ Classes && Saving Data to Class

Posted 03 December 2012 - 09:34 PM

I only know how to write a constructor that uses similar syntax to a struct (I believe) since I have a bit of experience with those. That's why I had no issue with things like joy.setName and so on. My confusion with constructors stems from the fact that the syntax within the main function for employee susan is completely different. Since ("Susan Meyers", 47899, "Accounting", "Vice President") is just a collection of the variables, I'm unsure of how to actually assign those to the proper variables. Once I figure that out, I'm sure I can figure out employee mark, as it appears to be a combination of the others.

So far I've tried passing the class (employee), I've tried to pass the object employee susan, I've tried passing the variables individually from each of my get functions (ie getName()). When I pass the object it comes up as undefined within the function. The fact that even passing the individual variables isn't working just has me more confused. At that point I was basically trying to get anything into the function just to form a better understanding of how the syntax worked.

This post has been edited by shadok: 03 December 2012 - 09:34 PM

Was This Post Helpful? 0
  • +
  • -

#6 shadok  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 36
  • Joined: 08-October 12

Re: Using Functions w/ Classes && Saving Data to Class

Posted 04 December 2012 - 04:57 PM

And apparently I'm a moron. I finally figured it all out. I was not taking data into my constructors unless it was sent there directly in main, but figured out how to do that. My issue with the function was that rather than use my accessors to set variables to the correct values within the function, I was trying to pull the actual values themselves directly to the function. It took quite a bit of time and frustration, but I figured it out. Thanks for the input!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1