Make your own free website on Tripod.com
« September 2018 »
S M T W T F S
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30
You are not logged in. Log in
Entries by Topic
All topics  «
Blog Tools
Edit your Blog
Build a Blog
RSS Feed
View Profile
Extra Credit
Saturday, 10 May 2008
Extra Credit

#include <iostream>
using namespace std;


enum COLOR { Red, Green, Blue, Yellow, White, Black, Brown } ;


class Animal        // common base to both Mammal and Fish
{
    public:
       Animal(int);
       virtual ~Animal() { cout << "Animal destructor...\n"; }
       virtual int GetAge() const { return itsAge; }
       virtual void SetAge(int age) { itsAge = age; }
       virtual void Sleep() const = 0;
       virtual void Eat() const = 0;
       virtual void Reproduce() const = 0;
       virtual void Move() const = 0;
       virtual void Speak() const = 0;
    private:
       int itsAge;
};

Animal::Animal(int age):
itsAge(age)
{
    cout << "Animal constructor...\n";
}

class Mammal : public Animal
{
public:
 Mammal(int age):Animal(age)
  { cout << "Mammal constructor...\n";}
 ~Mammal() { cout << "Mammal destructor...\n";}
 void Reproduce() const
  { cout << "Mammal reproduction depicted...\n"; }
};
 
class Fish : public Animal
{
    public:
        Fish(int age):Animal(age)
          { cout << "Fish constructor...\n";}
        ~Fish() {cout << "Fish destructor...\n";  }
         void Sleep() const { cout << "fish snoring...\n"; }
         void Eat() const { cout << "fish feeding...\n"; }
         void Reproduce() const
          { cout << "fish laying eggs...\n"; }
         void Move() const
           { cout << "fish swimming...\n";   }
        void Speak() const { }
};

class Horse : public Mammal
{
    public:
       Horse(int age, COLOR color ):
       Mammal(age), itsColor(color)
             { cout << "Horse constructor...\n"; }
      ~Horse() { cout << "Horse destructor...\n"; }
      void Speak()const { cout << "Whinny!... \n"; }
      COLOR GetItsColor() const { return itsColor; }
       void Sleep() const
          { cout << "Horse snoring...\n"; }
       void Eat() const { cout << "Horse feeding...\n"; }
      void Move() const { cout << "Horse running...\n";}
    protected:
       COLOR itsColor;
};

class Dog : public Mammal
{
    public:
       Dog(int age, COLOR color ):
       Mammal(age), itsColor(color)
             { cout << "Dog constructor...\n"; }
       ~Dog() { cout << "Dog destructor...\n"; }
      void Speak()const { cout << "Whoof!... \n"; }
      void Sleep() const { cout << "Dog snoring...\n"; }
      void Eat() const { cout << "Dog eating...\n"; }
      void Move() const  { cout << "Dog running...\n"; }
      void Reproduce() const
           { cout << "Dogs reproducing...\n"; }
    protected:
       COLOR itsColor;
};

int main()
{
       enum flag { x, y };
       Animal *pAnimal=0;
       int choice;
       flag quit = x;
       while (1)
       {
          cout << "(1)Dog (2)Horse (3)Fish (0)Quit: ";
          cin >> choice;
          switch (choice)
          {
             case 1: pAnimal = new Dog(5,Brown);
              break;
             case 2: pAnimal = new Horse(4,Black);
              break;
            case 3: pAnimal = new Fish (5);
              break;
            default: quit = y;
             break;
         }
         if (quit)
            break;
         pAnimal->Speak();
         pAnimal->Eat();
         pAnimal->Reproduce();
         pAnimal->Move();
         pAnimal->Sleep();
         delete pAnimal;
         cout << "\n";
     }
     return 0;
 }


Posted by veronicak5678 at 11:22 AM EDT

Newer | Latest | Older