Solved

# classes

Posted on 1998-10-15
200 Views
Lets say I have two classes.
the first class flowers has some functions for the flowers.
To tell if they are dead or alive.
the second class has flowers** ptr in private.
the second class needs to get a flower tell if its

0
Question by:strmtrpr

Accepted Solution

MostlyWater earned 100 total points
ID: 1175289
You have not stated what to do with this **Flower...
So, I guess you will use an "array of pointers".
is this what you want ?
class TFlower
{
public :
TFlower(void) //need a std c´tor for a vector
{ Alive = true; AlreadyAcessed=false; }

bool isAlive(void) const
bool isDead(void) const { return ! isAlive(); }
//you need to define what "access" means :
//  ->killing a flower is an access
//might add this (for the second meaning)
void kill(void)
{ Alive=false;
//You would not really need "AlreadyAccessed" then
//because "Alive" would show if someone
//grabbed at the flower.
}
//imagine this(probably what you wish to do)
bool change(whatever parameters)//non-const
{
//do your changes here, return true if params are chosen
//within valid ranges
}

private :
bool Alive;
};

class TFlowerLst
{
public :
TFlowerPtrLst(//well, that depends what you will do with it.
const int Size);

killRandom(const int HowMany=1);
int countAlive(void)const;//returns no of alive flowers
private :
TFlower **FlowerPtrs;
int Size;
}
TFlowerLst::TFlowerLst(const int Size)
{
FlowerPtrs = new (TFlower *)[Size];
for(int i=0;i<Size;++i)
{
FlowerPtrs[i] = new TFlower();//or whatever c´tor you will define
}
}
TFlowerLst::countAlive(void) const
{
int RetVal=0;
for(int i=0;i<Size;++i)
{
if( FlowerPtrs[i] )
{ if( FlowerPtrs[i]->isAlive() )
{ ++RetVal; }
}
}
return RetVal;
}
bool TFlowerLst::killRandom(const int HowMany/*=1*/)
{
if( countAlive() < HowMany ) return false;//will hang otherwise
//need to include stdlib.h for random()
bool KilledOne=false;
int  Index;
for(int i=0;i<HowMany;++i)
{
while( ! KilledOne )
{ Index = random(Size);
if( FlowerPtrs[i]->isAlive() )
{ FlowerPtrs[i]->kill();
KilledOne=true;
}
}
}
return true;
}

Hope that helps
0

Author Comment

ID: 1175290
Thanks
0

## Featured Post

### Suggested Solutions

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.