fork(1) download
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. class enemy{
  6.  
  7. public:
  8. void setAttackPower(int a){
  9.  
  10. attackPower = a;
  11. }
  12. protected:
  13. int attackPower;
  14.  
  15. };
  16.  
  17.  
  18. class ninja : public enemy{
  19.  
  20. public:
  21.  
  22. void attack(){
  23.  
  24. cout << "ninja attack" << attackPower;
  25. }
  26.  
  27. };
  28.  
  29. class monster: public enemy{
  30.  
  31. public:
  32. void attack(){
  33.  
  34. cout << "I am a monster" << attackPower;
  35.  
  36. }
  37. };
  38.  
  39. int main()
  40. {
  41. ninja n;
  42. enemy *enemy1 = &n;
  43. enemy1->setAttackPower(20); // working fine up to here,this is where I
  44. // thought it would set the n objects attackPower to 20 but does not
  45. n.attack();
  46.  
  47. }
Success #stdin #stdout 0s 3412KB
stdin
Standard input is empty
stdout
ninja attack20