GregLeck

Untitled

Feb 16th, 2022
651
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.04 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.     // Will print: "An ANIMAL is born!"
  27.     // and "A DOG is born!"
  28.     // Dog dog;
  29.  
  30.     // Will still print: "An ANIMAL is born!"
  31.     // and "I am a Spot with 4 limbs."
  32.     // (NB: Empty Animal() constructor always called)
  33.     Dog dog("Spot", 4);
  34.  
  35.     system("pause");
  36. }
  37.  
  38. Animal::Animal()
  39. {
  40.     cout << "An ANIMAL is born!" << endl;
  41.  
  42.     Name = "Default";
  43.     Limbs = 2;
  44. }
  45.  
  46. Animal::Animal(string name, int limbs) :
  47.     Name(name), Limbs(limbs)
  48. {
  49.     // Additional stuff could be executed here.
  50. }
  51.  
  52. void Animal::Description()
  53. {
  54.     cout << endl;
  55.     cout << "I am a " << Name << " with " << Limbs << " limbs." << endl;
  56.     cout << endl;
  57. }
  58.  
  59. Dog::Dog()
  60. {
  61.     cout << "A DOG is born!" << endl;
  62. }
  63.  
  64. Dog::Dog(string name, int limbs)
  65. {
  66.     Name = name;
  67.     Limbs = limbs;
  68.     Description();
  69. }
Advertisement
Add Comment
Please, Sign In to add comment