Class Confusion

Hello! I need help that regarding classes. I have a program that enrolls and computes the salary of the employees. I've created a class named salary_computation with a bunch of members. Here's the class:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class salary_computation{
public:	
	float tax_;
	float result_;
	float salary;     // This will store the salary using employee[m].salary
	//float temp_salary;
	string status;
	string F_name;
	string L_name;
	float compute_deduction(float,float){
		return ( salary * tax_);
	}
	float deduce(float,float){
		return (salary-result_);
	}
}*employee, solve;


Now I want to use the "salary" in the function included in the class but I guess i have a problem in the operation since Im using "employee[m].salary" in the code below.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
case 2:
	temp=0;
	for(;temp<m;temp++)
	{
		cout<<temp+1<<". "<<employee[temp].L_name<<", "<<employee[temp].F_name<<endl;
	}
	cout<<"Please Choose Employee by choosing their corresponding number:";
	cin>>choice;
	choice--;
	if(employee[choice].status=="Single")
		solve.tax_=.33f;
	else if(employee[choice].status=="Married")
		solve.tax_=.23f;
	else 
		solve.tax_=.14f;

	//solve.temp_salary=employee[choice].salary;
	solve.result_=solve.compute_deduction(employee[choice].salary,solve.tax_);
	cout<<employee[choice].L_name<<", "<<employee[choice].F_name<<":	P"<<solve.deduce(employee[choice].salary,solve.result_)<<endl<<endl;
	break;


I've already solved this using "temp_salary" to temporarily store the value of the "employee[m].salary", but I want to know how do I use "employee[m].salary" in the functions under the class so i wont create any temporary variables?? Thank you! By the way, here is the whole code(note: this is the one with the wrong output).
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#include <iostream>
#include <string>
#include <stdio.h>
#include <math.h>

using namespace std;

class salary_computation{
public:	
	float tax_;
	float result_;
	float salary;
	//float temp_salary;
	string status;
	string F_name;
	string L_name;
	float compute_deduction(float,float){
		return ( salary * tax_);
	}
	float deduce(float,float){
		return (salary-result_);
	}
}*employee, solve;



void main(){

bool ans;
bool invalid;
int x;
employee=new salary_computation[1001];
int m=0;
int stat_choice;
int temp;
int choice;
char reply;


do{

cout<<"Employee Salary Computation Software"<<endl;


cout<<"1. Add Employee"<<endl<<"2. Compute Net Salary"<<endl<<"3. View Employees"<<endl;
cin>>x;
switch(x){
case 1:
	cout<<"First Name: ";
	cin.clear();
	cin.sync();
	getline(cin,employee[m].F_name);
	cout<<"Last Name: ";
	getline(cin,employee[m].L_name);
	cout<<"Basic Salary: ";
	cin>>employee[m].salary;

	do{
	cout<<"Status: 1. Single    2. Married    3. Widow/Widower"<<endl;
	cin.clear();
	cin.sync();
	cin>>stat_choice;
		if(stat_choice==1)
			{employee[m].status="Single";
			invalid=false;}		
		else if(stat_choice==2)
			{employee[m].status="Married";
			invalid=false;}
		else if(stat_choice==3)
			{employee[m].status="Widow/Widower";
			invalid=false;}
		else
			{cout<<"Input is invalid.";
			invalid=true;}
	}while(invalid);
	cout<<employee[m].status;
	m++;
	break;

case 2:
	temp=0;
	for(;temp<m;temp++)
	{
		cout<<temp+1<<". "<<employee[temp].L_name<<", "<<employee[temp].F_name<<endl;
	}
	cout<<"Please Choose Employee by choosing their corresponding number:";
	cin>>choice;
	choice--;
	if(employee[choice].status=="Single")
		solve.tax_=.33f;
	else if(employee[choice].status=="Married")
		solve.tax_=.23f;
	else 
		solve.tax_=.14f;

	//solve.temp_salary=employee[choice].salary;
	solve.result_=solve.compute_deduction(employee[choice].salary,solve.tax_);
	cout<<employee[choice].L_name<<", "<<employee[choice].F_name<<":	P"<<solve.deduce(employee[choice].salary,solve.result_);
	break;

	case 3:
	temp=0;
	for(;temp<m;temp++)
	{
		cout<<temp+1<<". "<<employee[temp].L_name<<", "<<employee[temp].F_name<<endl;
	}
	break;


}

cout<<"Would you like to repeat? y/n";
cin>>reply;
if(reply=='y')
	ans=true;
else
	ans=false;

}while(ans);

delete [] employee;
}
There's a lot of information here, probably more than necessary.

I'm deducing that you want to pass a value that isn't a class member into a class function, correct?

If so, you were almost there. If you give the float parameters in your function identifiers, you can then pass values in to be used within the function:

1
2
3
4
float compute_deduction (float x,float y)
{
   return ( x * y );
}


In the example above, x and y aren't members of the class, but rather identifiers for values passed into the class function.

Hope this helps.
Hello iHutch105, I wanted it to give every bit of details. XD

Anyway, I tried this:
1
2
float compute_deduction(float x,float y){
		return ( salary * tax_);

But it was still giving the wrong output.

The value i want to pass in the function inside the class is the one stored in
"employee[m].salary"(Is this considered a member of the class?? I'm kinda confused since I'm self studying. I know that "salary" is a member, but what is the term for "employee[m].salary"?? is it object?? ), But i can't pass it directly.

Appreciate your reply! Thanks!




You can pass it directly. The problem with the code above is that you're passing values x and y in but you're not actually doing anything with them in the body of the function (you're returning the multiplication of the member values salary and tax). You'd need to use the values x and y in the function body to obtain a different result.

To me, it seems like this code might need a little restructuring. I'm not quite sure what the 'solve' instance should be doing. To me, it makes more sense to have a list of employees, the ability to set their salary and tax and something to work out their tax.

I'll give a brief example of what I mean. However, I'll use a struct to keep it implicitly public for simplicity:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
struct Employee
{
   string m_forename;
   string m_surname;
   double m_salary;
   double m_tax;

   double CalculateDeduction()
   {
      return m_salary * m_tax;
   }
};

// In main...
const int NUM_EMPLOYEES = 5;
Employee employees[NUM_EMPLOYEES];  // A list of five employees, for example

for( int i = 0; i < NUM_EMPLOYEES; i++ )
{
   // They're all named "John Smith" ;-)
   employees[i].m_forename = "John";
   employees[i].m_surname  = "Smith";

   // Assign some different values for salary and tax
   employees[i].m_salary = ( i + 1 ) * 10000;
   employees[i].m_salary = ( i + 1 ) * 0.05;

   cout << "The tax deduction for employee " << i << " is: " << employees[i].ComputeDeduction() << endl;
}


If you wanted to store the deduction for each employee, you could add a deduction amount member variable and instead of returning the value in ComputeDeduction, you could just assign it to that variable.

What I've written is more or less the same as yours, a few syntactically issues aside. It makes more sense to treat Employee as an object, rather than have a type of object called salary computation, which is more of a function.
I got too excited to try your previous suggestion that I forgot to modify the body of the function. I tried again and it worked already! Thank you!

Moving forward, thanks for pointing out the 'solve' thingy, I thought I can't use a pointer when using a function(employees[i].ComputeDeduction()) thus all of my functions were called as "solve.compute_deduction". That's the mystery of the 'solve' thingy. hehehe!

Thank you very much! you've been a great help to me!

Topic archived. No new replies allowed.