2012-06-13 4 views
3

사람이 나에게 그것은이 C++ 코드에 대한 간단한 포트란 2003 개체 지향 레이아웃에 해당이 될 것으로 예상되는 방법에 대한 예를 제공하시기 바랍니다 수 :Fortran 2003의 전체 객체 지향 예제?

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 <Animal.h> 
#include <iostream> 
Animal::Animal(int age) { 
    std::cout << "init animal" << age << std::endl; 
    this->age = age; 
} 
Animal::~Animal() { 
    std::cout << "dtor animal" << std::endl; 
} 
void Animal::speak() { 
    std::cout << "speak not reimplemented" << std::endl; 
} 
int Animal::getAge() { 
    return this->age; 
} 
stefanos-imac:tmp borini$ more Cat.h 
#include <Animal.h> 
class Cat : public Animal { 
    public: 
     Cat(int age); 
     ~Cat(); 
     virtual void speak(); 
}; 
stefanos-imac:tmp borini$ more Cat.cpp 
#include <Cat.h> 
#include <iostream> 
Cat::Cat(int age) : Animal(age) { 
    std::cout << "init cat" << std::endl; 
} 
Cat::~Cat() { 
    std::cout << "dtor cat" << std::endl; 
} 
void Cat::speak() { 
    std::cout << "meow" << std::endl; 
} 
stefanos-imac:tmp borini$ more main.cpp 
#include <iostream> 
#include <Cat.h> 

int main() { 
    Cat c(10); 

    std::cout << c.getAge() <<std::endl; 
    c.speak(); 
} 

내 코드에 문제가 오전, 다음

참조
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$ 

특히 기본 클래스를 초기화하는 방법과 파생 클래스의 소멸자를 정의하는 방법이 명확하지 않습니다.

감사

답변

4
subroutine ctor(self, age) 
     type(CatType), intent(inout) :: self 
     integer, intent(in) :: age 
     print *, "Constructor Cat" 
     self%AnimalType = AnimalType(age) 
    end subroutine 

이것은 잘못된 것입니다. CatType에 AnimalType 구성 요소가 없습니다. AnimalType 유형을 확장합니다. ctor는 서브 루틴이며 함수로 사용합니다. 나는 생성자를 정의 할 것을 제안한다. (하지만 초기화가 더 많을뿐 할당하지는 않는다.) 함수로 서브 루틴을 사용할 수도있다. 인터페이스 블록을 사용하여 클래스 이름과 동일한 이름을 얻을 수 있지만 일관성을 유지하고 서브 루틴으로 정의한 경우 CALL을 사용해야합니다.

파생 클래스의 생성자에서 올바른 구성 요소를 설정하거나 기본 클래스의 생성자를 사용할 수 있습니다. 그런 다음 유형에 맞는 올바른 사용자 정의 할당을 작성하는 것을 잊지 마십시오.

+2

실제로 Fortran 2008 표준 (ISO/IEC JTC 1/SC 22/WG 5/N1830 초안의 4.5.7.2.2 절)은 * 확장 유형에는 스칼라, 비 할당 자, 할당 할 수없는 부모 구성 요소이며 부모 유형의 유형 및 유형 매개 변수가 있습니다. 이 구성 요소의 이름은 첫 번째 문장을 틀리게 만드는 부모 유형 이름. *입니다. 필자는 Fortran 2003 표준을 사용할 수는 없지만이 언어의 측면이 표준간에 변경되었는지는 의문의 여지가 있습니다. 나는 당신의 대답의 나머지 부분에 대한 일반적인 추측에 동의한다. –

+0

좋습니다. 고맙습니다. 어쩌면 나는 그것을 읽었지 만 나는 그것을 잊어 버렸다. –