13029 - Animal   

Description

Use main.cpp, and complete 5 classes that consists some functions:

Animal

  • Run() – should print: Running!!! (type)
  • Sleep() – should print: Sleeping~~~ (type)

 

Dog

  • Run()
  • Sleep()
  • Woof() – should print: Woof! Woof!
  • type: dog

 

Bird

  • Run()
  • Sleep()
  • Fly() – should print: Flying!!! (type)
  • Sing() – Should print: The (type) sings like "sound"
  • Feed(string food) – Should print: You just feed the type some food, at the first line. If the food is the type’s secretfood than call LayAnEgg(); otherwise print: Nothing happened..., at the second line.

 

Chicken

  • Run()
  • Sleep()
  • Fly()
  • Sing()
  • Feed(string food)
  • type: chicken
  • sound: COCK-A-DOODLE-DOO
  • secretFood: uncooked rice

 

Duck

  • Run()
  • Sleep()
  • Fly()
  • Sing()
  • Feed(string food)
  • Swim() – Should print: Swimming~~~
  • type: duck
  • sound: GUA-GUA-GUAAA
  • secretFood: bookworms

 

Examples:

Duck duck;
duck.Run();              // Running!!! (duck)
duck.Fly();              // Flying!!! (duck)
duck.Sing();             // The duck sings like "GUA-GUA-GUAAA"
duck.Swim();             // Swimming~~~
duck.Feed("worms");      // You just feed the duck some worms
                         // Nothing happened...
duck.Feed("bookworms");  // You just feed the duck some worms
                         // "BURP!" the duck just laid an egg
duck.Sleep();            // Sleeping~~~ (duck)

 

main.cpp

#include <iostream>
#include <string>
 
using namespace std;
 
/* Todo */
// 1. Should invoke Run(), Sleep()
class Animal
{
public:
    Animal() : type("UNKNOWN") {}
 
protected:
    string type;
};
 
/* Todo */
// 1. Implement Inheritance
// 2. Should invoke Woof()
class Dog
{
public:
    Dog()
    {
        type = "dog";
    }
};
 
/* Todo */
// 1. Implement Inheritance
// 2. Should invoke Fly(), Sing()
// 3. Should complete Feed(string food)
class Bird
{
public:
    Bird() : sound("UNKNOWN"), secretFood("UNKNOWN")
    {
        type = "BIRD";
    }
 
    void Feed(string food)
    {
        cout << "You just feed the " << type << " some " << food << endl;
        /* Todo */
        cout << "Nothing happened..." << endl;
    }
 
protected:
    string sound;
    string secretFood;
 
private:
    void LayAnEgg()
    {
        cout << "\"BURP!\" the " << type << " just laid an egg" << endl;
    }
};
 
/* Todo */
// 1. Implement Inheritance
// 2. Should handle parameters sound, secretfood in constructor
class Chicken
{
public:
    Chicken()
    {
        type = "chicken";
        /* Todo */
    }
};
 
/* Todo */
// 1. Implement Inheritance
// 2. Should handle parameters sound, secretfood in constructor
// 3. Should invoke Swim()
class Duck
{
public:
    Duck()
    {
        type = "Duck";
        /* Todo */
    }
};
 
int main()
{
    int testcase = 0;
    /* Todo */
    // 1. Handle input
    // 2. Create Dog, Chicken, Duck Object
    // 3. No need to change anything in switchcase
 
    switch (testcase)
    {
    case 1:
        dog.Run();
        dog.Woof();
        dog.Sleep();
        break;
 
    case 2:
        chicken.Run();
        chicken.Fly();
        chicken.Sing();
        chicken.Feed("rice");
        chicken.Sleep();
        break;
 
    case 3:
        chicken.Run();
        chicken.Fly();
        chicken.Sing();
        chicken.Feed("uncooked rice");
        chicken.Sleep();
        break;
 
    case 4:
        duck.Run();
        duck.Fly();
        duck.Sing();
        duck.Swim();
        duck.Feed("worms");
        duck.Sleep();
        break;
 
    case 5:
        duck.Run();
        duck.Fly();
        duck.Sing();
        duck.Swim();
        duck.Feed("bookworms");
        duck.Sleep();
        break;
 
    default:
        cout << "Remember to handle input!" << endl;
        break;
    }
 
    return 0;
}

Input

No need to handle input. (Input would be number 1~5 to represent the index of test-case)

Output

Depend on test case show in main.cpp.

Sample Input  Download

Sample Output  Download

Tags




Discuss