Inheritance with pointers:Overloading?

This is a discussion on Inheritance with pointers:Overloading? within the C++ Programming forums, part of the General Programming Boards category; i derived a class from a linklist class. the problem is i can't access the next or previous pointers.type mismatch.do ...

  1. #1
    Registered User
    Join Date
    Apr 2004
    Posts
    2

    Unhappy Inheritance with pointers:Overloading?

    i derived a class from a linklist class. the problem is i can't access the next or previous pointers.type mismatch.do anybody have an idea how to solve it? should i overload the base class pointers, and how should i go about it?
    the program follows.i tried to make it as small as possible so please bear with me!!
    Code:
    #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::LLClass()
      {};
    
      LLClass::~LLClass()
      {}
    
    void LLClass::setnext(class LLClass *temp)
      {
       next=temp;
      }//setnext
    
      void LLClass::setprev(class LLClass *temp)
      {
       prev=temp;
      }//setprev
    
      class LLClass *LLClass::getnext()
      {
       return next;
      }//getnext
    
      class LLClass *LLClass::getprev()
      {
       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::setvalues(int xx, int yy)
     {
       x=xx;
       y=yy;
     }//setvalues
    
     void Point::setcolor(int chcolor)
    {
     color=chcolor;
    }//setcolor
    
    void Point::setdir(int 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:public 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::Rectangle()
      {}//cnstrctr
    
      Rectangle::~Rectangle()
      {}
    
      class LLClass *Rectangle::getnext()
      {
       return next;
      }//getnext
    
      class LLClass *Rectangle::getprev()
      {
       return prev;
      }//getprev
    
    int main(int argc,char *argv[])
    {
       class Rectangle *first,*last,*curr, *newptr, *temp;
       class LLClass *temp;
       int rct,choice;
    
    
       //auto detection
       int gdriver = 0, gmode, errorcode;
    
       //initialize graphics
       initgraph(&gdriver, &gmode, "c:\\tc\\bgi\\");
    
    
       //result of initialization
       errorcode = graphresult();
    
       if (errorcode != grOk)
       {
          printf("Graphics error: %s\n", grapherrormsg(errorcode));
          printf("Press any key to halt:");
          getch();
          exit(1);
       }
       //get the number of objects
       rct=atoi(argv[1]);
    
    
       //create first box
       randomize();
       first=curr=newptr=new Rectangle;
       first->setvalues(20+rand()%600,20+rand()%430);
    
    
    
       for(int b=1;b<=rct-1;b++)
       {
        newptr=new Rectangle;
        newptr->setvalues(20+rand()%600,20+rand()%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,630,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()<<endl;
       }//fr
    
     getch();
     closegraph();
     argc=argc;
     choice=choice;
     last=last;
     return 0;
    }
    if some one can help via e-mail or thread please do.thx

  2. #2
    Its not rocket science vasanth's Avatar
    Join Date
    Jan 2002
    Posts
    1,683
    why not have methods to return the pointers you want.. and i dint got hrough your code.. how abt you post only the parts required removing all the unnecessary stuff.

  3. #3
    Registered User
    Join Date
    Apr 2004
    Posts
    2

    inheritance with pointers:Overloading?

    what methods?
    i don't know what methods there are!
    i could post a smaller program:#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);

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

    };
    LLClass::LLClass()
    {};

    LLClass::~LLClass()
    {}

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

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

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

    class LLClass *LLClass::getprev()
    {
    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();
    };

    //no problem with this implementation
    //class definition of rectangle
    class Rectangleublic LLClass,public Point
    {
    private:
    int b,t;



    public:
    Rectangle(); //constructor
    ~Rectangle(); //destructor

    //functions in class


    };
    Rectangle::Rectangle()
    {}//cnstrctr

    Rectangle::~Rectangle()
    {}



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



    //create first box
    randomize();
    first=curr=newptr=new Rectangle;
    first->setvalues(20+rand()%600,20+rand()%430);



    for(int b=1;b<=rct-1;b++)
    {
    newptr=new Rectangle;
    newptr->setvalues(20+rand()%600,20+rand()%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,630,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++)
    {
    cout<<curr->getx()<<" ";
    cout<<curr->gety()<<endl;
    //?????????????????????
    curr=curr->getnext();
    }//fr

    getch();
    return 0;
    }

  4. #4
    Registered User strontium90's Avatar
    Join Date
    Apr 2004
    Posts
    3
    Hi katie,

    reading your (rather messy) code bearing in mind the error reported by your compiler "type mismatch" I can mention the line
    Code:
    curr=curr->getnext() ;
    in
    Code:
    int main(int argc,char *argv[])
    as one source of trouble; assigning a base class pointer (LLClass*) to a derived class pointer (Rectangle*); there's no such implicit conversion. If you can afford a guarantee that a base class pointer already points to a derived class object, brute force, that is static type casting, may help. However, it is tasteless.

    This, and similar errors, are IMHO indicators of bad design.
    Ask yourself is it really necessary to have both public data and examinig/modifying member functions in class Point ? Is it correct to couple concepts such as "linked list", "rectangle" and "point" together in one component ?
    Pay attention to cleaner design.

    Regardless, try not to reinvent the wheel; use standard <list>.

    -strontium90
    Last edited by strontium90; 04-13-2004 at 02:17 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 06-08-2009, 03:03 PM
  2. Forms of Inheritance
    By kolucoms6 in forum C++ Programming
    Replies: 1
    Last Post: 03-11-2008, 03:09 PM
  3. Multiple Inheritance - Size of Classes?
    By Zeusbwr in forum C++ Programming
    Replies: 10
    Last Post: 11-26-2004, 08:04 AM
  4. inheritance and performance
    By kuhnmi in forum C++ Programming
    Replies: 5
    Last Post: 08-04-2004, 12:46 PM
  5. Inheritance vs Composition
    By Panopticon in forum C++ Programming
    Replies: 11
    Last Post: 01-20-2003, 03:41 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21