Click here to Skip to main content
15,914,014 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: memory allocation syntax Pin
Mark Salsbery1-Jul-07 19:42
Mark Salsbery1-Jul-07 19:42 
GeneralRe: memory allocation syntax Pin
prithaa1-Jul-07 19:59
prithaa1-Jul-07 19:59 
GeneralRe: memory allocation syntax Pin
Mark Salsbery1-Jul-07 20:09
Mark Salsbery1-Jul-07 20:09 
GeneralRe: memory allocation syntax Pin
prithaa1-Jul-07 20:21
prithaa1-Jul-07 20:21 
QuestionAdding Members to a Base Class Pin
ForNow1-Jul-07 8:05
ForNow1-Jul-07 8:05 
AnswerRe: Adding Members to a Base Class Pin
Mark Salsbery1-Jul-07 8:49
Mark Salsbery1-Jul-07 8:49 
GeneralRe: Adding Members to a Base Class Pin
ForNow1-Jul-07 8:57
ForNow1-Jul-07 8:57 
GeneralRe: Adding Members to a Base Class Pin
Mark Salsbery1-Jul-07 12:11
Mark Salsbery1-Jul-07 12:11 
Members should be added to the appropriate class, and/or, at the appropriate place in the class
hierarchy. Expanding on the example:
class Fruit
{
protected:
   int Count;
public:
   Fruit() {Count = 0;}
   int GetCount()  {return Count;}
   virtual bool IsCitrus() = 0;
};
 
class Orange : public Fruit
{
public:
   virtual bool IsCitrus() {return true;}
};
 
class Apple : public Fruit
{
protected:
   COLORREF AppleColor;
public:
   Apple()  {AppleColor = RGB(0x00,0x00,0x00);}
   virtual bool IsCitrus() {return false;}
};
 
class GreenApple : public Apple
{
public:
   GreenApple()  {AppleColor = RGB(0x00,0xFF,0x00);}
};
 
class RedApple : public Apple
{
public:
   RedApple()  {AppleColor = RGB(0xFF,0x00,0x00);}
};

This is a silly example, but notice...

All Fruits can have a count, so I added a Count member variable to the Fruit class. All derived
classes will have this variable as well.

I want to differentiate between citrus and non-citrus fruits so I added a pure virtual method to
the Fruit class. Pure virtual means derived classes HAVE to implement the virtual method. I
went ahead and implemented the virtual method in the derived Apple and Orange classes.

I wanted all Apple classes to have a color, so I added an AppleColor member variable to the Apple
class. All Apple and Apple-derived objects will have this member. Fruit does NOT have this
member because Fruit is not an Apple so it makes no sense for it to have an Apple color. It's
not a great example, since in real-life, you'd probably want all Fruits to have a color.
Hopefully having just color for apples demonstrates why the member is added at that spot in the
hierarchy.

Mark



Mark Salsbery
Microsoft MVP - Visual C++

GeneralRe: Adding Members to a Base Class Pin
ForNow1-Jul-07 14:19
ForNow1-Jul-07 14:19 
GeneralRe: Adding Members to a Base Class Pin
Steve Echols1-Jul-07 17:32
Steve Echols1-Jul-07 17:32 
GeneralRe: Adding Members to a Base Class Pin
Mark Salsbery2-Jul-07 6:59
Mark Salsbery2-Jul-07 6:59 
GeneralRe: Adding Members to a Base Class Pin
Mark Salsbery2-Jul-07 7:01
Mark Salsbery2-Jul-07 7:01 
GeneralRe: Adding Members to a Base Class Pin
ForNow2-Jul-07 12:13
ForNow2-Jul-07 12:13 
QuestionURLDownloadToFile dont work in worker thread [modified] Pin
awah1-Jul-07 5:19
awah1-Jul-07 5:19 
AnswerRe: URLDownloadToFile dont work in worker thread Pin
Mark Salsbery1-Jul-07 9:00
Mark Salsbery1-Jul-07 9:00 
Questionproblem in USB Pen Drive detection Pin
cyberanee1-Jul-07 3:42
cyberanee1-Jul-07 3:42 
QuestionWhere to declare global variables? Pin
KaKa'1-Jul-07 1:48
KaKa'1-Jul-07 1:48 
AnswerRe: Where to declare global variables? Pin
john56321-Jul-07 3:45
john56321-Jul-07 3:45 
AnswerRe: Where to declare global variables? Pin
Arman S.1-Jul-07 4:33
Arman S.1-Jul-07 4:33 
AnswerRe: Where to declare global variables? Pin
Michael Dunn1-Jul-07 12:22
sitebuilderMichael Dunn1-Jul-07 12:22 
QuestionDLL load prevention Pin
Perspx1-Jul-07 1:13
Perspx1-Jul-07 1:13 
QuestionRe: DLL load prevention Pin
David Crow2-Jul-07 4:46
David Crow2-Jul-07 4:46 
Questionclosing application Pin
john56321-Jul-07 0:24
john56321-Jul-07 0:24 
AnswerRe: closing application Pin
Arman S.1-Jul-07 4:26
Arman S.1-Jul-07 4:26 
AnswerRe: closing application Pin
Stephen Hewitt1-Jul-07 15:10
Stephen Hewitt1-Jul-07 15:10 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.