473,395 Members | 1,530 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

simple inheritance question

This is part of my first ever largish project, a tile based game. I
havent got a lot of experience with c++ so be nice ;)

this is all within Tile.h, a lot has been taken out or simplified for
the sake of...er simplicity lol.

class Tile{
private:
int locationX;
int locationY;
public:
Tile(int inLocX, int inLocY){
setLocX(inLocX);
setLocY(inLocY); }
void setLocX(int inLocX){ locationX = inLocX; }
void setLocY(int inLocY){ locationY = inLocY; }
int getLocX(){ return locationX; }
int getLocY(){ return locationY; }
}//end of Tile class

class FloorTile : public Tile{
private:
int roomID;
public;
FloorTile(int inLocX, int inLocY, int inRoomID):Tile(inLocX, inLocY)
{
setRoomID(roomID); }
void setRoomID(int inRoomID){ roomID = inRoomID; }
int getRoomID(){ return roomID; }
}//end of FloorTile class

the idea is that i have a 2d array called map which holds tiles of
various kinds inlcuding wall, void and as seen above, floor. All of
that works fine, but when i look up, for example the tile at position
10,10 and i *know* that it is a floor tile, calling setRoomID() doenst
work. Origional code looks somthing like this:
map[10][10].setRoomID(15);
ive been shotgun programming it all afternoon, the best idea ive had
is pretty ugly, but still wont work because FloorTile is non-scalar
and cant be cast like thus:
FloorTile tempTile = map[10][10];
tempTile.setRoomID(15);
I guess what i need is some way to get it recognising the difference
between the types of tiles, once theyre pulled out of the array,
thereby opening the possibility of accessing the methods of the
subclass, but i have no idea...

sorry for noobing it up, im pretty sure this is a stupid thing to get
stuck on :P

-Numeron
Nov 27 '07 #1
4 1366
Numeron wrote:
This is part of my first ever largish project, a tile based game. I
havent got a lot of experience with c++ so be nice ;)

this is all within Tile.h, a lot has been taken out or simplified for
the sake of...er simplicity lol.

class Tile{
private:
int locationX;
int locationY;
public:
Tile(int inLocX, int inLocY){
setLocX(inLocX);
setLocY(inLocY); }
void setLocX(int inLocX){ locationX = inLocX; }
void setLocY(int inLocY){ locationY = inLocY; }
int getLocX(){ return locationX; }
int getLocY(){ return locationY; }
}//end of Tile class

class FloorTile : public Tile{
private:
int roomID;
public;
FloorTile(int inLocX, int inLocY, int inRoomID):Tile(inLocX, inLocY)
{
setRoomID(roomID); }
void setRoomID(int inRoomID){ roomID = inRoomID; }
int getRoomID(){ return roomID; }
}//end of FloorTile class

the idea is that i have a 2d array called map which holds tiles of
various kinds inlcuding wall, void and as seen above, floor. All of
that works fine, but when i look up, for example the tile at position
10,10 and i *know* that it is a floor tile, calling setRoomID() doenst
work.
How can you know it? I'm pretty curious to know how you declare your 2d
vector. If it is a Tile[][], all your tiles are basic tiles. The
(design) problem anyway is not so trivial, even though it's quite common
and there is a choice of solutions depending on the particular situation.

Regards,

Zeppe

Nov 27 '07 #2
How can you know it? I'm pretty curious to know how you declare your 2d
vector. If it is a Tile[][], all your tiles are basic tiles. The
(design) problem anyway is not so trivial, even though it's quite common
and there is a choice of solutions depending on the particular situation.
declaring looks something like this:

Tile map[78][39];

void InitializeMap(){
//Iterates through the map filling it with floor tiles
int x = 0; int y = 0;
while ((x != 78) && (y != 39)){
map[x][y] = new FloorTile(x,y,0);
x++;
if (x == 78){ y++; x = 0; }
}
}

once the initialise map method is called they're *all* floor tiles, so
any tile pulled from the map array must also be a floor tile..

-Numeron
Nov 27 '07 #3
On Tue, 27 Nov 2007 07:48:27 -0800, Numeron wrote:
[FloorTile is a subclass of Tile - introduces new method]
>How can you know it? I'm pretty curious to know how you declare your 2d
vector. If it is a Tile[][], all your tiles are basic tiles. The
(design) problem anyway is not so trivial, even though it's quite
common and there is a choice of solutions depending on the particular
situation.

declaring looks something like this:

Tile map[78][39];

void InitializeMap(){
//Iterates through the map filling it with floor tiles int x = 0;
int y = 0;
while ((x != 78) && (y != 39)){
map[x][y] = new FloorTile(x,y,0);
x++;
if (x == 78){ y++; x = 0; }
}
}

once the initialise map method is called they're *all* floor tiles, so
any tile pulled from the map array must also be a floor tile..
First of all - your code doesn't compile - the line
map[x][y] = new FloorTile(x, y, 0);
The left side of assignment has type Tile, while the right side is of
type FloorTile*. Maybe your array has type
Tile *map[78][39];

Second - if the whole array contains objects of FloorTile, why bother
with declaring it as containing objects of class Tile?
--
Tadeusz B. Kopec (tk****@NOSPAMPLEASElife.pl)
"A dirty mind is a joy forever."
-- Randy Kunkee
Nov 27 '07 #4
Zeppe wrote:
>Tile* map[78][39];
for(...)
for(...)
map[i][j] = new [whatever you want]
and then, when you want to access the FloorTile members, you can do a
static_cast<FloorTile*>(map[i][j])->floorTileMethod();
thanks for this it will be immesurably helpful. i understand how
pointers work, but in practice its a bit tricky to get right, but hey,
i guess i need to learn sometime ;)

Tadeusz B. Kopec wrote:
Second - if the whole array contains objects of FloorTile, why bother
with declaring it as containing objects of class Tile?
basically thats jsut the first part of the level generating
algorithm.. fills with floor, puts walls in tiles to give rectangled
rooms, adds some other stuff etc. so that the whole thing turns out to
look something like an old 'roguelike' game.

-Numeron
Nov 28 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: KK | last post by:
Windows Forms Inheritance, Incomplete? I was playing around with Windows Forms and found out this Forms Inheritance feature. The moment I saw that, I felt this can be used effectively if the...
2
by: KK | last post by:
** Posting it here cause after couple of days no body responded.** I was playing around with Windows Forms and found out this Forms Inheritance feature. The moment I saw that, I felt this can...
6
by: VR | last post by:
Hi, I read about Master Pages in ASP.Net 2.0 and after implementing some WinForms Visual Inheritance I tryed it with WebForms (let's say .aspx pages, my MasterPage does not have a form tag itself...
12
by: Michael S | last post by:
Why do people spend so much time writing complex generic types? for fun? to learn? for use? I think of generics like I do about operator overloading. Great to have as a language-feature, as...
1
by: relient | last post by:
Hi, I just started the chapter on "Inheritance" and I'm a bit confused so I have three questions: first, here's the code: using System; using System.Collections.Generic; using System.Text; ...
6
by: relient | last post by:
Hi, I have three classes: Animal, Dog and Cat. Dog inherits from Animal and Cat inherits from Dog. My question is: is Cat now also a Animal even though it didn't inherit directly from Animal?
3
by: RSH | last post by:
I have a simple question regarding inheritance in a web form. I have a DropDownList in an aspx form. It is called DropDownList1 I have a class that will be overriding the render event so I...
1
by: Oskar Bennet | last post by:
Hi everybody, I am supposed to draw a simple UML diagram for a very small project that consists of less than a dozen classes. I have never been working with UML before, I have read some tutorials...
8
by: Devon Null | last post by:
I was wondering if there is a container (i.e. a vector) that can hold data structures of differing sizes. I was thinking of something along the lines of a container of classes. Think of a backpack...
3
by: bg_ie | last post by:
Hi, I currently have a class called DefinedTest which relates to a set of tests I perform. But there are two Test states, those that are defined and those that are not. I'd therefore like to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.