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

This is a discussion on member variable of the base class get the value from the derived class. within the C++ Programming forums, part of the General Programming Boards category; To keep this simple I will show part of the "base class, and a derived class". I essentially NEED to ...

  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
    22,269
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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, 03:41 AM
  2. Replies: 8
    Last Post: 03-19-2008, 04: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, 07:32 PM
  4. Replies: 2
    Last Post: 04-06-2005, 08:25 AM
  5. Replies: 1
    Last Post: 12-11-2002, 10:31 PM

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