Skip to main content
gamedev.net
🔒 Locked

C++ class variable problem

Started by RevKilljoy Jul 19, 2003 at 2:58 PM 2 replies 350+ views
Original Post
RevKilljoy
RevKilljoy
I am currently taking a programming class from my university and am running into some strange problems with a program. We have to create an array of pointers to objects, manipulate the data within the objects, perform a calculation, and output everything to the screen. Here's the code:
/*
	prog2.cpp
*/

#include <iostream>
#include <string>
//#include <fstream>

using namespace std;

void processarray();
double calculatesalary();

class employee
{
public:
	employee() {}
	
	
	//Method to set the name variable.

	
	void setname(string n)
	{
		name = n;
		return;
	}

	//Method to retrieve the name variable

	//Returns string


	string getname() const
	{
		return name;
	}

	//Method to set the years variable.


	void setyears(double y)
	{
		years = y;
		return;
	}

	//Method to retrieve the years variable.

	//Returns double


	double getyears() const
	{
		return years;
	}

	//Method to set the pay variable


	void setpay(double p)
	{
		payrate = p;
		return;
	}

	//Method to retrieve the pay variable

	//Returns double


	double getpay() const
	{
		return payrate;
	}

	//Method to set the salary variable


	void setsalary(double s)
	{
		salary = s;
		return;
	}

	//Method to retrive the salary variable

	//Returns double


	double getsalary() const
	{
		return salary;
	}

private:
	
	//Variables for employee object.


	string name;		
	double years,
		   payrate,
		   salary;
};

/*
	Instantiation of an array of pointers to
	employee objects
*/

employee * empArray = new employee[1];


void main()
{
	/*
		Temp variables to be passed into
		employee object methods
	*/

	string n;
	double y,
		   p;

	int i;
	for(i=0; i<1; i++)
	{

		//string n; // Temp variable for name input

		//double y, // Temp variable for year input

		//	   p; // Temp variable for pay input


		cout << "Enter the employee name => ";
		cin >> n;
		empArray[i].setname(n);

		cout << "Enter the number of years which the employee " << endl
			 << "has worked for the company => ";
		cin >> y;
		empArray[i].setyears(y);

		cout << "Enter the employee payrate => ";
		cin >> p;
		empArray[i].setpay(p);
	}

	processarray();

}

double calculatesalary(employee e)
{
	e.setsalary((e.getyears() * e.getpay()));

	return e.getsalary();
}

void processarray()
{
	int i;
	for(i=0; i<1; i++)
	{
		calculatesalary(empArray[i]);
	}

	for(i=0; i<1; i++)
	{
		cout << "\nName: " << empArray[i].getname() << endl;
		cout << "Years at Company: " << empArray[i].getyears() << endl;
		cout << "Payrate: " << empArray[i].getpay() << endl;
		cout << "Salary: " << empArray[i].getsalary() << endl;

		cout << "\n" << empArray[i].getpay() << " " << empArray[i].getyears() << " "
			 << empArray[i].getsalary() << endl;
	}
}
I know the salary calculation doesn't make sense but it was what the professor specifically asked for... I double checked! Anyhow... say you enter bill for the name, 2 for the years and 2 for the payrate. The calculation makes salary 4 (which I can see from a debugger) but when it tries to output the data at the end, it prints a large negative value for salary... even though the other variables are ok. Any help would be greatly appreciated! Thanks in advance... -------------------------------------- I'm not a freak, I'm just really shiny. [edited by - RevKilljoy on July 19, 2003 3:59:42 PM] [edited by - RevKilljoy on July 19, 2003 4:00:35 PM]
--------------------------------------I'm not a freak, I'm just really shiny.
nullstate
nullstate
Hey,

When you are passing the object to the calculatesalary(employee e) function, you are passing the object by value. You need to pass the object by reference like so:
double calculatesalary(employee &e);

When you pass by value, the function recieves a COPY of the object or data type, and so any thing you do to the copy will not affect the actual object.

However, when you pass by reference, you are working directly with the object and not just a copy.

Let me know if this works,
nullstate
RevKilljoy
RevKilljoy
ARGH! That did it! I can''t believe I missed that!

Thanks a bunch, nullstate... it seems that it is always the simple things which are easy to overlook.

:D:D

--------------------------------------
I''m not a freak, I''m just really shiny.
--------------------------------------I'm not a freak, I'm just really shiny.
nullstate
nullstate
You''re welcome RevKilljoy!

Good luck,
nullstate

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.