[SOLVED]C++ Inherit help

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
User avatar
Zer0XoL
ES Beta Backer
ES Beta Backer
Posts: 54
Joined: Fri Apr 24, 2009 1:18 pm
Current Project: Zelda untitled multiplayer game
Favorite Gaming Platforms: PC, GBA, N64
Programming Language of Choice: C++, Lua
Location: Sweden
Contact:

[SOLVED]C++ Inherit help

Post by Zer0XoL »

Hi everyone, I am making a Gui system and ive come across something i cant seem to solve.
This is what the problem looks like:

Code: Select all

class g_element
{
protected:
int x, y;
public:
g_element(int _x, int _y){x = _x; y = _y;}
};
This is the base class for all elements like g_button should be based on

Code: Select all

class g_button : public g_element
{
private:
int w, h;
public:
g_button(int _x, int _y, int _w, int _h){x = _x; y = _y; w = _w; h = _h;}
void Handle();
void Draw();
};
And then when i handle all elements i use something similar to this:

Code: Select all

for(int i=0;i<element.size();i++)
{
element[i].Handle();
}
(element is a vector of g_element)
And well i know that g_element has no function named Handle(), but if i add one it gives me an error.
Also all objects in element are != g_element and all other classes should have the function Handle().
Im having a problem, i think its related to virtual functions but i dont know, i wish you guys could help me, thanks. :)
Last edited by Zer0XoL on Sat Feb 06, 2010 7:12 am, edited 1 time in total.
Image
Im Blue
User avatar
Bakkon
Chaos Rift Junior
Chaos Rift Junior
Posts: 384
Joined: Wed May 20, 2009 2:38 pm
Programming Language of Choice: C++
Location: Indiana

Re: C++ Inherit help

Post by Bakkon »

You may want to make g_element an abstract class by adding something like

Code: Select all

void virtual Handle() = 0;
void virtual Update() = 0;
void virtual Draw() = 0;
and you might want your vector to be of g_element pointers.
User avatar
Zer0XoL
ES Beta Backer
ES Beta Backer
Posts: 54
Joined: Fri Apr 24, 2009 1:18 pm
Current Project: Zelda untitled multiplayer game
Favorite Gaming Platforms: PC, GBA, N64
Programming Language of Choice: C++, Lua
Location: Sweden
Contact:

Re: C++ Inherit help

Post by Zer0XoL »

Bakkon wrote:You may want to make g_element an abstract class by adding something like

Code: Select all

void virtual Handle() = 0;
void virtual Update() = 0;
void virtual Draw() = 0;
and you might want your vector to be of g_element pointers.
I tried but it gives me this:

Code: Select all

cannot instantiate abstract class
1>        due to following members:
1>        'void Gui::g_element::Handle(void)' : is abstract
Image
Im Blue
andrew
Chaos Rift Regular
Chaos Rift Regular
Posts: 121
Joined: Mon Dec 08, 2008 2:12 pm

Re: C++ Inherit help

Post by andrew »

Code: Select all

class g_element
{
protected:
	int x, y;
public:
	g_element(int _x, int _y){x = _x; y = _y;}
	void virtual Handle() = 0;
	void virtual Update() = 0;
	void virtual Draw() = 0;
};

Code: Select all

class g_button : public g_element
{
private:
	int w, h;
public:
	g_button(int _x, int _y, int _w, int _h){x = _x; y = _y; w = _w; h = _h;}
	void Handle();
	void Update();
	void Draw();
};
You have to implement those virtual functions.
User avatar
Bakkon
Chaos Rift Junior
Chaos Rift Junior
Posts: 384
Joined: Wed May 20, 2009 2:38 pm
Programming Language of Choice: C++
Location: Indiana

Re: C++ Inherit help

Post by Bakkon »

Zer0XoL wrote: I tried but it gives me this:

Code: Select all

cannot instantiate abstract class
1>        due to following members:
1>        'void Gui::g_element::Handle(void)' : is abstract
Did you switch your vector to hold pointers? You can't instantiate abstract objects but you can define pointers to one. What's it look like when you create and add a button object?
User avatar
GroundUpEngine
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 835
Joined: Sun Nov 08, 2009 2:01 pm
Current Project: mixture
Favorite Gaming Platforms: PC
Programming Language of Choice: C++
Location: UK

Re: C++ Inherit help

Post by GroundUpEngine »

my polymorphism is a bit rough, but I read ya post and threw this together real quick dunno if it helps

Code: Select all

class g_element {
    protected:
       int x, y;
    public:
       void set_values(int _x, int _y)
         { x = _x; y = _y; }
       virtual void Handle() = 0;
       virtual void Update() = 0;
       virtual void Draw() = 0;
};

