fortran 2003的完整面向对象示例? 能有人给我举个例子,它是如何针对一个简单的FORTRAN 2003面向对象布局的,它与这个C++代码相当: 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(); }
特别是,我不清楚如何初始化基类,以及如何为派生类定义析构函数 谢谢 这是错误的。CatType没有AnimalType组件。它扩展了AnimalType类型。ctor是一个子例程,您可以将其用作函数。我建议您将构造函数定义为一个函数(但更多的是初始化,而不是分配),但也可以使用子例程。可以像使用接口块一样使用接口块来实现与类名相同的名称,但如果将其定义为子例程,则必须保持一致并使用fortran 2003的完整面向对象示例? 能有人给我举个例子,它是如何针对一个简单的FORTRAN 2003面向对象布局的,它与这个C++代码相当: 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(); },fortran,fortran2003,Fortran,Fortran2003,特别是,我不清楚如何初始化基类,以及如何为派生类定义析构函数 谢谢 这是错误的。CatType没有AnimalType组件。它扩展了AnimalType类型。ctor是一个子例程,您可以将其用作函数。我建议您将构造函数定义为一个函数(但更多的是初始化,而不是分配),但也可以使用子例程。可以像使用接口块一样使用接口块来实现与类名相同的名称,但如果将其定义为子例程,则必须保持一致并使用CALL 在派生类的构造函数中,您可以只设置正确的组件,或者使用基类的构造函数。请不要忘记为您的类型编写正确的工作用
CALL
在派生类的构造函数中,您可以只设置正确的组件,或者使用基类的构造函数。请不要忘记为您的类型编写正确的工作用户定义赋值。实际上,Fortran 2008标准(标准最新草案的第4.5.7.2.2条——ISO/IEC JTC 1/SC 22/WG 5/N1830)规定扩展类型具有标量、非指针、不可分配,具有父类型的类型和类型参数的父组件。此组件的名称是父类型名称。这使得你的第一句话不正确。我手头没有Fortran 2003标准,但我怀疑该语言的这一方面在不同标准之间发生了变化。不过,我同意你其余答案的主旨。好的,谢谢。也许我读过,但我肯定忘了。
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$
subroutine ctor(self, age)
type(CatType), intent(inout) :: self
integer, intent(in) :: age
print *, "Constructor Cat"
self%AnimalType = AnimalType(age)
end subroutine