Thread: ADT and pure virtual functions help

  1. #1
    left crog... back when? incognito's Avatar
    Join Date
    Oct 2001
    Posts
    1,427

    ADT and pure virtual functions help

    // Listing 14.10
    // Deriving ADTs from other ADTs
    Code:
    #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";}
    	virtual ~Mammal() { cout << "Mammal destructor...\n";}
    	virtual void Reproduce() const
    		{ cout << "Mammal reproduction depicted...\n"; }
    };
    
    class Fish : public Animal
    {
    public:
    	Fish(int age):Animal(age)
    		{ cout << "Fish constructor...\n";}
    	virtual ~Fish() {cout << "Fish destructor...\n";  }
    	virtual void Sleep() const { cout << "fish snoring...\n"; }
    	virtual void Eat() const { cout << "fish feeding...\n"; }
    	virtual void Reproduce() const
    		{ cout << "fish laying eggs...\n"; }
    	virtual void Move() const
    		{ cout << "fish swimming...\n";   }
    	virtual void Speak() const { }
    };
    
    class Horse : public Mammal
    {
    public:
    	Horse(int age, COLOR color ):
    	Mammal(age), itsColor(color)
    		{ cout << "Horse constructor...\n"; }
    	virtual ~Horse() { cout << "Horse destructor...\n"; }
    	virtual void Speak()const { cout << "Whinny!... \n"; }
    	virtual COLOR GetItsColor() const { return itsColor; }
    	virtual void Sleep() const
    		{ cout << "Horse snoring...\n"; }
    	virtual void Eat() const { cout << "Horse feeding...\n"; }
    	virtual 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"; }
    	virtual ~Dog() { cout << "Dog destructor...\n"; }
    	virtual void Speak()const { cout << "Whoof!... \n"; }
    	virtual void Sleep() const { cout << "Dog snoring...\n"; }
    	virtual void Eat() const { cout << "Dog eating...\n"; }
    	virtual void Move() const  { cout << "Dog running...\n"; }
    	virtual void Reproduce() const
    		{ cout << "Dogs reproducing...\n"; }
    
    protected:
    	COLOR itsColor;
    };
    
    int main()
    {
    	Animal *pAnimal=0;
    	int choice;
    	bool fQuit = false;
    
    	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: fQuit = true;
    				break;
    		}
    		if (fQuit)
    			break;
    
    		pAnimal->Speak();
    		pAnimal->Eat();
    		pAnimal->Reproduce();
    		pAnimal->Move();
    		pAnimal->Sleep();
    		delete pAnimal;
    		cout << "\n";
    	}
    	return 0;
    }
    /*######################################*/

    I have a question, because even though Mammal doesn't have a lot of method defined, I want to know if it still has this methods
    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 Move() const = 0;
    virtual void Speak() const = 0;

    because it derives from Animal and therefore making it and ADT, so in turn you cannot make an instance of it? Is this right? If not please explain.
    Last edited by incognito; 03-02-2002 at 10:06 AM.
    There are some real morons in this world please do not become one of them, do not become a victim of moronitis. PROGRAMMING IS THE FUTURE...THE FUTURE IS NOW!!!!!!!!!

    "...The only real game I thank in the world is baseball..." --Babe Ruth

    "Life is beautiful"-Don Corleone right before he died.

    "The expert on anything was once a beginner" -Baseball poster I own.


    Left cprog on 1-3-2005. Don't know when I am coming back. Thanks to those who helped me over the years.

  2. #2
    left crog... back when? incognito's Avatar
    Join Date
    Oct 2001
    Posts
    1,427
    anybody...........
    There are some real morons in this world please do not become one of them, do not become a victim of moronitis. PROGRAMMING IS THE FUTURE...THE FUTURE IS NOW!!!!!!!!!

    "...The only real game I thank in the world is baseball..." --Babe Ruth

    "Life is beautiful"-Don Corleone right before he died.

    "The expert on anything was once a beginner" -Baseball poster I own.


    Left cprog on 1-3-2005. Don't know when I am coming back. Thanks to those who helped me over the years.

  3. #3
    Registered User
    Join Date
    Jan 2002
    Location
    Vancouver
    Posts
    2,212
    Three ...'s is sufficient, more than three .'s looks stupid.

  4. #4
    left crog... back when? incognito's Avatar
    Join Date
    Oct 2001
    Posts
    1,427
    That doesn't answer my question brian...............don't you have anything better to do than to just ****ing criticize people?
    There are some real morons in this world please do not become one of them, do not become a victim of moronitis. PROGRAMMING IS THE FUTURE...THE FUTURE IS NOW!!!!!!!!!

    "...The only real game I thank in the world is baseball..." --Babe Ruth

    "Life is beautiful"-Don Corleone right before he died.

    "The expert on anything was once a beginner" -Baseball poster I own.


    Left cprog on 1-3-2005. Don't know when I am coming back. Thanks to those who helped me over the years.

  5. #5
    left crog... back when? incognito's Avatar
    Join Date
    Oct 2001
    Posts
    1,427
    second and last bump for this thread........"bump"
    There are some real morons in this world please do not become one of them, do not become a victim of moronitis. PROGRAMMING IS THE FUTURE...THE FUTURE IS NOW!!!!!!!!!

    "...The only real game I thank in the world is baseball..." --Babe Ruth

    "Life is beautiful"-Don Corleone right before he died.

    "The expert on anything was once a beginner" -Baseball poster I own.


    Left cprog on 1-3-2005. Don't know when I am coming back. Thanks to those who helped me over the years.

  6. #6
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,138
    The ones that are written functionname()=0 means that they can only be used by an inheritted class and that inheritted class will modify the function.


    [edit]Sorry, I didn't really read ur question. Why don't u try creating an instance of an animal and see what happens. I think that you should be able to create an instance of animal but will not be able to call the function. Not sure though cause I never really tried.[edit]
    Last edited by golfinguy4; 03-02-2002 at 03:44 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ question
    By datainjector in forum C++ Programming
    Replies: 59
    Last Post: 12-20-2002, 09:21 PM