Classes and Derivation

This is a discussion on Classes and Derivation within the C++ Programming forums, part of the General Programming Boards category; I've got an abstract class, CFractal, and two derived classes, CFractalMandel and CFractalJulia. I plan on adding several more in ...

  1. #1
    Registered User
    Join Date
    Jun 2002
    Posts
    17

    Classes and Derivation

    I've got an abstract class, CFractal, and two derived classes, CFractalMandel and CFractalJulia. I plan on adding several more in the future.

    Well, in my main app i have a pointer to a CFractal which will point one of the two or more derived classes. Well, when the user does something (zooms in, in my case) I want to create a new object with the same type as the current pointer. So if my CFractal pointer points to a CFractalMandel, well then the new object should be of that class, or if its CFractalJulia well then of that type.

    Both derived classes have the same constructors (they just pass paramaters along to the CFractal), and I dont want to have to resort to something like saying if *mainFrac->GetKindOfFractal == Mandel... if *mainFrac-<GetKindOfFractal == Julia...

    Im doing this under MFC btw, if that helps at all.

  2. #2
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    Make a clone method in your CFractal class. It will allocate a subclass of CFractal, and return a CFractal* to the allocated type.
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  3. #3
    Registered User
    Join Date
    Jun 2002
    Posts
    17
    hmm.. Im not quite sure what you mean, sorry I'm sorta new to C++. From MSDN all i seem to gather is that subclassing only has to do with windows, like CWnd.

    I was thining that I would need to add some method in CFractal, but could you xplain what you meant?

    Thanks alot.

  4. #4
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    Code:
    class CFractal {
    public:
        virtual void doFractalThing()=0;
        CFractal* clone()=0;
    };
    
    class CFractalMandel : public CFractal{
    public:
        virtual void doFractalThing();
        CFractal* clone() { return new CFractalMandel(*this); }
        // define the copy constructor if need be
    private:
      // whatever
    };
    
    class CFractalJulia : public CFractal{
    public:
         virtual void doFractalThing();
         CFractal* clonse() { return new CFractalJulia(*this); }
         // again, define copy constructor if needed
    private:
        // whatever
    }; 
    
    CFractal* mainFractal;
    
    void onZoomIn() {
         CFractal* newFract = mainFractal->clone();
        // do whatever, and remember to deallocate newFract when time comes
    }
    edit: added class inheritance stuff (big missing factor.. sorry )
    Last edited by SilentStrike; 06-01-2002 at 03:47 AM.
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  5. #5
    Registered User
    Join Date
    Jun 2002
    Posts
    17
    Hey thanks alot, worked pretty well.

    I have another question now... is there some way so that when you call a overloaded method from a derived class, both that one and the base class' will be executed?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. what's a game engine?
    By h_howee in forum Game Programming
    Replies: 15
    Last Post: 06-25-2007, 07:32 PM
  2. Creating a database
    By Shamino in forum Game Programming
    Replies: 19
    Last Post: 06-10-2007, 02:09 PM
  3. derived class from string
    By brianptodd in forum C++ Programming
    Replies: 17
    Last Post: 12-10-2003, 05:34 PM
  4. Inheiritance and derived classes
    By pecymanski in forum C++ Programming
    Replies: 2
    Last Post: 12-09-2001, 03:50 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21