Inheritance with pointers:Overloading?

K

Katie

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.classic 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!!!
 
B

Buster

Katie said:
my one base class is a linklist class.

You have defined a class called "linklist". OK.
now i'd like to inherit it by
another class.

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

Now you've lost me. Why not? What pointers? How do you "use"
the derived class?
classic type mismatch.
What?

how do i link the derived class if i can't compare the derived
class's pointers to the linklist class's pointers?

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 `=='?
do i do it with overloading?

I shouldn't think so. Without knowing more about the
problem it's impossible to say for sure.
i tried that,but could get it to work.
Right.

my handbooks (all 4 of them) has no example on how to do overloading.

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.
can someone help?

I tried, really. I don't understand what you are trying to do.
Can you show us the program you are trying to compile?
e-mail if you want to.

Post here, read here.
please, i'm on a deadline!!!

Good luck with that.
 
K

Katie

Buster said:
You have defined a class called "linklist". OK.


You want to define another class, derived from "linklist". OK.


Now you've lost me. Why not? What pointers? How do you "use"
the derived class?


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 `=='?


I shouldn't think so. Without knowing more about the
problem it's impossible to say for sure.


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.


I tried, really. I don't understand what you are trying to do.
Can you show us the program you are trying to compile?


Post here, read here.


Good luck with that.

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::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;
}
thx a lot
 
B

Buster

Katie said:
here follows the whole program
any help would be appreciated

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::getnext (); }
virtual simple * getprev () { return LLClass::getprev (); }
};

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.
 
K

Katie

Buster said:
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.

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

Katie

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

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::getnext cannot be returned(*LLClass !=*simple)
virtual simple * getnext () { return LLClass::getnext (); }
virtual simple * getprev () { return LLClass::getprev (); }
};

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

Buster

Katie said:
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::getnext cannot be returned(*LLClass !=*simple)

You're right, of course. Sorry about that.
virtual simple * getnext () { return LLClass::getnext (); }
virtual simple * getprev () { return LLClass::getprev (); }
};

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

Shame. Have you looked inside the "list" header for inspiration?
There are also many, many books which provide code for linked lists.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,164
Messages
2,570,898
Members
47,439
Latest member
shasuze

Latest Threads

Top