CArray of different classes derived from common base 
Author Message
 CArray of different classes derived from common base

Quote:

> I've got this problem here about dealing with derived classes and
> arrays. Let's say that my base class is CFruit. I have got a number of
> derived classes from CFruit such as CApple, CLemon and CBanana.

> I've got a doc/view app using MFC (VC5) and its associated template
> classes.

> I want to draw each fruit so CFruit has a Draw(CDC* pDC) member
> function in it that children classes will override to draw their own
> unique fruit. *pDC is passed from my draw function.

> Now I have my document and I have a member CArray of CFruit to
> represent all of my fruit. However, CFruit should be able to be
> anything. So, in essence, I want a CArray that can contain CApple's,
> CLemon's and CBanana's. This array needn't know what kind of fruit it
> has, only that it is CFruit.

> I like this idea in theory because it simplifies my program
> significantly. However, the implementation of the idea has stopped me
> dead in my tracks. I started up by initializing my array with a few
> different kinds of fruit such as CBanana, CApple, and CLemon. This was
> fine. Then, in my draw function, I called the Draw function for each
> element of the CArray. Instead of magically calling a specific fruit
> draw function, it called the CFruit draw function. How can I make the
> program call the intended function?

> Just for extra detail, CFruit has some basic properties such as weight
> and colour and the child classes have different properties such as
> bitterness (for CLemon) and bendiness (for CBanana). This means that
> these specific details for each fruit kind must be kept whilst in the
> CArray.

> Thankyou in advance to anyone who replies,
> Rodney Lorrimar

> Sent via Deja.com http://www.*-*-*.com/
> Before you buy.

You must declare the base class Draw function to be virtual.  That makes the
derived class Draw function override the base class function.

--
Scott McPhillips [VC++ MVP]



Mon, 27 May 2002 03:00:00 GMT  
 CArray of different classes derived from common base
Make CFruit have a virtual Draw function
then have each fruit override with its own Draw function
that ought to do it!
Larry
Quote:

> I've got this problem here about dealing with derived classes and
> arrays. Let's say that my base class is CFruit. I have got a number of
> derived classes from CFruit such as CApple, CLemon and CBanana.

> I've got a doc/view app using MFC (VC5) and its associated template
> classes.

> I want to draw each fruit so CFruit has a Draw(CDC* pDC) member
> function in it that children classes will override to draw their own
> unique fruit. *pDC is passed from my draw function.

> Now I have my document and I have a member CArray of CFruit to
> represent all of my fruit. However, CFruit should be able to be
> anything. So, in essence, I want a CArray that can contain CApple's,
> CLemon's and CBanana's. This array needn't know what kind of fruit it
> has, only that it is CFruit.

> I like this idea in theory because it simplifies my program
> significantly. However, the implementation of the idea has stopped me
> dead in my tracks. I started up by initializing my array with a few
> different kinds of fruit such as CBanana, CApple, and CLemon. This was
> fine. Then, in my draw function, I called the Draw function for each
> element of the CArray. Instead of magically calling a specific fruit
> draw function, it called the CFruit draw function. How can I make the
> program call the intended function?

> Just for extra detail, CFruit has some basic properties such as weight
> and colour and the child classes have different properties such as
> bitterness (for CLemon) and bendiness (for CBanana). This means that
> these specific details for each fruit kind must be kept whilst in the
> CArray.

> Thankyou in advance to anyone who replies,
> Rodney Lorrimar

> Sent via Deja.com http://www.deja.com/
> Before you buy.



Mon, 27 May 2002 03:00:00 GMT  
 CArray of different classes derived from common base
I've got this problem here about dealing with derived classes and
arrays. Let's say that my base class is CFruit. I have got a number of
derived classes from CFruit such as CApple, CLemon and CBanana.

I've got a doc/view app using MFC (VC5) and its associated template
classes.

I want to draw each fruit so CFruit has a Draw(CDC* pDC) member
function in it that children classes will override to draw their own
unique fruit. *pDC is passed from my draw function.

Now I have my document and I have a member CArray of CFruit to
represent all of my fruit. However, CFruit should be able to be
anything. So, in essence, I want a CArray that can contain CApple's,
CLemon's and CBanana's. This array needn't know what kind of fruit it
has, only that it is CFruit.

I like this idea in theory because it simplifies my program
significantly. However, the implementation of the idea has stopped me
dead in my tracks. I started up by initializing my array with a few
different kinds of fruit such as CBanana, CApple, and CLemon. This was
fine. Then, in my draw function, I called the Draw function for each
element of the CArray. Instead of magically calling a specific fruit
draw function, it called the CFruit draw function. How can I make the
program call the intended function?

Just for extra detail, CFruit has some basic properties such as weight
and colour and the child classes have different properties such as
bitterness (for CLemon) and bendiness (for CBanana). This means that
these specific details for each fruit kind must be kept whilst in the
CArray.

Thankyou in advance to anyone who replies,
Rodney Lorrimar

Sent via Deja.com http://www.deja.com/
Before you buy.



Tue, 28 May 2002 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. CArray of different classes derived from common base

2. address of base class and derived class different

3. CArray and classes derived from a base

4. How to Create CCtrlView derived class based on CTreeCtrl derived class

5. How to Create CCtrlView derived class based on CTreeCtrl derived class

6. How to Stop Derived Class from overriding base class method

7. std::vector holding different class types sharing a same base class

8. creating an instance of a derived class from a base class

9. How to prevent derived classes from changing a base class function

10. Deriving from CControlBar - no base class in New Class dialog

11. CDialog derived class as a base class for another dialog box

12. serializing a class derived from an abstract base class

 

 
Powered by phpBB® Forum Software