Deriving Abstract Classes from Other Abstract Classes : abstract class « Class « C++ Tutorial

Home
C++ Tutorial
1.Language Basics
2.Data Types
3.Operators statements
4.Array
5.Development
6.Exceptions
7.Function
8.Structure
9.Class
10.Operator Overloading
11.Pointer
12.File Stream
13.template
14.STL Introduction
15.string
16.vector
17.list
18.bitset
19.set multiset
20.valarray
21.queue stack
22.deque
23.map multimap
24.STL Algorithms Modifying sequence operations
25.STL Algorithms Non modifying sequence operations
26.STL Algorithms Binary search
27.STL Algorithms Sorting
28.STL Algorithms Merge
29.STL Algorithms Min Max
30.STL Algorithms Iterator
31.STL Algorithms Heap
32.STL Algorithms Helper
C / ANSI-C
C Tutorial
C++
Visual C++ .NET
C++ Tutorial » Class » abstract class 
9.31.1.Deriving Abstract Classes from Other Abstract Classes
#include <iostream>
using namespace std;

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

class Animal        
{
  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..." << endl;
}

class Mammal : public Animal
{
  public:
    Mammal(int age):Animal(age){ cout << "Mammal constructor..." << endl;}
    virtual ~Mammal() { cout << "Mammal destructor..." << endl;}
    virtual void Reproduce() constcout << "Mammal reproduction depicted..."<<endl; }
};

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

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

  protected:
    COLOR itsColor;
};

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

  protected:
    COLOR itsColor;
};

int main()
{
   Animal *pAnimal=0;
   pAnimal = new Dog(5,Brown);
   pAnimal->Speak();
   pAnimal->Eat();
   pAnimal->Reproduce();
   pAnimal->Move();
   pAnimal->Sleep();
   delete pAnimal;
   
   pAnimal = new Horse(4,Black);
   pAnimal->Speak();
   pAnimal->Eat();
   pAnimal->Reproduce();
   pAnimal->Move();
   pAnimal->Sleep();
   delete pAnimal;
   
   pAnimal = new Fish (5);
   pAnimal->Speak();
   pAnimal->Eat();
   pAnimal->Reproduce();
   pAnimal->Move();
   pAnimal->Sleep();
   delete pAnimal;
    return 0;
 }
9.31.abstract class
9.31.1.Deriving Abstract Classes from Other Abstract Classes
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.