Thread: member variable of the base class get the value from the derived class.

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    2

    member variable of the base class get the value from the derived class.

    To keep this simple I will show part of the "base class, and a derived class".

    I essentially NEED to have the ; member variable of the base class get the value from the derived class.

    Hopefully the code will make it clear what I'm trying to do.
    (I left out a ton of code, and just reference the part that wont work.)

    Assuming I did everything else right, how do I get the member variable of the base class to get the .bmp indicated in the derived class?)


    THANKS! ------LOOK BELOW

    Code:
    
    -----------------------CODE-----------…
    
    ----------BASE CLASS----------
    class cWidget
    {
    public:
    SDL_Surface *widget;
    cWidget();
    }
    
    ----------DERIVED CLASS----------
    class bluecWidget:public cWidget
    {
    public:
    bluecWidget();
    bluecWidget(SDL_Surface *widget);
    };
    
    ----------------------THE FOLLOWING IS IN THE .CPP FILE-----------------
    -----------BASE CLASS--------
    cWidget::cWidget()
    {
    //VARIABLE NOT REFERENCED HERE
    }
    
    -----------BASE CLASS FUNCTION---------
    void cWidget::apply_surface( int x, int y)
    {
    //Blit
    SDL_BlitSurface( widget, NULL, screen, &offset );
    }
    
    
    --------DERIVED CLASS-----------
    bluecWidget::bluecWidget():cWidget()
    {
    
    }
    
    bluecWidget::bluecWidget(SDL_Surface *widget)
    {
    widget = SDL_LoadBMP("Game_blue.bmp");
    }

  2. #2
    Registered User
    Join Date
    Sep 2010
    Posts
    90
    I just completed a derived class assignment a few days ago, a week in advance. If you have not figured out yet, if I saw the full soruce I think I may be able to help. I read on the net that many seasoned C++ and JAVA coders don't know much about base and derived classes. It's a Deitel academic or quirks thing. Its fun to work with.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by louiehook
    Assuming I did everything else right, how do I get the member variable of the base class to get the .bmp indicated in the derived class?
    You already did it: the widget member variable in the derived class is from the base class. Presumably you would declare it as protected instead. If you want to declare it as private, then you might provide a protected setter to set it.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 06-20-2008, 02:41 AM
  2. Replies: 8
    Last Post: 03-19-2008, 03:04 AM
  3. derived class can not access base class protected member?
    By George2 in forum C++ Programming
    Replies: 2
    Last Post: 10-21-2007, 06:32 PM
  4. Replies: 2
    Last Post: 04-06-2005, 07:25 AM
  5. Replies: 1
    Last Post: 12-11-2002, 10:31 PM