Classes and interfaces

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Webster

    Classes and interfaces

    Hello,

    I want to create a Shape class and some subclasses such as Rectangle, Circle
    etc. However for the Shape Class, I want to have a list of points (using a
    vector) and also I want it so that the Shape Classes forces the subclasses
    to implement a Draw() function. I believe what I want is called an
    interface right?? If so, how do I go about creating the Shape Class to force
    the subclasses to implement the Draw() function??

    Thanks for any help!

    --
    Regards,
    Webster


  • Victor Bazarov

    #2
    Re: Classes and interfaces

    "Webster" <noone@nowhere. net> wrote...[color=blue]
    > I want to create a Shape class and some subclasses such as Rectangle,[/color]
    Circle[color=blue]
    > etc. However for the Shape Class, I want to have a list of points (using a
    > vector) and also I want it so that the Shape Classes forces the subclasses
    > to implement a Draw() function. I believe what I want is called an
    > interface right??[/color]

    Interface is a mechanism for communication between two parties. Any
    function is an interface between the caller and the called (if those are
    objects). So, yes, member functions fall into the "interface" category.
    Whether it's something the base class _forces_ the derived classes to
    implement or not does not matter.
    [color=blue]
    > If so, how do I go about creating the Shape Class to force
    > the subclasses to implement the Draw() function??[/color]

    It has to be pure virtual.

    V


    Comment

    • Jeff Schwab

      #3
      Re: Classes and interfaces

      Webster wrote:[color=blue]
      > Hello,
      >
      > I want to create a Shape class and some subclasses such as Rectangle, Circle
      > etc. However for the Shape Class, I want to have a list of points (using a
      > vector) and also I want it so that the Shape Classes forces the subclasses
      > to implement a Draw() function. I believe what I want is called an
      > interface right??[/color]

      In Java. In C++, it's called an "abstract class."
      [color=blue]
      > If so, how do I go about creating the Shape Class to force
      > the subclasses to implement the Draw() function??[/color]

      struct Shape
      {
      virtual void draw( ) =0;
      };

      Comment

      • Jumbo

        #4
        Re: Classes and interfaces


        "Webster" <noone@nowhere. net> wrote in message
        news:6qxQb.1300 15$7JB1.70873@n ews04.bloor.is. net.cable.roger s.com...[color=blue]
        > Hello,
        >
        > I want to create a Shape class and some subclasses such as Rectangle,[/color]
        Circle[color=blue]
        > etc. However for the Shape Class, I want to have a list of points (using a
        > vector) and also I want it so that the Shape Classes forces the subclasses
        > to implement a Draw() function. I believe what I want is called an
        > interface right?? If so, how do I go about creating the Shape Class to[/color]
        force[color=blue]
        > the subclasses to implement the Draw() function??
        >
        > Thanks for any help!
        >
        > --
        > Regards,
        > Webster
        >
        >[/color]
        An abstract or pure virtual base class is the same thing AFAIK, and what
        this means is a class that cannot be instantiated i.e: it is designed in
        such a way that is is only meant to be derived from. These are called
        Interfaces in COM, C# etc (dunno about Java).
        Note: classes of this type are also know as ADT's ( Abstract data types).

        However what you seem to require is a virtual method, which is normally
        closely related with ADT's ( Abstract data types) as an ADT which exposes
        any methods will normall use vitual methods.
        What virtual means is tell the compiler that I intend to overide this
        function in a derived class i.e:
        class Class1{
        public:
        virtual void Draw(){std::cou t << "Class1 draw()"<< std::endl;}
        };

        That is an example of a virtual method.
        Next if you create a derived class an incorporate a method with the same
        signature like so:

        class Class2:public Class1{
        public:
        void Draw(){std::cou t << "Class2 draw()"<< std::endl;}
        };


        When you create an instance of the derived class the compiler knows to call
        the correct method i.e:
        int main(){
        Class1 obj1;
        Class2 obj2;
        obj1.Draw(); //outputs Class1 Draw()
        obj2.Draw(); //outputs Class2 Draw()

        return 0;
        }

        If a function is virtual then it will percolate up to a derived class if the
        derived class contains a method of the same signature.
        I'm not sure if I have explained this very well but I hope it helps.

        You can also create instances of the base class, you don't need to make it
        an ADT.
        Additionally if you do want to make a base class an ADT then you put '=0'
        after one of the methods. The following code would make Class1 pure virtual
        and therefore you would be intending that it is not directly initialised and
        only to be used as an interface. (This is what lies at the heart of COM and
        ATL(Abstract template library) ):

        class Class1{
        public:
        virtual void Draw() =0 {std::cout << "Class1 draw()"<< std::endl;}
        };

        You cannot create an instance of Class1 now but Class2 can still derive from
        it and the virtual functions will still percolate up to Class2. Be sure to
        include virtual destructors etc.

        However having said that you can declare a pointer to an ADT. It all gets a
        bit complicated from here so I'll leave it at that.



        Comment

        • Daniel T.

          #5
          Re: Classes and interfaces

          "Webster" <noone@nowhere. net> wrote:
          [color=blue]
          > I want to create a Shape class and some subclasses such as Rectangle, Circle
          > etc. However for the Shape Class, I want to have a list of points (using a
          > vector) and also I want it so that the Shape Classes forces the subclasses
          > to implement a Draw() function. I believe what I want is called an
          > interface right?? If so, how do I go about creating the Shape Class to force
          > the subclasses to implement the Draw() function??
          >
          > Thanks for any help![/color]

          class Shape {
          public:
          virtual ~Shape() { }
          virtual std::vector<poi nt> getPoints() const = 0;
          virtual void draw() = 0;
          };

          I have to ask, what are shapes supposed to draw on? Shouldn't that be a
          parameter of the draw member-function?

          Comment

          • Webster

            #6
            Re: Classes and interfaces

            "Daniel T." <postmaster@eat hlink.net> wrote in message
            news:postmaster-09542C.15391424 012004@news01.e ast.earthlink.n et...[color=blue]
            > "Webster" <noone@nowhere. net> wrote:[/color]
            [color=blue]
            > class Shape {
            > public:
            > virtual ~Shape() { }
            > virtual std::vector<poi nt> getPoints() const = 0;
            > virtual void draw() = 0;
            > };[/color]

            Cool.. thanks for the exact class!!
            [color=blue]
            > I have to ask, what are shapes supposed to draw on? Shouldn't that be a
            > parameter of the draw member-function?[/color]

            The draw function will be called from another class. I am using FLTK and
            OpenGL so there is a panel that calls the Draw() function which just sends
            the points.

            --
            Regards,
            Webster


            Comment

            • Cy Edmunds

              #7
              Re: Classes and interfaces

              "Webster" <noone@nowhere. net> wrote in message
              news:_3CQb.1057 81$lGr.22191@tw ister01.bloor.i s.net.cable.rog ers.com...[color=blue]
              > "Daniel T." <postmaster@eat hlink.net> wrote in message
              > news:postmaster-09542C.15391424 012004@news01.e ast.earthlink.n et...[color=green]
              > > "Webster" <noone@nowhere. net> wrote:[/color]
              >[color=green]
              > > class Shape {
              > > public:
              > > virtual ~Shape() { }
              > > virtual std::vector<poi nt> getPoints() const = 0;
              > > virtual void draw() = 0;
              > > };[/color]
              >
              > Cool.. thanks for the exact class!!
              >[color=green]
              > > I have to ask, what are shapes supposed to draw on? Shouldn't that be a
              > > parameter of the draw member-function?[/color]
              >
              > The draw function will be called from another class. I am using FLTK and
              > OpenGL so there is a panel that calls the Draw() function which just sends
              > the points.
              >
              > --
              > Regards,
              > Webster
              >
              >[/color]

              Well then let me suggest an alternative: use a standalone function to draw
              the points:

              class Shape {
              // same as before except no draw() function
              };

              void draw(const Shape &);

              You are considering a shape to be a bunch of points. Fair enough, but what
              does that have to do with drawing? Absolutely nothing. Your Shape family of
              classes might prove useful in other applications if you don't package the
              drawing stuff with them.

              --
              Cy



              Comment

              • Webster

                #8
                Re: Classes and interfaces

                "Cy Edmunds" <cedmunds@spaml ess.rochester.r r.com> wrote in message
                news:MDHQb.7704 7$Su5.26843@twi ster.nyroc.rr.c om...
                [color=blue][color=green][color=darkred]
                > > > I have to ask, what are shapes supposed to draw on? Shouldn't that be[/color][/color][/color]
                a[color=blue][color=green][color=darkred]
                > > > parameter of the draw member-function?[/color][/color][/color]
                [color=blue][color=green]
                > > The draw function will be called from another class. I am using FLTK and
                > > OpenGL so there is a panel that calls the Draw() function which just[/color][/color]
                sends[color=blue][color=green]
                > > the points.[/color][/color]
                [color=blue]
                > Well then let me suggest an alternative: use a standalone function to draw
                > the points:
                >
                > class Shape {
                > // same as before except no draw() function
                > };
                >
                > void draw(const Shape &);
                >
                > You are considering a shape to be a bunch of points. Fair enough, but what
                > does that have to do with drawing? Absolutely nothing. Your Shape family[/color]
                of[color=blue]
                > classes might prove useful in other applications if you don't package the
                > drawing stuff with them.[/color]

                You bring up a very good and valid point... but how can I draw the shape
                unless I know what to do with the points?? For instance, if I have a Line
                specified by 2 Points, I just draw a line between them, but a Rectangle
                specified by two Points, I have to derive the extra 2 Points. That is why I
                wanted a Draw() function for each Shape, so that I can know how to draw
                them, or if any special considerations need to be taken into account.

                --
                Regards,
                Webster


                Comment

                • Jeff Schwab

                  #9
                  Re: Classes and interfaces

                  Webster wrote:[color=blue]
                  > "Cy Edmunds" <cedmunds@spaml ess.rochester.r r.com> wrote in message
                  > news:MDHQb.7704 7$Su5.26843@twi ster.nyroc.rr.c om...
                  >
                  >[color=green][color=darkred]
                  >>>>I have to ask, what are shapes supposed to draw on? Shouldn't that be[/color][/color]
                  >
                  > a
                  >[color=green][color=darkred]
                  >>>>parameter of the draw member-function?[/color][/color]
                  >
                  >[color=green][color=darkred]
                  >>>The draw function will be called from another class. I am using FLTK and
                  >>>OpenGL so there is a panel that calls the Draw() function which just[/color][/color]
                  >
                  > sends
                  >[color=green][color=darkred]
                  >>>the points.[/color][/color]
                  >
                  >[color=green]
                  >>Well then let me suggest an alternative: use a standalone function to draw
                  >>the points:
                  >>
                  >>class Shape {
                  >>// same as before except no draw() function
                  >>};
                  >>
                  >>void draw(const Shape &);
                  >>
                  >>You are considering a shape to be a bunch of points. Fair enough, but what
                  >>does that have to do with drawing? Absolutely nothing. Your Shape family[/color]
                  >
                  > of
                  >[color=green]
                  >>classes might prove useful in other applications if you don't package the
                  >>drawing stuff with them.[/color]
                  >
                  >
                  > You bring up a very good and valid point... but how can I draw the shape
                  > unless I know what to do with the points?? For instance, if I have a Line
                  > specified by 2 Points, I just draw a line between them, but a Rectangle
                  > specified by two Points, I have to derive the extra 2 Points. That is why I
                  > wanted a Draw() function for each Shape, so that I can know how to draw
                  > them, or if any special considerations need to be taken into account.[/color]

                  If you're writing a very small program, putting the shapes in a
                  hierarchy with a "draw" method is probably okay. For larger projects,
                  it may be worth creating a separate "Drawer" hierarchy, and moving the
                  polymorphism of Shapes to compile-time.


                  #include <iostream>

                  namespace Shapes
                  {
                  struct Rectangle { };
                  struct Triangle { };
                  }

                  namespace Drawing
                  {
                  template< typename T >
                  void draw( T const& t )
                  {
                  std::cout << "Don't know how to draw typeid \""
                  << typeid( t ).name( ) << "\". :(\n";
                  }

                  template< >
                  void draw( Shapes::Rectang le const& rectangle )
                  {
                  std::cout << "I drew a rectangle.\n";
                  }

                  template< >
                  void draw( Shapes::Triangl e const& triangle )
                  {
                  std::cout << "I drew a triangle.\n";
                  }

                  struct Abstract_drawer
                  {
                  virtual void draw_shape( ) =0;
                  };

                  template< typename Shape >
                  struct Drawer: Abstract_drawer
                  {
                  void draw_shape( )
                  {
                  draw( shape );
                  }

                  Shape shape;
                  };
                  }

                  #include <algorithm>
                  #include <vector>

                  int main( )
                  {
                  using namespace Shapes;
                  using namespace Drawing;

                  Drawer< Rectangle > rectangle_drawe r;
                  Drawer< Triangle > triangle_drawer ;
                  Drawer< int > int_drawer;

                  std::vector< Abstract_drawer * > drawers;

                  drawers.push_ba ck( &rectangle_draw er );
                  drawers.push_ba ck( &triangle_drawe r );
                  drawers.push_ba ck( &int_drawer );

                  std::for_each( drawers.begin( ),
                  drawers.end( ),
                  std::mem_fun( &Abstract_drawe r::draw_shape ) );
                  }

                  Comment

                  • Frank Puck

                    #10
                    Re: Classes and interfaces


                    "Jeff Schwab" <jeffplus@comca st.net> wrote in message
                    news:ioKdnVdcQs pqO4_dRVn-gQ@comcast.com. ..
                    [color=blue]
                    > struct Shape
                    > {
                    > virtual void draw( ) =0;
                    > };[/color]


                    make this

                    virtual void draw() const = 0;

                    as by drawing itself the object should not change.
                    Usually you also have a device/window/printer to draw on,
                    which needs to be passed to the method.

                    virtual void draw(DEVICE *) const = 0;

                    In windows this would be a HDC (called differently on other systems).


                    Comment

                    Working...