Hello, I have the following classes
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
|
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!