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
Solved

classes

Posted on 1998-10-15
2
206 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 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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Excel/Word Add-in in what language? 5 128
C++ Language error 28 245
How to gracefully close the c++ 11 thread? 3 108
C++ help/ Toy problem 19 30
In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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 learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

860 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