Challenge: solve this multiple inheritance problem in your favorite language

mw mingwu at gmail.com
Thu Jun 4 07:11:26 UTC 2020


Problem:

Suppose a person who has both US & UK residence, travel to Paris, 
and feel ill need to withdraw some money and see a doctor:

1) the person can only have 1 (one) name
2) the person has 3 addresses: one in US, one in UK, and a temp 
hotel address in Paris
3) the person's bank account that can only be read by the bank
4) the person's health info that can only be read by doctor

I will show the Eiffel program, with the compiler ensures all 
these constraints.

First, let me show your the program running result:
--------------------------------------------------------------------------------------
$ compile  app.e -o app  # Eiffel compiler command
$ ./app
A person have only *one* name
My hotel in Paris
US addr: London
UK addr: NewYork
bank_acct: only view-able by bank
health_info: only view-able by doctor

--------------------------------------------------------------------------------------


This is the multiple inheritance implementation in Eiffel: with 
some comments

--------------------------------------------------------------------------------------
class PERSON

feature {ANY}   -- ANY, basically means `public` in 
C/C++/C#/D/Java world
    name: STRING is do Result := "A person have only *one* name"  
end
    addr: STRING is do Result := "A person can have *multi*.addr" 
end

feature {BANK}
    bank_acct: STRING is do Result := "bank_acct: only view-able 
by bank" end

feature {DOCTOR}
    health_info: STRING is do Result := "health_info: only 
view-able by doctor" end

end

--------------------------------------------------------------------------------------
class UK_RESIDENT
inherit PERSON redefine addr end       -- redefine == override in 
C#/D/Java
feature {ANY}
    addr: STRING is do Result := "London" end
end

--------------------------------------------------------------------------------------
class US_RESIDENT
inherit PERSON redefine addr end       -- redefine == override in 
C#/D/Java
feature {ANY}
    addr: STRING is do Result := "NewYork" end
end

--------------------------------------------------------------------------------------
class VISITOR
inherit             -- Note: inherit PERSON 3 times! but treat 
each 3 address individually
         UK_RESIDENT rename addr as uk_addr end
         US_RESIDENT rename addr as us_addr end
         PERSON      redefine addr select addr end

create {ANY}   -- means c-tor in C++/C#/D/Java
    make

feature {ANY}
    make is do end

    addr: STRING is
       do
          Result := "My hotel in Paris"
       end
end

--------------------------------------------------------------------------------------
class BANK

create {ANY}
    make

feature
   make is do end

   read_bank_acct(u: PERSON) is
     do
       io.put_string(u.bank_acct    + "%N")
     --io.put_string(u.health_info  + "%N") -- ****** Fatal Error: 
This feature is only exported to {DOCTOR}.
     end
end

--------------------------------------------------------------------------------------
class DOCTOR

create {ANY}
    make

feature
   make is do end

   read_health_info(u: PERSON) is
     do
     --io.put_string(u.bank_acct    + "%N") -- ****** Fatal Error: 
This feature is only exported to {BANK}.
       io.put_string(u.health_info  + "%N")
     end
end

--------------------------------------------------------------------------------------
-- to build: compile  app.e -o app
class APP

create {ANY}
    main

feature {ANY}
    visitor: VISITOR
    bank: BANK
    doctor: DOCTOR

    print_uk_addr(u: VISITOR) is do io.put_string("US addr: " + 
u.uk_addr + "%N") end
    print_us_addr(u: VISITOR) is do io.put_string("UK addr: " + 
u.us_addr + "%N") end

    main is
       do
          create bank.make
          create doctor.make
          create visitor.make

          io.put_string(visitor.name + "%N")
          io.put_string(visitor.addr + "%N")
          print_uk_addr(visitor)
          print_us_addr(visitor)

          bank.read_bank_acct(visitor)
          doctor.read_health_info(visitor)
       end

end
--------------------------------------------------------------------------------------

Note: the `--` commented out line, followed by the compiler error 
message: "Fatal Error ...."


(I'm a bit busy today, stop here. I will continue tomorrow).

Feel free to add your implementation in your favorite programming 
language.


More information about the Digitalmars-d mailing list