Advertisement
Guest User

Untitled

a guest
Jun 27th, 2017
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.17 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. // Base class
  6. class Animal {
  7. public:
  8. void setWeight(int w) {
  9. this->weight = w;
  10. }
  11.  
  12. void die() {
  13. this->energy = 0;
  14. }
  15.  
  16.  
  17. int getWeight() {
  18. return this->weight;
  19. }
  20.  
  21. virtual void saySomething(){
  22. cout << "";
  23. }
  24.  
  25. void run(int speed){
  26. this->energy -= speed/10;
  27. }
  28.  
  29. void eat(int callories){
  30. this->energy += callories/10;
  31. }
  32.  
  33. static void action(Animal& x, Animal& y){
  34. if(x.getWeight() > y.getWeight()){
  35. y.die();
  36. } else
  37. x.die();
  38. }
  39.  
  40. Animal(void){
  41. this->energy = 50;
  42. }
  43.  
  44. protected:
  45. int weight;
  46. int energy;
  47. };
  48.  
  49. // Derived class
  50. class Mouse: public Animal {
  51. public:
  52. void saySomething() {
  53. if(this->energy>0)
  54. cout << "PIPIPI\n" ;
  55. else
  56. cout << "mouse is dead";
  57. }
  58. };
  59.  
  60. class Cat: public Animal {
  61. public:
  62. void saySomething() {
  63. if(this->energy>0)
  64. cout << "MEOW\n" ;
  65. else
  66. cout << "cat is dead";
  67. }
  68. };
  69.  
  70. int main(void) {
  71. Mouse lom;
  72. Cat cherry;
  73. cherry.saySomething();
  74. lom.setWeight(10);
  75. cherry.setWeight(40);
  76.  
  77. Animal->action(lom, cherry);
  78. return 0;
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement