I'm having trouble with a homework assignment. This inheritance business is driving me crazy. In short, I'm trying to access data and functions from a parent class, through a subclass. The data I'm trying to arrange is best kept together, but I keep getting confused as to how to accomplish this. I made a couple of notes in red below to try clarify my problem a bit.
Thanks,
wheels
Extract from my code:
Code:
class People
{
public:
void display(void);
void read_data(void);
protected:
StrType name;
float pay_rate;
float gross_pay;
float inc_tax;
float ss_tax;
float net_pay;
int hours;
};
class Police : public People
{
People cops; This is where I try to link the struct to class "People"
Police *left;
Police *right;
};
Police* infile(Police*);
Police* add(Police*, Police*);
void display(Police*);
void print_menu(void);
void main()
{
//infile will read in data from a file specified by the user after checking
//to ensure the file is valid. infile will also perform the computations
//necessary to populate the node then call the "add" function to insert the
//newly populated node to the appropriate place in the tree.
Police* infile(Police* root)
{
StrType filename;
Police* buffer;
cout << "Enter a filename: ";
cin >> filename;
cin.ignore(80,'\n');
cout << endl << endl;
fin.open(filename.c_str());
//If file doesn't open, let user know
if(fin.fail())
{
cout << "Couldn't open file " << filename << endl;
cout << "Press any key to continue.";
cin.ignore(80,'\n');
} //end if
else
//Create new node to attach to tree
{
buffer = new Police;
while(fin)
{
void Police.read_data(); I get an error here saying "Improper use of typedef Police"
buffer->left = NULL; I get an error here and other places saying "'Police::left' is unaccessible (or something similar"
buffer->right = NULL;
//Call "add" function to attach node to correct spot in the tree
root = add(root,buffer);
//Create new node and read in the next set of data
buffer = new Police;
}//end while
//Delete instance of buffer before "infile" returns
delete buffer;
} //end else
return root;
} //end infile
//"add" is a recursive function that will search through the tree for the
//appropiate placement of new nodes being added to the tree.
Police* add(Police* root, Police* buffer)
{
if(root)
{
if(buffer->name <= root->name)
root->left = add(root->left,buffer);
else
root->right = add(root->right,buffer);
}
else
root = buffer;
return root;
}