Code: Select all

class g_button : public g_element {
    private:
       int width, height;
    public:
       void set_values(int _x, int _y, int _w, int _h)
         { x = _x; y = _y; width = _w; height = _h; }
       void Handle()
         { cout << "x: " << this->x << ", y: " << this->y
         << " w: " << this->width << ", h: " << this->height << endl; }
       void Update(){}
       void Draw(){}
};

Code: Select all

int main(){
    //5 Buttons
    vector<g_button>button(5);
    
    for(int i = 0; i < button.size(); i++){
        //Set random values
        button[i].set_values(rand(), rand(), rand(), rand());
        //Print values for each button
        button[i].Handle();
    }

    cin.get(); return 0;
}
andrew
Chaos Rift Regular
Chaos Rift Regular
Posts: 121
Joined: Mon Dec 08, 2008 2:12 pm

Re: C++ Inherit help

Post by andrew »

Here's the simplest example of polymorphism I could come up with:

Code: Select all

#include <iostream>

class Animal {
    public:
        virtual void Speak() = 0;
};

class Duck : public Animal {
    public:
        void Speak() { std::cout << "Quack!\n"; }
};

class Dog : public Animal {
    public:
        void Speak() { std::cout << "Bark!\n"; }
};

int main() {
    Animal *Donald = new Duck;
    Animal *Spike = new Dog;
    Donald->Speak();
    Spike->Speak();
    delete Donald;
    delete Spike;
    return 0;
}
You could put them in a list like this:

Code: Select all

    std::list <Animal*> Animals;
    Animals.push_back(new Dog);
    Animals.push_back(new Duck);
    Animals.push_back(new Duck);
    Animals.push_back(new Dog);
    Animals.push_back(new Dog);

    std::list <Animal*>::iterator node = Animals.begin();
    while (node != Animals.end()) {
        (*node)->Speak();
        ++node;
    }

    node = Animals.begin();
    while (node != Animals.end()) {
        if ((*node) != NULL) {
            delete *node;
            *node = NULL;
        }
        ++node;
    }
Bakkon wrote:You can't instantiate abstract objects but you can define pointers to one.
@Zer0Xol: This is true, if you don't do it you'll get that error that you posted.
qpHalcy0n
Respected Programmer
Respected Programmer
Posts: 387
Joined: Fri Dec 19, 2008 3:33 pm
Location: Dallas
Contact:

Re: C++ Inherit help

Post by qpHalcy0n »

For interfaces and abstract types you'll want to consider using a virtual destructor in the above case as well.
User avatar
Falco Girgis
Elysian Shadows Team
Elysian Shadows Team
Posts: 10294
Joined: Thu May 20, 2004 2:04 pm
Current Project: Elysian Shadows
Favorite Gaming Platforms: Dreamcast, SNES, NES
Programming Language of Choice: C/++
Location: Studio Vorbis, AL
Contact:

Re: C++ Inherit help

Post by Falco Girgis »

qpHalcy0n wrote:For interfaces and abstract types you'll want to consider using a virtual destructor in the above case as well.
VERY important that you listen to this man.
User avatar
Zer0XoL
ES Beta Backer
ES Beta Backer
Posts: 54
Joined: Fri Apr 24, 2009 1:18 pm
Current Project: Zelda untitled multiplayer game
Favorite Gaming Platforms: PC, GBA, N64
Programming Language of Choice: C++, Lua
Location: Sweden
Contact:

Re: C++ Inherit help

Post by Zer0XoL »

GyroVorbis wrote:
qpHalcy0n wrote:For interfaces and abstract types you'll want to consider using a virtual destructor in the above case as well.
VERY important that you listen to this man.
And how do i make a virtual destructor?
Thanks all for the help btw :D
Image
Im Blue
User avatar
GroundUpEngine
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 835
Joined: Sun Nov 08, 2009 2:01 pm
Current Project: mixture
Favorite Gaming Platforms: PC
Programming Language of Choice: C++
Location: UK

Re: C++ Inherit help

Post by GroundUpEngine »

Zer0XoL wrote:
GyroVorbis wrote:
qpHalcy0n wrote:For interfaces and abstract types you'll want to consider using a virtual destructor in the above case as well.
VERY important that you listen to this man.
And how do i make a virtual destructor?
Thanks all for the help btw :D

Code: Select all

class g_element
{
    public:    
      g_element() { // Constructor: Base // }
      virtual ~g_element() { //  Destructor : Base // }
};

class g_button : public g_element
{
    public:    
      g_button(){ // Constructor: Derived // }
      ~g_button { // Destructor : Derived // }
};
Post Reply