Hi
I am working on a little program while doing my hnd in computer software development and we are currently using c++ for oop experience. My idea is to model a person class and have a farmer class. The farmer objects need to interact with an object I am calling a field object so the farmer can plant a crop in the field. I am just at the early stages of the work.
these are my early classes:
#include <iostream>
usingnamespace std;
class Field
{
public:
Field();
Field(int theCrop);
~Field();
int getCrop();
void setCrop(int);
protected:
private:
/* crops are int values. 1 = wheat, 2 = cabbage, 3 = barley
the idea is that eventually to have crop rotation
*/
int crop;
};
Field::Field()
{
crop = 1;// crop is wheat if not defined in overloaded constructor below
}
Field::Field(int c)
{
crop = c;
}
int Field::getCrop()
{
return crop;
}
void Field::setCrop(int theCrop)
{
crop = theCrop;
}
Field::~Field()
{
}
class Person
{
public:
Person();
Person(char sex, int age);
~Person();
char getSex();
void plant(int c);
int getAge();
protected:
private:
int age;
char sex;
};
Person::Person()
{
sex = 'M';
age = 19;
}
Person::~Person()
{
}
Person::Person(char s, int a)
{
sex = s;
age = a;
}
int Person::getAge()
{
return age;
}
char Person::getSex()
{
return sex;
}
int main()
{
Person Mark;
cout << Mark.getAge() << endl;
cout << Mark.getSex() << endl;
Field F1;
cout << F1.getCrop() << endl;
return 0;
}
basically, should it be possible for an object of the class person, (will end up as farmer), to use the setCrop() method?
thanks in advance for all and any pointers, no pun intended!
Mark
hi Disch
so, if I us Field *fieldPointer = new Field(1);
to create a pointer to a new field what signature do I need in the plant() method of the person object? Pointers are still a mystery to me, despite the best attempts of Mike, my lecturer!
Mark
Your Person will need to know which Field he is working in order to plant anything, right?
What a pointer does, it is let's you refer to (or "point" to) a Field that exists somewhere else, so that your Person can find it.
Exactly how you do this depends on what you want. I'm a little unclear as to how you want these classes to behave so I'm not sure how to really advise you further.
What do you want the Person::Plant function to do? Conceptually, I mean.
I have been thinking on your question, i need a crop of 0 so when the plant method is used it will change to the new crop value. So, the person uses the plant method to change from crop type 0 to crop type 1 or whatever.
sort of like this:
field has crop type 0
person comes along and uses plant method
field now has crop type 1
then after a delay, the person object can use the, as yet unwritten, harvest method thus returning the crop value to 0.
if there are more than 1 field object then the person wants to be able to use plant() on these others and perhaps plant a different crop in each.
Does this make sense?