GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 19-Apr-2011, 18:52
louiehook louiehook is offline
New Member
 
Join Date: Feb 2011
Posts: 2
louiehook is on a distinguished road

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

CPP / C++ / C 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");
} 

Last edited by LuciWiz : 20-Apr-2011 at 01:57. Reason: Please insert your C++ code between [cpp] & [/cpp] tags
  #2  
Old 19-Apr-2011, 19:34
prattcmp prattcmp is offline
Junior Member
 
Join Date: Mar 2011
Posts: 44
prattcmp will become famous soon enough

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


Quote:
Originally Posted by louiehook
I essentially need to have the member variable of the base class get the value from the derived class.

If I am correct (correct me if I am wrong), you cannot access a derived class's information through a base class. It is just the other way around. But, if the information is made public, you can access it through your "main"
CPP / C++ / C Code:
 int main () 

You did not supply enough information (to me) for me to figure out if that would work.

Did my best to answer question for you
__________________
The Revived Coder
  #3  
Old 20-Apr-2011, 07:25
fakepoo fakepoo is offline
Regular Member
 
Join Date: Oct 2007
Posts: 969
fakepoo is a jewel in the roughfakepoo is a jewel in the roughfakepoo is a jewel in the rough

Re: Member variable of the base class get the value from the derived class.


Quote:
Originally Posted by louiehook
...how do I get the member variable of the base class to get the .bmp indicated in the derived class?
One way to do it is to mark a function as virtual and have the derived classes override it. See Virtual Members.
CPP / C++ / C Code:
class Mammal
{
  public:
    virtual int NumberOfLegs()
    {
      // The default is zero
      return 0;
    }

    void Print()
    {
      cout << "Number of legs: " << NumberOfLegs() << endl;
    }
};

class Dog : public Mammal
{
  public:
    int NumberOfLegs()
    {
      // Dogs have 4 legs
      return 4;
    }
};
  #4  
Old 16-Jun-2011, 22:43
Allan Allan is offline
Junior Member
 
Join Date: Nov 2008
Posts: 33
Allan has a spectacular aura about

Re: Member variable of the base class get the value from the derived class.


Quote:
One way to do it is to mark a function as virtual and have the derived classes override it.


CPP / C++ / C Code:
int main()
{
  // create an instance of a dog
  Dog dog = Dog();
  
  // calling the base Print method by casting it back to Mammal
  // to call the base NumberOfLegs method
  static_cast<Mammal>(dog).Print();
  
  // calling the inherited Print method
  // to call the overriden method NumberOfLegs
  dog.Print(); 
  
  return 0;
}

output:

Number of legs: 0
Number of legs: 4
 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Scope issues with derived class template in g++ Blake C++ Forum 1 04-Apr-2011 14:20
Interfacing a map class insanoflex C++ Forum 9 03-Dec-2010 08:06
[Tutorial] GUI programming with FLTK dsmith FLTK Forum 10 03-Oct-2005 15:41
Static Class Member Accessing Melvin Lin C++ Forum 12 04-May-2004 02:48

Network Sites: GIDNetwork · GIDApp · GIDBlog · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 19:50.


vBulletin, Copyright © 2000 - 2014, Jelsoft Enterprises Ltd.