[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

classes

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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
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 how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses

649 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