GregLeck

Untitled

Feb 16th, 2022
673
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.02 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5. class Animal
  6. {
  7. public:
  8.     Animal();
  9.     Animal(string name, int limbs);
  10.  
  11.     string Name;
  12.     int Limbs;
  13.  
  14.     void Description();
  15. };
  16.  
  17. class Dog : public Animal
  18. {
  19. public:
  20.     Dog();
  21.     Dog(string name, int limbs);
  22. };
  23.  
  24. int main()
  25. {
  26.     // "An ANIMAL is born!" IS NOT CALLED
  27.     // "I am a Spot with 4 limbs." IS PRINTED
  28.     Dog dog("Spot", 4);
  29.  
  30.     system("pause");
  31. }
  32.  
  33. Animal::Animal()
  34. {
  35.     cout << "An ANIMAL is born!" << endl;
  36.  
  37.     Name = "Default";
  38.     Limbs = 2;
  39. }
  40.  
  41. Animal::Animal(string name, int limbs) :
  42.     Name(name), Limbs(limbs)
  43. {
  44.     // Additional stuff could be executed here.
  45. }
  46.  
  47. void Animal::Description()
  48. {
  49.     cout << endl;
  50.     cout << "I am a " << Name << " with " << Limbs << " limbs." << endl;
  51.     cout << endl;
  52. }
  53.  
  54. Dog::Dog()
  55. {
  56.     cout << "A DOG is born!" << endl;
  57. }
  58.  
  59. // In the event we don't want the empty parent,
  60. // Animal() constructor to be called:
  61. Dog::Dog(string name, int limbs) : Animal(name, limbs)
  62. {
  63.     Name = name;
  64.     Limbs = limbs;
  65.     Description();
  66. }
Advertisement
Add Comment
Please, Sign In to add comment