du7ri
du7ri

Reputation: 342

Is it possible to make a create which returns a boolean

hi i dont know if i asked the question clearly.

i have a deferred class "animal" which contains two features : "bite" (this returns a boolean -> bite:BOOLEAN) and "speak"(speak(word:BOOLEAN)).

now i made a class named "dog" what inherit from "animal". i redefined the two features without a compiler error. now i want to make a create, which contains the function bite (create bite:BOOLEAN).

this gaves me a compiler error, if i try it with the other feature it works fine.

my error code : VGCP(2) creators part lists improper identifier.

thank u for ur help

My Application.e:

note
    description : "root class of the application"
    date        : "$Date$"
    revision    : "$Revision$"

class
    APPLICATION

inherit
    ARGUMENTS_32

create
    make


feature
    d1:DOG

feature {NONE} -- Initialization

    make
            -- Run application.
        do
            print(d1.bite)
            print ("Hello Eiffel World!%N")
        end

end

my animal class:

deferred class 
    ANIMAL

    feature 
        bite_:BOOLEAN
            deferred 
            end


    feature 
        speak(word:BOOLEAN)
            deferred 
            end 
end            

my dog class:

class 
    DOG

inherit 
    ANIMAL

        redefine 
            bite_,speak_

                end

create 
    bite_

    feature 
        bite_:BOOLEAN
            do 
                Result:=5<3

            end



    feature 
        speak(word:BOOLEAN)
            do 


                print("yes")

            end 

Upvotes: 0

Views: 79

Answers (1)

Alexander Kogtenkov
Alexander Kogtenkov

Reputation: 5810

I'm not sure what is the intended semantics of feature speak, so I'll focus solely on feature bite, or, to express the supposed intention better can_bite. Class ANIMAL is similar to what you have:

deferred class
    ANIMAL

feature -- Status report

    can_bite: BOOLEAN
            -- Can this animal bite?
        deferred 
        end

end

With class DOG, two variations are possible: when every dog bites (possibly, depending on some internal state), when the ability to bite is set at object creation time.

In the first scenario, can_bite is a function and the class looks like

class
    DOG

inherit
    ANIMAL

feature -- Status report

    can_bite: BOOLEAN
            -- Can this animal bite?
        do
             Result := True -- It could also be some expression.
        end

end

In the second scenario, can_bite is an attribute initialized by a creation procedure, and the class looks like

class
    DOG

inherit
    ANIMAL

create
    make

feature {NONE} -- Creation

    make (is_biting: BOOLEAN)
            -- Initialize with ability `is_biting`.
        do
            can_bite := is_biting
        end

feature -- Status report

    can_bite: BOOLEAN
            -- Can this animal bite?

end

Note that in both cases the client has to create the dog object before making any calls on it. In the first case, the default creation procedure is used, and the client code looks like

create d1
print (d1.can_bite)

In the second case, the specific creation procedure is used (it initializes can_bite), and the client code looks like

create d1.make (True)
print (d1.can_bite)

Upvotes: 0

Related Questions