Guest User

Untitled

a guest
Dec 10th, 2018
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.52 KB | None | 0 0
  1. class Animal{
  2. public:
  3. Animal() {
  4. age = 0;
  5. }
  6. virtual ~Animal(){}
  7. static Animal* factory(int type);
  8. int aliveSince(){
  9. return age;
  10. }
  11. virtual int fly(int m){} //error, needs a return value
  12. virtual int swim(int m){} //error, needs a return value
  13.  
  14. private:
  15. int age;
  16. };
  17.  
  18. class Bird: public Animal{
  19. public:
  20. Bird(){
  21. totalFlight = 0;
  22. }
  23. int fly(int m){
  24. cout<<"Bird flew "<<m<<" metresn";
  25. return totalFlight = totalFlight+m;
  26. }
  27. private:
  28. int totalFlight;
  29. };
  30.  
  31. class Fish: public Animal{
  32. public:
  33. Fish(){
  34. totalSwim = 0;
  35. }
  36. int swim(int m){
  37. cout<<"Fish swam "<<m<<" metres undergroundn";
  38. return totalSwim = totalSwim + m;
  39. }
  40. private:
  41. int totalSwim;
  42. };
  43.  
  44. Animal* Animal::factory(int type){
  45. if (type) return new Bird();
  46. else return new Fish();
  47. }
  48.  
  49. Animal *a = Animal::factory(1); //Bird
  50. Animal *b = Animal::factory(0); //Fish
  51. a->aliveSince();
  52. b->aliveSince();
  53. a->fly(5);
  54. b->swim(5);
  55.  
  56. virtual int fly(int m){ return 0; }
  57. virtual int swim(int m){ return 0; }
  58.  
  59. class Animal{
  60. public:
  61. Animal()
  62. {
  63. totalMoved = 0;
  64. }
  65. virtual ~Animal(){}
  66. virtual int move( int m )
  67. {
  68. totalMoved += m;
  69. cout<<"Total distance " << totalMoved << std::endl;
  70. return totalMoved ;
  71. }
  72.  
  73. protected:
  74. int totalMoved;
  75. };
  76.  
  77. class Bird: public Animal{
  78. public:
  79. int move(int m)
  80. {
  81. cout<<"Bird flew "<<m<<" metresn";
  82. return Animal::move( m );
  83. }
  84. };
  85.  
  86. class Fish: public Animal{
  87. public:
  88. int swim(int m){
  89. cout<<"Fish swam "<<m<<" metres undergroundn";
  90. return Animal::move( m );
  91. }
  92. };
Add Comment
Please, Sign In to add comment