gpt4 book ai didi

fortran - Fortran 2003 的完整面向对象示例?

转载 作者:行者123 更新时间:2023-12-01 12:53:04 25 4
gpt4 key购买 nike

任何人都可以给我一个例子,说明一个简单的 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();
}

我的代码有问题,请参阅以下内容

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

这是错误的。 CatType 没有 AnimalType 组件。它扩展了 AnimalType 类型。 ctor 是一个子例程,您可以将它用作函数。我建议您将构造函数(但它更多只是初始化,而不是分配)定义为函数,但也可以使用子例程。你可以像你一样使用接口(interface) block 来实现与类名相同的名称,但你必须保持一致并使用 CALL,如果你将它定义为子例程。

在派生类的构造函数中,您可以只设置正确的组件,或者使用基类的构造函数。然后不要忘记为您的类型编写正确的工作用户定义的分配。

关于fortran - Fortran 2003 的完整面向对象示例?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11023792/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com