Inheritance with pointers:Overloading?

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

    Inheritance with pointers:Overloading?

    my one base class is a linklist class.now i'd like to inherit it by
    another class.The problem is that when i use the derived class i can't
    access the base class's pointers.classi c type mismatch.how do i link
    the derived class if i can't compare the derived class's pointers to
    the linklist class's pointers?
    do i do it with overloading?i tried that,but could get it to work.my
    handbooks (all 4 of them) has no example on how to do overloading.can
    someone help?e-mail if you want to.please, i'm on a deadline!!!
  • Buster

    #2
    Re: Inheritance with pointers:Overlo ading?

    Katie wrote:
    [color=blue]
    > my one base class is a linklist class.[/color]

    You have defined a class called "linklist". OK.
    [color=blue]
    > now i'd like to inherit it by
    > another class.[/color]

    You want to define another class, derived from "linklist". OK.
    [color=blue]
    > The problem is that when i use the derived class i can't
    > access the base class's pointers.[/color]

    Now you've lost me. Why not? What pointers? How do you "use"
    the derived class?
    [color=blue]
    > classic type mismatch.[/color]

    What?
    [color=blue]
    > how do i link the derived class if i can't compare the derived
    > class's pointers to the linklist class's pointers?[/color]

    What does it mean to "link" a class?

    You _can_ compare a base class pointer and a derived class pointer
    for equality. Did you use two equals signs, like `=='?
    [color=blue]
    > do i do it with overloading?[/color]

    I shouldn't think so. Without knowing more about the
    problem it's impossible to say for sure.
    [color=blue]
    > i tried that,but could get it to work.[/color]

    Right.
    [color=blue]
    > my handbooks (all 4 of them) has no example on how to do overloading.[/color]

    Borrow or buy a better book. There are reviews on the ACCU site.
    More recent reviews are more useful because the way people use C++
    has evolved. "The C++ Programming Language", 3rd/Special edition,
    by Bjarne Stroustrup, covers the whole language nicely. I don't know
    that it's very suitable for a beginner.
    [color=blue]
    > can someone help?[/color]

    I tried, really. I don't understand what you are trying to do.
    Can you show us the program you are trying to compile?
    [color=blue]
    > e-mail if you want to.[/color]

    Post here, read here.
    [color=blue]
    > please, i'm on a deadline!!![/color]

    Good luck with that.

    --
    Regards,
    Buster.

    Comment

    • Buster

      #3
      Re: Inheritance with pointers:Overlo ading?

      Katie wrote:
      [color=blue]
      > my one base class is a linklist class.[/color]

      You have defined a class called "linklist". OK.
      [color=blue]
      > now i'd like to inherit it by
      > another class.[/color]

      You want to define another class, derived from "linklist". OK.
      [color=blue]
      > The problem is that when i use the derived class i can't
      > access the base class's pointers.[/color]

      Now you've lost me. Why not? What pointers? How do you "use"
      the derived class?
      [color=blue]
      > classic type mismatch.[/color]

      What?
      [color=blue]
      > how do i link the derived class if i can't compare the derived
      > class's pointers to the linklist class's pointers?[/color]

      What does it mean to "link" a class?

      You _can_ compare a base class pointer and a derived class pointer
      for equality. Did you use two equals signs, like `=='?
      [color=blue]
      > do i do it with overloading?[/color]

      I shouldn't think so. Without knowing more about the
      problem it's impossible to say for sure.
      [color=blue]
      > i tried that,but could get it to work.[/color]

      Right.
      [color=blue]
      > my handbooks (all 4 of them) has no example on how to do overloading.[/color]

      Borrow or buy a better book. There are reviews on the ACCU site.
      More recent reviews are more useful because the way people use C++
      has evolved. "The C++ Programming Language", 3rd/Special edition,
      by Bjarne Stroustrup, covers the whole language nicely. I don't know
      that it's very suitable for a beginner.
      [color=blue]
      > can someone help?[/color]

      I tried, really. I don't understand what you are trying to do.
      Can you show us the program you are trying to compile?
      [color=blue]
      > e-mail if you want to.[/color]

      Post here, read here.
      [color=blue]
      > please, i'm on a deadline!!![/color]

      Good luck with that.

      --
      Regards,
      Buster.

      Comment

      • Katie

        #4
        Re: Inheritance with pointers:Overlo ading?

        Buster <noone@nowhere. com> wrote in message news:<c56q76$uq 0$1@news6.svr.p ol.co.uk>...[color=blue]
        > Katie wrote:
        >[color=green]
        > > my one base class is a linklist class.[/color]
        >
        > You have defined a class called "linklist". OK.
        >[color=green]
        > > now i'd like to inherit it by
        > > another class.[/color]
        >
        > You want to define another class, derived from "linklist". OK.
        >[color=green]
        > > The problem is that when i use the derived class i can't
        > > access the base class's pointers.[/color]
        >
        > Now you've lost me. Why not? What pointers? How do you "use"
        > the derived class?
        >[color=green]
        > > classic type mismatch.[/color]
        >
        > What?
        >[color=green]
        > > how do i link the derived class if i can't compare the derived
        > > class's pointers to the linklist class's pointers?[/color]
        >
        > What does it mean to "link" a class?
        >
        > You _can_ compare a base class pointer and a derived class pointer
        > for equality. Did you use two equals signs, like `=='?
        >[color=green]
        > > do i do it with overloading?[/color]
        >
        > I shouldn't think so. Without knowing more about the
        > problem it's impossible to say for sure.
        >[color=green]
        > > i tried that,but could get it to work.[/color]
        >
        > Right.
        >[color=green]
        > > my handbooks (all 4 of them) has no example on how to do overloading.[/color]
        >
        > Borrow or buy a better book. There are reviews on the ACCU site.
        > More recent reviews are more useful because the way people use C++
        > has evolved. "The C++ Programming Language", 3rd/Special edition,
        > by Bjarne Stroustrup, covers the whole language nicely. I don't know
        > that it's very suitable for a beginner.
        >[color=green]
        > > can someone help?[/color]
        >
        > I tried, really. I don't understand what you are trying to do.
        > Can you show us the program you are trying to compile?
        >[color=green]
        > > e-mail if you want to.[/color]
        >
        > Post here, read here.
        >[color=green]
        > > please, i'm on a deadline!!![/color]
        >
        > Good luck with that.[/color]

        here follows the whole program
        any help would be appreciated

        #include <graphics.h>
        #include <stdlib.h>
        #include <stdio.h>
        #include <conio.h>
        #include <stdio.h>
        #include <iostream.h>
        #include <dos.h>

        //class definition of link list
        class LLClass
        {
        protected:
        class LLClass *next; //pointer to next position
        class LLClass *prev; //pointer to previous position



        public:
        LLClass();
        ~LLClass();
        void setnext(class LLClass *temp);
        void setprev(class LLClass *temp);

        virtual class LLClass *getnext();
        virtual class LLClass *getprev();

        };
        LLClass::LLClas s()
        {};

        LLClass::~LLCla ss()
        {}

        void LLClass::setnex t(class LLClass *temp)
        {
        next=temp;
        }//setnext

        void LLClass::setpre v(class LLClass *temp)
        {
        prev=temp;
        }//setprev

        class LLClass *LLClass::getne xt()
        {
        return next;
        }//getnext

        class LLClass *LLClass::getpr ev()
        {
        return prev;
        }//getprev

        //class definition for class point
        class Point
        {
        private:
        int x,y; //top left corner
        int color; //color of the box
        int direction; //direction the rectangle is going


        public:
        Point();
        ~Point();

        int xx,yy; //public coordinates
        int chcolor; //change in the color
        int dir; //the direction of the box

        void setvalues(int xx, int yy); //get values for x,y
        void setcolor(int chcolor); //change the color
        void setdir(int dir); //change the direction

        int getx();
        int gety();
        int getcolor();
        int getdir();

        int inccolor();
        int deccolor();
        int incx();
        int decx();
        int incy();
        int decy();
        };
        Point::Point()
        {
        x=y=0; //default box at 0
        direction=0; //default direction at 0
        color=2; //default color green
        }//cnstrctr
        Point::~Point()
        {}

        void Point::setvalue s(int xx, int yy)
        {
        x=xx;
        y=yy;
        }//setvalues

        void Point::setcolor (int chcolor)
        {
        color=chcolor;
        }//setcolor

        void Point::setdir(i nt dir)
        {
        direction=dir;
        }//setdir

        int Point::getx()
        {
        return x;
        }//getx

        int Point::gety()
        {
        return y;
        }//gety

        int Point::getcolor ()
        {
        return color;
        }//getcolor

        int Point::getdir()
        {
        return direction;
        }//getdir

        int Point::inccolor ()
        {
        color++;
        return color;
        }//inccolor

        int Point::deccolor ()
        {
        color--;
        return color;
        }//deccolor

        int Point::incy()
        {
        y++;
        return y;
        }//incy

        int Point::decy()
        {
        y--;
        return y;
        }//decy

        int Point::incx()
        {
        x++;
        return x;
        }//incx

        int Point::decx()
        {
        x--;
        return x;
        }//decx

        //class definition of rectangle
        class Rectangle:publi c LLClass,public Point
        {
        private:
        int b,t;


        public:
        Rectangle(); //constructor
        ~Rectangle(); //destructor
        class Rectangle *newptr,*first, *last,*curr;//pointers

        //functions in class
        void setrnext(class LLClass *temp);
        class LLClass *convnext(class LLClass *temp);
        class Rectangle *getrnext(); //next pointer


        class LLClass *getnext(); //next pointer
        class LLClass *getprev(); //prev pointer
        };
        Rectangle::Rect angle()
        {}//cnstrctr

        Rectangle::~Rec tangle()
        {}

        class LLClass *Rectangle::get next()
        {
        return next;
        }//getnext

        class LLClass *Rectangle::get prev()
        {
        return prev;
        }//getprev

        int main(int argc,char *argv[])
        {
        class Rectangle *first,*last,*c urr, *newptr, *temp;
        class LLClass *temp;
        int rct,choice;


        //auto detection
        int gdriver = 0, gmode, errorcode;

        //initialize graphics
        initgraph(&gdri ver, &gmode, "c:\\tc\\bgi\\" );


        //result of initialization
        errorcode = graphresult();

        if (errorcode != grOk)
        {
        printf("Graphic s error: %s\n", grapherrormsg(e rrorcode));
        printf("Press any key to halt:");
        getch();
        exit(1);
        }
        //get the number of objects
        rct=atoi(argv[1]);


        //create first box
        randomize();
        first=curr=newp tr=new Rectangle;
        first->setvalues(20+r and()%600,20+ra nd()%430);



        for(int b=1;b<=rct-1;b++)
        {
        newptr=new Rectangle;
        newptr->setvalues(20+r and()%600,20+ra nd()%430);


        //linking
        newptr->setprev(curr );
        curr->setnext(newptr );
        curr=newptr;
        }//end of new box (fr)
        newptr->setnext(first) ;
        last=newptr;
        first->setprev(newptr );

        //draw outerbox
        choice=0;
        rectangle(1,1,6 30,470);


        //problem starts here
        //the linklist was created and data was written to it but i can't access it
        //draw the small boxes
        curr=first;
        setcolor(15);
        cout<<endl;
        for (int j=1;j<=rct;j++)
        {
        temp=curr->getnext();
        curr=curr->getnext();
        cout<<newptr->getprev()<<" ";
        cout<<newptr->getnext()<<end l;
        }//fr

        getch();
        closegraph();
        argc=argc;
        choice=choice;
        last=last;
        return 0;
        }
        thx a lot

        Comment

        • Katie

          #5
          Re: Inheritance with pointers:Overlo ading?

          Buster <noone@nowhere. com> wrote in message news:<c56q76$uq 0$1@news6.svr.p ol.co.uk>...[color=blue]
          > Katie wrote:
          >[color=green]
          > > my one base class is a linklist class.[/color]
          >
          > You have defined a class called "linklist". OK.
          >[color=green]
          > > now i'd like to inherit it by
          > > another class.[/color]
          >
          > You want to define another class, derived from "linklist". OK.
          >[color=green]
          > > The problem is that when i use the derived class i can't
          > > access the base class's pointers.[/color]
          >
          > Now you've lost me. Why not? What pointers? How do you "use"
          > the derived class?
          >[color=green]
          > > classic type mismatch.[/color]
          >
          > What?
          >[color=green]
          > > how do i link the derived class if i can't compare the derived
          > > class's pointers to the linklist class's pointers?[/color]
          >
          > What does it mean to "link" a class?
          >
          > You _can_ compare a base class pointer and a derived class pointer
          > for equality. Did you use two equals signs, like `=='?
          >[color=green]
          > > do i do it with overloading?[/color]
          >
          > I shouldn't think so. Without knowing more about the
          > problem it's impossible to say for sure.
          >[color=green]
          > > i tried that,but could get it to work.[/color]
          >
          > Right.
          >[color=green]
          > > my handbooks (all 4 of them) has no example on how to do overloading.[/color]
          >
          > Borrow or buy a better book. There are reviews on the ACCU site.
          > More recent reviews are more useful because the way people use C++
          > has evolved. "The C++ Programming Language", 3rd/Special edition,
          > by Bjarne Stroustrup, covers the whole language nicely. I don't know
          > that it's very suitable for a beginner.
          >[color=green]
          > > can someone help?[/color]
          >
          > I tried, really. I don't understand what you are trying to do.
          > Can you show us the program you are trying to compile?
          >[color=green]
          > > e-mail if you want to.[/color]
          >
          > Post here, read here.
          >[color=green]
          > > please, i'm on a deadline!!![/color]
          >
          > Good luck with that.[/color]

          here follows the whole program
          any help would be appreciated

          #include <graphics.h>
          #include <stdlib.h>
          #include <stdio.h>
          #include <conio.h>
          #include <stdio.h>
          #include <iostream.h>
          #include <dos.h>

          //class definition of link list
          class LLClass
          {
          protected:
          class LLClass *next; //pointer to next position
          class LLClass *prev; //pointer to previous position



          public:
          LLClass();
          ~LLClass();
          void setnext(class LLClass *temp);
          void setprev(class LLClass *temp);

          virtual class LLClass *getnext();
          virtual class LLClass *getprev();

          };
          LLClass::LLClas s()
          {};

          LLClass::~LLCla ss()
          {}

          void LLClass::setnex t(class LLClass *temp)
          {
          next=temp;
          }//setnext

          void LLClass::setpre v(class LLClass *temp)
          {
          prev=temp;
          }//setprev

          class LLClass *LLClass::getne xt()
          {
          return next;
          }//getnext

          class LLClass *LLClass::getpr ev()
          {
          return prev;
          }//getprev

          //class definition for class point
          class Point
          {
          private:
          int x,y; //top left corner
          int color; //color of the box
          int direction; //direction the rectangle is going


          public:
          Point();
          ~Point();

          int xx,yy; //public coordinates
          int chcolor; //change in the color
          int dir; //the direction of the box

          void setvalues(int xx, int yy); //get values for x,y
          void setcolor(int chcolor); //change the color
          void setdir(int dir); //change the direction

          int getx();
          int gety();
          int getcolor();
          int getdir();

          int inccolor();
          int deccolor();
          int incx();
          int decx();
          int incy();
          int decy();
          };
          Point::Point()
          {
          x=y=0; //default box at 0
          direction=0; //default direction at 0
          color=2; //default color green
          }//cnstrctr
          Point::~Point()
          {}

          void Point::setvalue s(int xx, int yy)
          {
          x=xx;
          y=yy;
          }//setvalues

          void Point::setcolor (int chcolor)
          {
          color=chcolor;
          }//setcolor

          void Point::setdir(i nt dir)
          {
          direction=dir;
          }//setdir

          int Point::getx()
          {
          return x;
          }//getx

          int Point::gety()
          {
          return y;
          }//gety

          int Point::getcolor ()
          {
          return color;
          }//getcolor

          int Point::getdir()
          {
          return direction;
          }//getdir

          int Point::inccolor ()
          {
          color++;
          return color;
          }//inccolor

          int Point::deccolor ()
          {
          color--;
          return color;
          }//deccolor

          int Point::incy()
          {
          y++;
          return y;
          }//incy

          int Point::decy()
          {
          y--;
          return y;
          }//decy

          int Point::incx()
          {
          x++;
          return x;
          }//incx

          int Point::decx()
          {
          x--;
          return x;
          }//decx

          //class definition of rectangle
          class Rectangle:publi c LLClass,public Point
          {
          private:
          int b,t;


          public:
          Rectangle(); //constructor
          ~Rectangle(); //destructor
          class Rectangle *newptr,*first, *last,*curr;//pointers

          //functions in class
          void setrnext(class LLClass *temp);
          class LLClass *convnext(class LLClass *temp);
          class Rectangle *getrnext(); //next pointer


          class LLClass *getnext(); //next pointer
          class LLClass *getprev(); //prev pointer
          };
          Rectangle::Rect angle()
          {}//cnstrctr

          Rectangle::~Rec tangle()
          {}

          class LLClass *Rectangle::get next()
          {
          return next;
          }//getnext

          class LLClass *Rectangle::get prev()
          {
          return prev;
          }//getprev

          int main(int argc,char *argv[])
          {
          class Rectangle *first,*last,*c urr, *newptr, *temp;
          class LLClass *temp;
          int rct,choice;


          //auto detection
          int gdriver = 0, gmode, errorcode;

          //initialize graphics
          initgraph(&gdri ver, &gmode, "c:\\tc\\bgi\\" );


          //result of initialization
          errorcode = graphresult();

          if (errorcode != grOk)
          {
          printf("Graphic s error: %s\n", grapherrormsg(e rrorcode));
          printf("Press any key to halt:");
          getch();
          exit(1);
          }
          //get the number of objects
          rct=atoi(argv[1]);


          //create first box
          randomize();
          first=curr=newp tr=new Rectangle;
          first->setvalues(20+r and()%600,20+ra nd()%430);



          for(int b=1;b<=rct-1;b++)
          {
          newptr=new Rectangle;
          newptr->setvalues(20+r and()%600,20+ra nd()%430);


          //linking
          newptr->setprev(curr );
          curr->setnext(newptr );
          curr=newptr;
          }//end of new box (fr)
          newptr->setnext(first) ;
          last=newptr;
          first->setprev(newptr );

          //draw outerbox
          choice=0;
          rectangle(1,1,6 30,470);


          //problem starts here
          //the linklist was created and data was written to it but i can't access it
          //draw the small boxes
          curr=first;
          setcolor(15);
          cout<<endl;
          for (int j=1;j<=rct;j++)
          {
          temp=curr->getnext();
          curr=curr->getnext();
          cout<<newptr->getprev()<<" ";
          cout<<newptr->getnext()<<end l;
          }//fr

          getch();
          closegraph();
          argc=argc;
          choice=choice;
          last=last;
          return 0;
          }
          thx a lot

          Comment

          • Buster

            #6
            Re: Inheritance with pointers:Overlo ading?

            Katie wrote:
            [color=blue]
            > here follows the whole program
            > any help would be appreciated[/color]

            I suggest you test your LLClass class using a much simpler
            object first, say

            class simple : public LLClass
            {
            private:
            int data;
            public:
            void setdata (int d) { data = d; }
            int getdata () { return data; }

            virtual simple * getnext () { return LLClass::getnex t (); }
            virtual simple * getprev () { return LLClass::getpre v (); }
            };

            Note that standard C++, the topic of this newsgroup, has no facilities
            for graphics or even coloured text. However, it does have linked lists
            in the form of the std::list class template.

            --
            Regards,
            Buster

            Comment

            • Buster

              #7
              Re: Inheritance with pointers:Overlo ading?

              Katie wrote:
              [color=blue]
              > here follows the whole program
              > any help would be appreciated[/color]

              I suggest you test your LLClass class using a much simpler
              object first, say

              class simple : public LLClass
              {
              private:
              int data;
              public:
              void setdata (int d) { data = d; }
              int getdata () { return data; }

              virtual simple * getnext () { return LLClass::getnex t (); }
              virtual simple * getprev () { return LLClass::getpre v (); }
              };

              Note that standard C++, the topic of this newsgroup, has no facilities
              for graphics or even coloured text. However, it does have linked lists
              in the form of the std::list class template.

              --
              Regards,
              Buster

              Comment

              • Katie

                #8
                Re: Inheritance with pointers:Overlo ading?

                Buster <noone@nowhere. com> wrote in message news:<c599vl$7q 1$1@newsg2.svr. pol.co.uk>...[color=blue]
                > I suggest you test your LLClass class using a much simpler
                > object first, say
                >
                > Note that standard C++, the topic of this newsgroup, has no facilities
                > for graphics or even coloured text. However, it does have linked lists
                > in the form of the std::list class template.[/color]

                thx
                i'll try that
                i'll let u know if it works

                Comment

                • Katie

                  #9
                  Re: Inheritance with pointers:Overlo ading?

                  Buster <noone@nowhere. com> wrote in message news:<c599vl$7q 1$1@newsg2.svr. pol.co.uk>...[color=blue]
                  > I suggest you test your LLClass class using a much simpler
                  > object first, say
                  >
                  > Note that standard C++, the topic of this newsgroup, has no facilities
                  > for graphics or even coloured text. However, it does have linked lists
                  > in the form of the std::list class template.[/color]

                  thx
                  i'll try that
                  i'll let u know if it works

                  Comment

                  • Katie

                    #10
                    Re: Inheritance with pointers:Overlo ading?

                    > i'll try that[color=blue]
                    > i'll let u know if it works[/color]

                    class simple : public LLClass
                    {
                    private:
                    int data;
                    public:
                    void setdata (int d) { data = d; }
                    int getdata () { return data; }

                    //first it cannot override the virtual LLClass *getnext:
                    //LLClass::getnex t cannot be returned(*LLCla ss !=*simple)
                    virtual simple * getnext () { return LLClass::getnex t (); }
                    virtual simple * getprev () { return LLClass::getpre v (); }
                    };

                    //The assignment tells us to create our own link list template

                    --
                    didn’t solve it, but thanks for the effort

                    Comment

                    • Buster

                      #11
                      Re: Inheritance with pointers:Overlo ading?

                      Katie wrote:[color=blue][color=green]
                      >>i'll try that
                      >>i'll let u know if it works[/color]
                      >
                      > class simple : public LLClass
                      > {
                      > private:
                      > int data;
                      > public:
                      > void setdata (int d) { data = d; }
                      > int getdata () { return data; }
                      >
                      > //first it cannot override the virtual LLClass *getnext:
                      > //LLClass::getnex t cannot be returned(*LLCla ss !=*simple)[/color]

                      You're right, of course. Sorry about that.
                      [color=blue]
                      > virtual simple * getnext () { return LLClass::getnex t (); }
                      > virtual simple * getprev () { return LLClass::getpre v (); }
                      > };
                      >
                      > //The assignment tells us to create our own link list template[/color]

                      Shame. Have you looked inside the "list" header for inspiration?
                      There are also many, many books which provide code for linked lists.
                      [color=blue]
                      > didn’t solve it, but thanks for the effort[/color]

                      --
                      Regards,
                      Buster.

                      Comment

                      Working...