Solved

classes

Posted on 1998-10-15
2
208 Views
Last Modified: 2010-04-02
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
dead or alive and if that flower has already been accessed.
PLEASE HELP..

0
Comment
Question by:strmtrpr
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 

Accepted Solution

by:
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
  { AlreadyAcessed=true; return Alive; }
  bool isDead(void) const { return ! isAlive(); }
  bool hasAlreadyBeenAcessed(void)const{return AlreadyAcessed;}
  //you need to define what "access" means :
  //  ->asking if dead or not == accessed
  //  ->killing a flower is an access
  //might add this (for the second meaning)
  void kill(void)
  { Alive=false;
    AlreadyAcessed=true; // ?? really ??
                                               //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
   AlreadyAcessed=true;
  }

 private :
  bool Alive;
  bool AlreadyAcessed;
};

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

by:strmtrpr
ID: 1175290
Thanks
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question