-
Struct / Class Issues
Hello, I am new to working with structs and classes and seem to have run into a few issues. For the most part I think I have structs down but I am still have some trouble understand classes.
I am currently working on a program that will create an employee database for five employees.
I don't understand why I am getting compiler errors. It is saying that my variables in the bool function are undefined but they are private variables and I was under the impression that function accessing variables from within the same class could use those same variables. This only seems to be an issue with the bool function. Also if anyone could give me some direction as to how to take the program from here it would be greatly appreciated. Thanks!
So far I have this set up.
Code:
#include <iostream>
using namespace std;
const int MAX = 5;
struct nameType
{
string first;
char mid;
string last;
};
struct dateType
{
int month;
int day;
int year;
};
struct RecType
{
nameType employee;
nameType supervisor;
dateType dob;
dateType starting;
float rate;
int id_Num;
int hours;
float gross;
float all_Gross;
};
class ListType
{
public:
ListType ( );
void Insert(RecType);
bool GetNext(RecType&);
void Reset( );
private:
RecType DB[MAX];
int current;
int noemp;
};
ListType::ListType( )
{
current = 0;
noemp = 0;
}
void ListType::Insert(RecType rec)
{
DB[current] = rec;
current++;
noemp++;
}
bool GetNext::(RecType& rec)
{
if (current == noemp || noemp == 0)
return false;
else
{
rec = DB[current];
current++;
}
}
void Reset( )
{
current = 0;
}
int main()
{
}
-
Notice how
Code:
void ListType::Insert(RecType rec)
suddenly became
Code:
bool GetNext(RecType& rec)
Why do you think GetNext isn't seeing any class variables?
-
also this line:
Code:
void Reset( );
{ ...
has 2 errors
-
Look closely at the functions. You've defined them at global scope, not inside your class.
-
I made the changes you guys said except for the scope one which I don't understand. I'm still getting a lot of errors.
-
Code:
bool ListType::GetNext(RecType& rec)
{
if (current == noemp || noemp == 0)
return false;
else
{
rec = DB[current];
current++;
}
}
void ListType::Reset( )
{
current = 0;
}
-
Alright! Thanks I finally got it to compile I just got this program to insert information for all these employees to work with structs. I'm not sure of where to do with the classes though. The classes and functions of the class were given to us by the instructor does anyone know where I can go to get some help or could someone lead me in the right direction? Classes are totally new to me, I've been reading the book but all it is talk about is telling us how to increment the minutes, hours, and seconds of a clock :(. I don't know this is all just very confusing to me.
-
there is google man!
search for class, c++, tutorial, object oriented programming or what else you need.
http://www.cprogramming.com/tutorial/lesson12.html
http://www.cplusplus.com/doc/tutorial/classes.html
-
Obviously I've tried Google. I have not found any information that is more useful than the text book. I was wondering if you guys new of anything specifically or could help me understand the problem at hand. Thanks.
-
Well, long story short: an object (which is an instance of a class) is basically self-maintained. It exposes an interface for you, the programmer, to use. But the class will manage its own state, memory and all that stuff.
Kind of like a car. You have the pedestals, the steering wheels. Yet when you use them, a lot of stuff is done internally. That stuff is the class's job and the steering wheel and the pedestals are the interface.
(Btw, an instance refers to a new class, so if you define a variable somewhere of the class, then you've made a new instance.)
So what you need to think is what classes are necessary? Should the class actually represent an employee for example?
Then you need to think of what information or the interface you wish to expose. Member functions.
Then you add necessary data and internal workings to make the class work. So you can set age, get age, set profession, etc.
I'm not sure what you actually have to do.
-
With that code I have posted I need to take the information for 5 employees using the class save them in an array and them print them out
The input must be:
First name
Last name
Mid initial
DOB
Pay Rate
Hours
Start Date
Supervisor name
I figured it out with structs I just need to do it now with classes and I'm not really understanding the functions that the teacher gave us.
-
The functions I see just work like a vector, an array. I'm not sure if you really need to use the code? Are you allowed to use std::vector instead?
But as I mentioned, create an Employee class and expose an interface that allowed you to set the name of the employee, get the name, etc with all that info you need.
For example, the class might need a SetFirstName function. The function must take a string as argument. The class must also be able to store the first name, just as in the struct.
Good luck.
-
Those function were the ones he gave us I guess we don't need to use the structs... i dunno. So just make a seperate function within the class for each input?
-
I actually mean the classes... the ListType class.
It's pretty poorly made and somewhat unsafe and it's not really necessary since std::vector can do all it does and more.
But yeah, pretty much a separate function for each.