Loading ...
Sorry, an error occurred while loading the content.

C'mon guys, sort this out !

Expand Messages
  • Steve
    Hi, I am new to Eiffel (started 3 weeks ago) and I have a multiple inheritance problem which I am not quite sure how to implement in Eiffel. Can any of you
    Message 1 of 3 , Nov 1, 2002
    • 0 Attachment
      Hi,

      I am new to Eiffel (started 3 weeks ago) and I have a multiple
      inheritance problem which I am not quite sure how to implement in
      Eiffel. Can any of you guys please help out?

      Basically, I have the following classes:
      (Please don't be put-off, it's very easy stuff! – nothing
      complex)





      deferred class WRITING_INSTRUMENT

      feature
      color : STRING
      price : INTEGER

      setPrice(aPrice : INTEGER) is
      do
      price := aPrice
      end -- setPrice

      setColor(aColor : STRING) is
      do
      color := clone(aColor)
      end -- setColor

      stats is -- display a writing instrument
      do
      io.putstring("Color : ")
      io.putstring(color); io.new_line
      io.putstring("Price : ")
      io.putint(price); io.new_line
      end -- stats

      getdata is
      do
      io.putstring("Enter color : ")
      io.readword
      setColor(io.laststring)
      io.putstring("Enter price : $")
      io.readint
      setPrice(io.lastint)
      end -- getdata

      end -- WRITING_INSTRUMENT






      class PENCIL

      inherit
      WRITING_INSTRUMENT
      redefine
      stats,
      getdata
      end

      feature
      eraserSize : INTEGER
      leadthickness : INTEGER

      setEraserSize(size : INTEGER) is
      do
      eraserSize := size
      end -- setEraserSize

      setLeadThickness(thickness : INTEGER) is
      do
      leadThickness := thickness
      end -- setLeadThickness

      stats is
      do
      precursor -- calls stats from WRITING_INSTRUMENT

      io.putstring("Eraser size : ")
      io.putint(eraserSize); io.new_line
      io.putstring("Lead thickness : ")
      io.putint(leadThickness); io.new_line
      end -- stats

      getdata is
      do
      precursor -- calls getdata from WRITING_INSTRUMENT

      io.putstring("Enter eraser size : ")
      io.readint
      setEraserSize(io.lastint)
      io.putstring("Enter lead thickness : ")
      io.readint
      setLeadThickness(io.lastint)
      end -- getdata

      end -- PENCIL






      deferred class PEN

      inherit
      WRITING_INSTRUMENT
      redefine
      stats,
      getdata
      end

      feature
      inkColor : STRING
      pointThickness : INTEGER

      setInkColor(aColor : STRING) is
      do
      inkColor := clone(aColor)
      end -- setInkColor

      setPointThickness(thickness : INTEGER) is
      do
      pointThickness := thickness
      end -- setPointThickness

      stats is
      do
      precursor -- calls stats from WRITING_INSTRUMENT

      io.putstring("Ink color : ")
      io.putstring(inkColor); io.new_line
      io.putstring("Point thickness : ")
      io.putint(pointThickness); io.new_line
      end -- stats

      getdata is
      do
      precursor -- calls getdata from WRITING_INSTRUMENT

      io.putstring("Enter ink color : ")
      io.readword
      setInkColor(io.laststring)
      io.putstring("Enter point thickness : ")
      io.readint
      setPointThickness(io.lastint)
      end -- getdata

      end -- PEN






      class
      BALLPOINT_PEN

      inherit
      PEN
      redefine
      stats,
      getdata
      end

      feature
      ballType : STRING

      setBallType(ball : STRING) is
      do
      ballType := clone(ball)
      end -- setBallType

      stats is
      do
      precursor -- calls stats from PEN

      io.putstring("Ball type : ")
      io.putstring(ballType); io.new_line
      end -- stats

      getdata is
      do
      precursor -- calls getdata from PEN

      io.putstring("Enter ball type : ")
      io.readword
      setBallType(io.laststring)
      end -- getdata

      end -- class BALLPOINT_PEN




      With the above classes I want to create a new class called
      NOVELTY_PENCIL. A novelty pencil not only functions as a pencil but
      also has a ballpoint pen at the other end. It does of course have a
      single 'color' and 'price', but it also has the functionality of
      pencils and ballpoint pens. Thus, NOVELTY_PENCIL inherits from PENCIL
      and BALLPOINT_PEN.

      Has anyone got any ideas how I can make this work without clashing
      with the attributes from the inherited hierarchy? Thank you for your
      help.

      Thank you.
    • Ian Joyner
      Well, it really smells like a homework assignment. Whether it is or not, I can hint that you first assemble the NOVELTY_PEN class inheriting BALLPOINT_PEN and
      Message 2 of 3 , Nov 1, 2002
      • 0 Attachment
        Well, it really smells like a homework assignment. Whether it is or not,
        I can hint that you first assemble the NOVELTY_PEN class inheriting
        BALLPOINT_PEN and PENCIL, compile it and see where the compiler
        complains about clashes. Use renames/select clauses to disambiguate,
        reading up about these in OOSC or Eiffel: The Language. Then when you
        turn your assignment in, or turn it into a major project, use standard
        Eiffel (and natural language) formatting. ':'s are not preceded by white
        space (in both code and io routines), but '(' is (observe this in any
        natural language book). Don't use upcase to delimit words in
        identifiers, eg 'setPrice' should be 'set_price'. Then you have
        consistency of underscore use in both upcase names (as in class names,
        or #define names in C). A practice I have recently used is to call
        parameters to 'set' routines 'to_'x, so name 'aPrice' 'to_price'. Use a
        require clause:

        set_price (to_price: INTEGER) is
        require
        to_price >= 0
        do
        price := to_price
        ensure
        price = to_price
        end -- set_price

        and get top marks! But better than that, this is just good programming,
        using something useful, like design by contract, instead of useless junk
        like UML.

        Good luck.

        Ian

        On Saturday, November 2, 2002, at 01:39 PM, Steve wrote:

        > Hi,
        >
        > I am new to Eiffel (started 3 weeks ago) and I have a multiple
        > inheritance problem which I am not quite sure how to implement in
        > Eiffel. Can any of you guys please help out?
        >
        > Basically, I have the following classes:
        > (Please don't be put-off, it's very easy stuff! – nothing
        > complex)
        >
        >
        >
        >
        >
        > deferred class WRITING_INSTRUMENT
        >
        > feature
        > color : STRING
        > price : INTEGER
        >
        > setPrice(aPrice : INTEGER) is
        > do
        > price := aPrice
        > end -- setPrice
        >
        > setColor(aColor : STRING) is
        > do
        > color := clone(aColor)
        > end -- setColor
        >
        > stats is -- display a writing instrument
        > do
        > io.putstring("Color : ")
        > io.putstring(color); io.new_line
        > io.putstring("Price : ")
        > io.putint(price); io.new_line
        > end -- stats
        >
        > getdata is
        > do
        > io.putstring("Enter color : ")
        > io.readword
        > setColor(io.laststring)
        > io.putstring("Enter price : $")
        > io.readint
        > setPrice(io.lastint)
        > end -- getdata
        >
        > end -- WRITING_INSTRUMENT
        >
        >
        >
        >
        >
        >
        > class PENCIL
        >
        > inherit
        > WRITING_INSTRUMENT
        > redefine
        > stats,
        > getdata
        > end
        >
        > feature
        > eraserSize : INTEGER
        > leadthickness : INTEGER
        >
        > setEraserSize(size : INTEGER) is
        > do
        > eraserSize := size
        > end -- setEraserSize
        >
        > setLeadThickness(thickness : INTEGER) is
        > do
        > leadThickness := thickness
        > end -- setLeadThickness
        >
        > stats is
        > do
        > precursor -- calls stats from WRITING_INSTRUMENT
        >
        > io.putstring("Eraser size : ")
        > io.putint(eraserSize); io.new_line
        > io.putstring("Lead thickness : ")
        > io.putint(leadThickness); io.new_line
        > end -- stats
        >
        > getdata is
        > do
        > precursor -- calls getdata from WRITING_INSTRUMENT
        >
        > io.putstring("Enter eraser size : ")
        > io.readint
        > setEraserSize(io.lastint)
        > io.putstring("Enter lead thickness : ")
        > io.readint
        > setLeadThickness(io.lastint)
        > end -- getdata
        >
        > end -- PENCIL
        >
        >
        >
        >
        >
        >
        > deferred class PEN
        >
        > inherit
        > WRITING_INSTRUMENT
        > redefine
        > stats,
        > getdata
        > end
        >
        > feature
        > inkColor : STRING
        > pointThickness : INTEGER
        >
        > setInkColor(aColor : STRING) is
        > do
        > inkColor := clone(aColor)
        > end -- setInkColor
        >
        > setPointThickness(thickness : INTEGER) is
        > do
        > pointThickness := thickness
        > end -- setPointThickness
        >
        > stats is
        > do
        > precursor -- calls stats from WRITING_INSTRUMENT
        >
        > io.putstring("Ink color : ")
        > io.putstring(inkColor); io.new_line
        > io.putstring("Point thickness : ")
        > io.putint(pointThickness); io.new_line
        > end -- stats
        >
        > getdata is
        > do
        > precursor -- calls getdata from WRITING_INSTRUMENT
        >
        > io.putstring("Enter ink color : ")
        > io.readword
        > setInkColor(io.laststring)
        > io.putstring("Enter point thickness : ")
        > io.readint
        > setPointThickness(io.lastint)
        > end -- getdata
        >
        > end -- PEN
        >
        >
        >
        >
        >
        >
        > class
        > BALLPOINT_PEN
        >
        > inherit
        > PEN
        > redefine
        > stats,
        > getdata
        > end
        >
        > feature
        > ballType : STRING
        >
        > setBallType(ball : STRING) is
        > do
        > ballType := clone(ball)
        > end -- setBallType
        >
        > stats is
        > do
        > precursor -- calls stats from PEN
        >
        > io.putstring("Ball type : ")
        > io.putstring(ballType); io.new_line
        > end -- stats
        >
        > getdata is
        > do
        > precursor -- calls getdata from PEN
        >
        > io.putstring("Enter ball type : ")
        > io.readword
        > setBallType(io.laststring)
        > end -- getdata
        >
        > end -- class BALLPOINT_PEN
        >
        >
        >
        >
        > With the above classes I want to create a new class called
        > NOVELTY_PENCIL. A novelty pencil not only functions as a pencil but
        > also has a ballpoint pen at the other end. It does of course have a
        > single 'color' and 'price', but it also has the functionality of
        > pencils and ballpoint pens. Thus, NOVELTY_PENCIL inherits from PENCIL
        > and BALLPOINT_PEN.
        >
        > Has anyone got any ideas how I can make this work without clashing
        > with the attributes from the inherited hierarchy? Thank you for your
        > help.
        >
        > Thank you.
        >
        >
        >
        >
        > ---------------------------
        >
        > http://www.eiffel-nice.org/
        >
        > --------------------------
        >
        > Your use of Yahoo! Groups is subject to
        > http://docs.yahoo.com/info/terms/
        >
        >
        >
        >
        Ian
      • Chris Saunders
        A few hints:- If you have attributes from different classes that have the same name and you need to use them as separate attributes in your new class then:
        Message 3 of 3 , Nov 1, 2002
        • 0 Attachment
          A few hints:-
           
          If you have attributes from different classes that have the same
          name and you need to use them as separate attributes in your
          new class then:
           
          class
              NOVELTY_PENCIL
           
          inherit
              PENCIL
                  rename
                      a as pencil_a
                  end
           
              BALLPOINT_PEN
                  rename
                      a as ballpoint_pen_a
                  end
           
          end -- class NOVELTY_PENCIL
           
          If you have attributes from different classes that have the same
          name but you want to use an attribute from only one of the classes
          then:
           
          class
              NOVELTY_PENCIL
           
          inherit
              PENCIL
           
              BALLPOINT_PEN
                  select
                      a
                  end
           
          end -- class NOVELTY_PENCIL
           
          If you have attributes from different classes that have the same
          name but you want to replace them with a newly defined
          attribute then:
           
          class
              NOVELTY_PENCIL
           
          inherit
              PENCIL
                  undefine
                      a
                  end
           
              BALLPOINT_PEN
                  undefine
                      a
                  end
           
          feature -- Access
           
              a: INTEGER
                      -- This assumes that `a' from each class returned an INTEGER.
           
          end -- class NOVELTY_PENCIL
           
          There are lots of nuances to this sort of thing but I think that
          these represent the most common cases and should get
          you started.
           
          Regards
          Chris Saunders
          chris.saunders.@...
           
          ----- Original Message -----
          From: Steve
          Sent: Friday, November 01, 2002 9:39 PM
          Subject: [eiffel-nice-library] C'mon guys, sort this out !

          Hi,

          I am new to Eiffel (started 3 weeks ago) and I have a multiple
          inheritance problem which I am not quite sure how to implement in
          Eiffel. Can any of you guys please help out?

          Basically, I have the following classes:
          (Please don't be put-off, it's very easy stuff! – nothing
          complex)





          deferred class WRITING_INSTRUMENT

          feature
          color : STRING
          price : INTEGER

          setPrice(aPrice : INTEGER) is
          do
          price := aPrice
          end -- setPrice

          setColor(aColor : STRING) is
          do
          color := clone(aColor)
          end -- setColor

          stats is -- display a writing instrument
          do
          io.putstring("Color : ")
          io.putstring(color); io.new_line
          io.putstring("Price : ")
          io.putint(price); io.new_line
          end -- stats

          getdata is
          do
          io.putstring("Enter color : ")
          io.readword
          setColor(io.laststring)
          io.putstring("Enter price : $")
          io.readint
          setPrice(io.lastint)
          end -- getdata

          end -- WRITING_INSTRUMENT






          class PENCIL

          inherit
          WRITING_INSTRUMENT
          redefine
          stats,
          getdata
          end

          feature
          eraserSize : INTEGER
          leadthickness : INTEGER

          setEraserSize(size : INTEGER) is
          do
          eraserSize := size
          end -- setEraserSize

          setLeadThickness(thickness : INTEGER) is
          do
          leadThickness := thickness
          end -- setLeadThickness

          stats is
          do
          precursor -- calls stats from WRITING_INSTRUMENT

          io.putstring("Eraser size : ")
          io.putint(eraserSize); io.new_line
          io.putstring("Lead thickness : ")
          io.putint(leadThickness); io.new_line
          end -- stats

          getdata is
          do
          precursor -- calls getdata from WRITING_INSTRUMENT

          io.putstring("Enter eraser size : ")
          io.readint
          setEraserSize(io.lastint)
          io.putstring("Enter lead thickness : ")
          io.readint
          setLeadThickness(io.lastint)
          end -- getdata

          end -- PENCIL






          deferred class PEN

          inherit
          WRITING_INSTRUMENT
          redefine
          stats,
          getdata
          end

          feature
          inkColor : STRING
          pointThickness : INTEGER

          setInkColor(aColor : STRING) is
          do
          inkColor := clone(aColor)
          end -- setInkColor

          setPointThickness(thickness : INTEGER) is
          do
          pointThickness := thickness
          end -- setPointThickness

          stats is
          do
          precursor -- calls stats from WRITING_INSTRUMENT

          io.putstring("Ink color : ")
          io.putstring(inkColor); io.new_line
          io.putstring("Point thickness : ")
          io.putint(pointThickness); io.new_line
          end -- stats

          getdata is
          do
          precursor -- calls getdata from WRITING_INSTRUMENT

          io.putstring("Enter ink color : ")
          io.readword
          setInkColor(io.laststring)
          io.putstring("Enter point thickness : ")
          io.readint
          setPointThickness(io.lastint)
          end -- getdata

          end -- PEN






          class
          BALLPOINT_PEN

          inherit
          PEN
          redefine
          stats,
          getdata
          end

          feature
          ballType : STRING

          setBallType(ball : STRING) is
          do
          ballType := clone(ball)
          end -- setBallType

          stats is
          do
          precursor -- calls stats from PEN

          io.putstring("Ball type : ")
          io.putstring(ballType); io.new_line
          end -- stats

          getdata is
          do
          precursor -- calls getdata from PEN

          io.putstring("Enter ball type : ")
          io.readword
          setBallType(io.laststring)
          end -- getdata

          end -- class BALLPOINT_PEN




          With the above classes I want to create a new class called
          NOVELTY_PENCIL. A novelty pencil not only functions as a pencil but
          also has a ballpoint pen at the other end. It does of course have a
          single 'color' and 'price', but it also has the functionality of
          pencils and ballpoint pens. Thus, NOVELTY_PENCIL inherits from PENCIL
          and BALLPOINT_PEN.

          Has anyone got any ideas how I can make this work without clashing
          with the attributes from the inherited hierarchy? Thank you for your
          help.

          Thank you.



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

          http://www.eiffel-nice.org/

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


          Your use of Yahoo! Groups is subject to the Yahoo! Terms of Service.
        Your message has been successfully submitted and would be delivered to recipients shortly.