Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- using namespace std;
- class Animal
- {
- public:
- Animal();
- Animal(string name, int limbs);
- string Name;
- int Limbs;
- void Description();
- };
- class Dog : public Animal
- {
- public:
- Dog();
- Dog(string name, int limbs);
- };
- int main()
- {
- // Will print: "An ANIMAL is born!"
- // and "A DOG is born!"
- // Dog dog;
- // Will still print: "An ANIMAL is born!"
- // and "I am a Spot with 4 limbs."
- // (NB: Empty Animal() constructor always called)
- Dog dog("Spot", 4);
- system("pause");
- }
- Animal::Animal()
- {
- cout << "An ANIMAL is born!" << endl;
- Name = "Default";
- Limbs = 2;
- }
- Animal::Animal(string name, int limbs) :
- Name(name), Limbs(limbs)
- {
- // Additional stuff could be executed here.
- }
- void Animal::Description()
- {
- cout << endl;
- cout << "I am a " << Name << " with " << Limbs << " limbs." << endl;
- cout << endl;
- }
- Dog::Dog()
- {
- cout << "A DOG is born!" << endl;
- }
- Dog::Dog(string name, int limbs)
- {
- Name = name;
- Limbs = limbs;
- Description();
- }
Advertisement
Add Comment
Please, Sign In to add comment