?
Solved

classes

Posted on 1998-10-15
2
Medium Priority
?
218 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
2 Comments
 

Accepted Solution

by:
MostlyWater earned 400 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

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses

809 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