AT90 AT90 - 2 months ago 7
C++ Question

How to access member in derived classes by a pointer of type base but pointing to the derived class

I've following code:

#include <iostream>
using namespace std;

class Weapon
{
protected:
int strength;
char type;
public:
int modified;

int return_strength()
{
return strength;
}

char return_type()
{
return type;
}

void setPower (int val)
{
strength = val;
return;
}
};



class Rock: public Weapon
{

public:
Rock()
{
type='r';
}
bool battle(Weapon w)
{
//Write your solution code below this line

//default return statement below

switch ( w.return_type() ) {
case 'p':
modified = strength / 2;
w.modified = strength * 2;
break;
case 's':
modified = strength * 2;
w.modified = strength / 2;
break;
default:
cout << "Unkown option";
break;
}

if (modified > w.modified )
{
return true;
}
else if (modified < w.modified )
{
return false;
}
else
{
cout << "The strenghs are equal";
return -1;
}
}

};

class Paper: public Weapon
{

public:
Paper()
{
type='p';
}
bool battle(Weapon w)
{
//Write your solution code below this line

//default return statement below
return true;
//remove the default return statement and return your own result

}

};

class Scissors: public Weapon
{

public:
Scissors()
{
type='s';
}
bool battle(Weapon w)
{
//Write your solution code below this line

//default return statement below
return true;
//remove the default return statement and return your own result


}

};



int main()
{
char play1, play2, str_winner;
int str1, str2;
bool winner;
Paper obj_paper;
Rock obj_rock;
Scissors obj_scissors;
Weapon *p1, *p2;


cout << "1st player: Choose Rock (r), Paper (p) or Scissors (s)";
cin >> play1;
cout << "Insert a strength";
cin >> str1;
cout << "2st player: Choose Rock (r), Paper (p) or Scissors (s)";
cin >> play2;
cout << "Insert a strength";
cin >> str2;

switch( play1 ){
case 'r':
p1 = &obj_rock;
case 'p':
p1 = &obj_paper;
case 's':
p1 = &obj_scissors;
}

switch( play2 ){
case 'r':
p2 = &obj_rock;
case 'p':
p2 = &obj_paper;
case 's':
p2 = &obj_scissors;
}

p1->setPower(str1);
p2->setPower(str2);

winner = p1->battle(*p2);

return 0;
}


The code is not entirely finished but I'm facing two problems.

1) When I try to access battle from one of the derived classes, it says that there is no member of that name. It seems like p1 does only have access to the base class. Does c++ now allow me to access the functions in derived class when I let the pointer points to that derived class?

2) Is there a better way of how to determine which derived class I should be using during run time. As you see, I get two inputs from user telling what weapons he wants, and then I need to point out what objects to consider.

Thank you

Answer
  1. access battle from one of the derived classes; and that's the type of p1 Weapon doesn't have battle as a member. You may also wish to look up the "virtual" keyword. (Java, C# & other OO languages apply this keyword by default) Also worth reading about are pure virtual functions.

2.

 switch(play1 ){
        case 'r':
           p1 = &obj_rock; 

 ...

While what you're doing is fine, I think what you want to do is p1 = new Rock() {followed by delete p1 when you're done with that memory}.

This is also a more generic problem for which people use the factory pattern