title: Full object oriented example with fortran 2003?
body: <p>Can anyone please give me an example on how is it supposed to be for a simple Fortran 2003 Object oriented layout equivalent to this C++ code:</p>
  
  <pre><code>stefanos-imac:tmp borini$ more Animal.h 
  class Animal {
      public:
          Animal(int age);
          ~Animal();
          virtual void speak();
          int getAge();
      private:
          int age;
  };
  
  stefanos-imac:tmp borini$ more Animal.cpp 
  #include &lt;Animal.h&gt;
  #include &lt;iostream&gt;
  Animal::Animal(int age) {
      std::cout &lt;&lt; "init animal" &lt;&lt; age &lt;&lt; std::endl;
      this-&gt;age = age;
  }
  Animal::~Animal() {
      std::cout &lt;&lt; "dtor animal" &lt;&lt; std::endl;
  }
  void Animal::speak() {
      std::cout &lt;&lt; "speak not reimplemented" &lt;&lt; std::endl;
  }
  int Animal::getAge() {
      return this-&gt;age;
  }
  stefanos-imac:tmp borini$ more Cat.h 
  #include &lt;Animal.h&gt;
  class Cat : public Animal {
      public:
          Cat(int age);
          ~Cat();
          virtual void speak();
  };
  stefanos-imac:tmp borini$ more Cat.cpp 
  #include &lt;Cat.h&gt;
  #include &lt;iostream&gt;
  Cat::Cat(int age) : Animal(age) {
      std::cout &lt;&lt; "init cat" &lt;&lt; std::endl;
  }
  Cat::~Cat() { 
      std::cout &lt;&lt; "dtor cat" &lt;&lt; std::endl;
  }
  void Cat::speak() {
      std::cout &lt;&lt; "meow" &lt;&lt; std::endl;
  }
  stefanos-imac:tmp borini$ more main.cpp 
  #include &lt;iostream&gt;
  #include &lt;Cat.h&gt;
  
  int main() {
      Cat c(10);
  
      std::cout &lt;&lt; c.getAge() &lt;&lt;std::endl;
      c.speak();
  }
  </code></pre>
  
  <p>I am having problems with my code, see the following</p>
  
  <pre><code>stefanos-imac:oop borini$ more Animal.f90 
  module AnimalModule
     implicit none
     private
     public :: AnimalType
  
     type :: AnimalType
        private
        integer :: age
     contains
        procedure :: getAge 
        procedure :: speak
        final :: dtor
     end type
  
     interface AnimalType
        module procedure ctor
     end interface
  contains 
     subroutine ctor(self, age)
        type(AnimalType), intent(inout) :: self
        integer :: age
        print *, "Constructor Animal"
        self%age = age
     end subroutine 
     subroutine dtor(self)
        type(AnimalType), intent(inout) :: self
        print *, "Destroying animal"
     end subroutine 
  
     function getAge(self)
        class(AnimalType), intent(inout) :: self
        integer :: getAge
        getAge = self%age
     end function
     subroutine speak(self)
        class(AnimalType), intent(in) :: self
  
        print *, "Animal::speak not overridden"
     end subroutine
  end
  
  stefanos-imac:oop borini$ more Cat.f90 
  module CatModule
     use AnimalModule
     implicit none
     private
  
     type, extends(AnimalType) :: CatType
        private
     contains
        procedure :: speak 
        final :: dtor
     end type
  
     interface CatType
        module procedure ctor
     end interface
  
  contains 
     subroutine ctor(self, age)
        type(CatType), intent(inout) :: self
        integer, intent(in) :: age
        print *, "Constructor Cat"
        self%AnimalType = AnimalType(age)
     end subroutine 
     subroutine dtor(self)
        type(CatType), intent(inout) :: self
        print *, "Destroying Cat"
     end subroutine 
     subroutine speak(self)
        class(CatType), intent(in) :: self
  
        print *, "Meow"
     end subroutine
  end
  
  stefanos-imac:oop borini$ ifort Animal.f90 Cat.f90 
  Cat.f90(10): error #8341: Final subroutines are not inherited through type extension and cannot be overridden.   [DTOR]
        final :: dtor
  ---------------^
  Cat.f90(22): error #6292: The parent type of this field is use associated with the PRIVATE fields attribute   [AGE]
        self%AnimalType = AnimalType(age)
  -----------------------------------^
  compilation aborted for Cat.f90 (code 1)
  stefanos-imac:oop borini$ 
  </code></pre>
  
  <p>In particular, it's not clear to me how to initialize the base class, and how to define the destructor for the derived class.</p>
  
  <p>Thanks</p>