How you change a pointer from base class to derived class?

5 pts.
Tags:
C++
C++ classes
cast
Class
Classes
Derived class
Pointer
Hello, I have the following classes
class Rock {
public:
  Rock() { goldAmount = rand() % 100 + 1; price = 10; };
  int revealContent() { return goldAmount; };
  virtual void checkPrice() { cout << price << endl; };
protected:
  int price;
private:
  int goldAmount;
};

class ValuableRock : public Rock {
public:
  ValuableRock() { price = 100; }; // Try to set the new price in constructor
};

class PreciousRock : public Rock {
public:
  PreciousRock() { }; // Try changeing new price in overloaded function instead
  virtual void checkPrice() { cout << price * 100 << endl; };
};

void main() {
  srand((unsigned int)time(NULL));
  Rock **bagOfRocks = new Rock*[3];
  for (int i = 0; i < 3; i++) {
    bagOfRocks[i] = new Rock;	// 3 random rocks in a bag
  }
  // just crack open some of the rocks
  // first method
  if (bagOfRocks[1]->revealContent() <= 70) {  // not many gold inside
  //... something I dont know how to write but I tried this
    bagOfRocks[1] = (ValuableRock*)bagOfRocks[1];
  } else if (bagOfRocks[1]->revealContent() > 70) {  // many gold inside
  //... again, I dont know how to write
    bagOfRocks[1] = (PreciousRock*)bagOfRocks[1];
  }
  // second method
  if (bagOfRocks[2]->revealContent() <= 70) {
    //... something I dont know how to write but I also tried this
    delete bagOfRocks[2];
    bagOfRocks[2] = new ValuableRock;	// but the content of rock is rand()
                                        // may not necessary <= 70 this time
  } else if (bagOfRocks[2]->revealContent() > 70) {
    //... again, I dont know how to write
    delete bagOfRocks[2];
    bagOfRocks[2] = new PreciousRock;	// but the content of rock is rand()
                                        // may not necessary > 70 this time
  }
  for (int i = 0; i < 3; i++) {
    bagOfRocks[i]->checkPrice();// I expect it to output 10 100or1000 100or1000
  }
  for (int i = 0; i < 3; i++) {
    delete bagOfRocks[i];
    bagOfRocks[i] = NULL;
  }
  delete [] bagOfRocks;
  bagOfRocks = NULL;
} 
Here I have a base class Rock, which can have random number of gold amount. If after a rock object is created, I check how many gold is inside If there is less than 71 gold, I have to change it to a ValuableRock If there is more than 70 gold, I have to change it to a PreciousRock Rocks uncracked is still ordinary Rock but once cracked, it has to be either ValuableRock or PreciousRock and by calling checkPrice(), it should print out the corresponding price I tried to type cast the pointer type (method 1) but it does not call the corresponding function then I tried to delete the object (method 2) and create an appropriate object using the same base class pointer, this way the correct checkprice is called, but since it creates a new object, the gold amount is another random one Can anyone provide a good solution for me so that 1. I can access all kind of rock classes using an array of pointers 2. gold must rand() in the base class, not in the main() then assign appropriate class of xxxRocks construction because not all Rocks are cracked at time of creation 3. class hierarchy Rock, ValuableRock, PreciousRock must be maintained. (I know there is solution that can be done within a single class) Thank you everyone!

Answer Wiki

Thanks. We'll let you know when a new response is added.

Is there a requirement to have the 3 different classes ?

I see that all three classes have the same attributes, and the only difference is the constructor in one case and a virtual method in the other. I would use only one class, and will add a couple of attributes such as RockType to define if it is a valuable rock or a precious one, and a PriceFactor that would be 1 for a valuable rock and 100 for a precious one, so the checkPrice method would just need to return ‘price * PriceFactor’.

This way you would just need to change the RockType attirube when you want to convert a precious rock into a valuable one or vice versa, and wuold not need to cast or delete objects.

On the other hand, if you must have those three classes, you could write a method to set a fixed gold amount, so you could create a new PreciousRock or ValuableRock object, then set the gold amount with the value that the previous object had, then delete the previous object.

Discuss This Question:  

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

To follow this tag...

There was an error processing your information. Please try again later.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Thanks! We'll email you when relevant content is added and updated.

Following