Page 1 of 1

[SOLVED]C++ Inherit help

Posted: Sun Jan 17, 2010 2:27 pm
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. :)

Re: C++ Inherit help

Posted: Sun Jan 17, 2010 2:31 pm
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.

Re: C++ Inherit help

Posted: Sun Jan 17, 2010 3:45 pm
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

Re: C++ Inherit help

Posted: Sun Jan 17, 2010 4:43 pm
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.

Re: C++ Inherit help

Posted: Sun Jan 17, 2010 6:34 pm
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?

Re: C++ Inherit help

Posted: Tue Jan 19, 2010 1:19 am
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;
}

Re: C++ Inherit help

Posted: Tue Jan 19, 2010 1:25 pm
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.

Re: C++ Inherit help

Posted: Thu Jan 21, 2010 2:56 pm
by qpHalcy0n
For interfaces and abstract types you'll want to consider using a virtual destructor in the above case as well.

Re: C++ Inherit help

Posted: Thu Jan 21, 2010 3:06 pm
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.

Re: C++ Inherit help

Posted: Sat Feb 06, 2010 7:13 am
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

Re: C++ Inherit help

Posted: Sat Feb 06, 2010 7:46 am
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 // }
};