C++/2D Array Issue
Expert: Zlatko - 11/28/2010
QuestionI am not sure with the 2D array. There are a few errors during compilation. Compiler complains the invalid use of data members. Pls let me know why is it wrong? Tks.
#include <iostream>
using namespace std;
//Class Staff
class Staff
{
private:
int staffNumber, salesMade;
string first, last;
double bonus, salary;
char grade;
void computeBonus();
public:
double table[3][3]; //2D Array
static void displayTable();
void setFields(int, string, string, double, char, int);
void display();
};
double Staff::table[3][3] = {0.030, 0.020, 0.005, 0.050, 0.035, 0.015, 0.075, 0.055, 0.040};
void Staff::displayTable()
{
cout << "-----------------------------------------------------------------" << endl;
cout << "Sales\tClass C\tClass B\tClass a" << endl;
cout << "0 - 20 ";
for (int x = 0; x<3; ++x)
cout << table[0][x] << " ";
cout << endl;
cout << "21 - 50 ";
for (int x=0; x < 3; ++x)
cout << table[1][x] << " ";
cout << endl;
cout << "51+ ";
for (int x=0; x < 3; ++x)
cout << table[2][x] << " ";
cout << endl;
cout << "-----------------------------------------------------------------" << endl;
}
void Staff::setFields(int idNum, string one, string two, double sal, char staffGrade, int sales)
{
staffNumber = idNum;
first = one;
two = last;
salary = sal;
grade = staffGrade;
salesMade = sales;
computeBonus();
}
void Staff::computeBonus()
{
int row = (salesMade < 21 ? 0 : (salesMade < 51 ? 2 : 1));
bonus = table[row][grade-65] * salary / salesMade;
}
void Staff::display()
{
cout << "Name: " << first << " " << last << endl;
cout << "Grade: " << grade << " Staff Number #" << staffNumber << endl;
cout << "Salary: " << salary << " Sales Made #" << salesMade << endl;
cout << "Bonus: $" << bonus << endl;
}
//Main function
int main()
{
Staff staff1, staff2;
Staff::displayTable();
staff1.setFields(1001, "Thomas", "the Tank Engine", 10000, 'A', 26);
staff2.setFields(1002, "Bo", "the builder", 5000, 'c', 60);
staff1.display();
staff2.display();
}
AnswerHi Steve.
You can initialize the table array as you are doing if you make it a static variable. I assume that is what you want since displayTable is static, and you have not written any methods to change the table.
The code is below.
Best regards
Zlatko
#include <iostream>
#include <string>
using namespace std;
//Class Staff
class Staff
{
private:
int staffNumber, salesMade;
string first, last;
double bonus, salary;
char grade;
void computeBonus();
public:
Staff();
static double table[3][3]; //2D Array
static void displayTable();
void setFields(int, string, string, double, char, int);
void display();
};
/* You can do this if you make table a static. I assume that is what you
want because displayTable is static
This will not work if table is not static
*/
double Staff::table[3][3] = {0.030, 0.020, 0.005, 0.050, 0.035, 0.015, 0.075, 0.055, 0.040};
/* I recommend you have a constructor, as well as the setFields. The constructor
sets all the fields to known values instead of random values.
*/
Staff::Staff()
{
staffNumber = 0;
salesMade = 0;
bonus = 0;
salary = 0;
grade = 0;
}
/* It would be good to have another constructor to take all the same parameters that
setFields does, so that construction and, initialization can be done in one step.
The constructor should call the setFields method so that there is no redundant code
*/
void Staff::displayTable()
{
cout << "-----------------------------------------------------------------" << endl;
cout << "Sales\tClass C\tClass B\tClass a" << endl;
cout << "0 - 20 ";
for (int x = 0; x<3; ++x)
cout << table[0][x] << " ";
cout << endl;
cout << "21 - 50 ";
for (int x=0; x < 3; ++x)
cout << table[1][x] << " ";
cout << endl;
cout << "51+ ";
for (int x=0; x < 3; ++x)
cout << table[2][x] << " ";
cout << endl;
cout << "-----------------------------------------------------------------" << endl;
}
void Staff::setFields(int idNum, string one, string two, double sal, char staffGrade, int sales)
{
staffNumber = idNum;
first = one;
two = last;
salary = sal;
grade = staffGrade;
salesMade = sales;
computeBonus();
}
void Staff::computeBonus()
{
int row = (salesMade < 21 ? 0 : (salesMade < 51 ? 2 : 1));
bonus = table[row][grade-65] * salary / salesMade;
}
void Staff::display()
{
cout << "Name: " << first << " " << last << endl;
cout << "Grade: " << grade << " Staff Number #" << staffNumber << endl;
cout << "Salary: " << salary << " Sales Made #" << salesMade << endl;
cout << "Bonus: $" << bonus << endl;
}
//Main function
int main()
{
Staff staff1, staff2;
Staff::displayTable();
staff1.setFields(1001, "Thomas", "the Tank Engine", 10000, 'A', 26);
staff2.setFields(1002, "Bo", "the builder", 5000, 'c', 60);
staff1.display();
staff2.display();
}