Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- // Base class
- class Animal {
- public:
- void setWeight(int w) {
- this->weight = w;
- }
- void die() {
- this->energy = 0;
- }
- int getWeight() {
- return this->weight;
- }
- virtual void saySomething(){
- cout << "";
- }
- void run(int speed){
- this->energy -= speed/10;
- }
- void eat(int callories){
- this->energy += callories/10;
- }
- static void action(Animal& x, Animal& y){
- if(x.getWeight() > y.getWeight()){
- y.die();
- } else
- x.die();
- }
- Animal(void){
- this->energy = 50;
- }
- protected:
- int weight;
- int energy;
- };
- // Derived class
- class Mouse: public Animal {
- public:
- void saySomething() {
- if(this->energy>0)
- cout << "PIPIPI\n" ;
- else
- cout << "mouse is dead";
- }
- };
- class Cat: public Animal {
- public:
- void saySomething() {
- if(this->energy>0)
- cout << "MEOW\n" ;
- else
- cout << "cat is dead";
- }
- };
- int main(void) {
- Mouse lom;
- Cat cherry;
- cherry.saySomething();
- lom.setWeight(10);
- cherry.setWeight(40);
- Animal->action(lom, cherry);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement