Advertisement
Guest User

Untitled

a guest
Mar 28th, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.24 KB | None | 0 0
  1. Animal.h
  2.  
  3. #include <iostream>
  4. #include <string>
  5. using namespace std;
  6.  
  7. class Animal
  8. {
  9. private:
  10. string animal;
  11. public:
  12. Animal(){}
  13. Animal(string animal)
  14. {
  15. this->animal = animal;
  16. }
  17.  
  18. void move()
  19. {
  20. cout<<"\n\tEvery animal moves.";
  21. }
  22.  
  23. };
  24.  
  25. Dog.h
  26.  
  27. #include <iostream>
  28. #include <string>
  29. #include "Animal.h"
  30. using namespace std;
  31.  
  32. class Dog:public Animal
  33. {
  34. private:
  35. string dog
  36. ;
  37. public:
  38. Dog(string dog):Animal()
  39. {
  40. this->dog=dog;
  41. }
  42. void move()
  43. {
  44. cout<<"\n\tDog moves running.\n";
  45. }
  46. };
  47.  
  48. Main.cpp
  49.  
  50. #include <iostream>
  51. #include <string>
  52. #include "Dog.h"
  53. using namespace std;
  54.  
  55. int main()
  56. {
  57. Animal anyAnimal("Jery");
  58. Dog myDog("Tom");
  59. Animal &anyAnimalRef = anyAnimal;
  60. Dog &myDogRef = myDog;
  61.  
  62. cout<<"\nCalling function move() for objects";
  63. anyAnimal.move();
  64. myDog.move();
  65.  
  66. cout<<"\nCalling function move() with references";
  67. anyAnimalRef.move();
  68. myDogRef.move();
  69.  
  70. cout<<"\nCalling function move() with the changed reference";
  71. anyAnimalRef = myDog;
  72. anyAnimalRef.move();
  73.  
  74. cout<<"\n\n Calling function move() with base-class reference to derived-class object";
  75. Animal &anyAnimalRef2 = myDog;
  76. anyAnimalRef2.move();
  77. cout<<endl;
  78.  
  79. cout<<endl;
  80. system ("pause");
  81. return 0;
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement