Inheriting functions and adding to them

Hello,

How would I go about inheriting a function and changing it?
Here is what I have so far..

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49

...Code here...

class Item {

  protected:
  
  ...a bunch of variables...
  
  public:
    
  ...a bunch of functions here...
  
  //Magic
  void Draw ();
  
};

void Item::Draw () {
  
  ...operations...

}

class File: public Item {

  public:

  void Draw ();

};

void File::Draw () {

  Item::Draw();

  ...operations to add to Item::Draw...

}

Item *items[32000];
items[0] = new File();

...More code here...

items[0]->Draw();

...Rest of program...


What I get from the above code, is that the Item::Draw function works.. but it doesn't add the File stuff..

Thanks!

P.S.

If you want all of the code I can send a link. (too much to post here :S)
Last edited on
You need to make the Draw function in the Item class virtual.
http://www.cplusplus.com/forum/general/44630/#msg241726
closed account (zwA4jE8b)
Maybe I'm just inexperienced but... you create and array of Item pointers but you assign a 'File' object to be pointed to.
seems like that would be like writing char *ch = new int;

should it not be
1
2
File *items[32000];
items[0] = new File();
@L B

thanks

@ CreativeMFS

no, because there are more classes than just "File" that I also want to group in that array
@CreativeMFS: You need to read up on polymorphism and inheritance ;)
http://www.cplusplus.com/doc/tutorial/polymorphism/
Topic archived. No new replies allowed.