[Help]virtual methods, constructors, destructors.

Posts 1–10 of 10 · Page 1 of 1
[Help]virtual methods, constructors, destructors.
I would appreciate explanations, my book does a bad jawb explaining
patrick? o_O
Quote Originally Posted by why06 View Post
patrick? o_O
yes, nao explain
Quote Originally Posted by rtemis View Post
I would appreciate explanations, my book does a bad jawb explaining
Buy another book.
Quote Originally Posted by Void View Post
Buy another book.
Or you could take a couple minutes and explain
what book do you have?
It's a long explanation and I'm tired.

Super short version:
Virtual: virtual things can be used by classes that inherit that base, but not by the base class itself. The reason being is that method has to be defined somewhere. virtual things are not real untill a class implements that virtual method. The advantage fo doing this is a base class pointer can call methods in it's child classes whitout specifying or knowing exactly what each will do.

Costructors: Makes class object, allocates memory, assigns variables, etc.

Destructor: Deletes object.
virtual methods: methods that can be overridden by a derived class.

constructors: code called when object is created. usually used to create mem / resources and initialise variables and data.

destructors: code called when object is destroyed. usually used to free mem / resources. you spelt that wrong btw...
classes.cpp
Code:
Animal::GetAnimalType()
{
    return "I'm a generic animal!";
}

Dog::GetAnimalType()
{
    return "I'm a dog!";
}

Cat::GetAnimalType()
{
    return "I'm a cat!";
}

Korean::GetAnimalType()
{
    return "I AM A HOMOSEXUAL KOREAN!!!!!!!";
}
classes.h
Code:
class Animal
{
public:
    virtual char *GetAnimalName();
};

class Dog : public Animal
{
    char *GetAnimalName();
};

class Cat : public Animal
{
    char *GetAnimalName();
};

class Korean : public Animal
{
    char *GetAnimalName();
}
main.cpp
Code:
#include <iostream>
#include "classes.h"
using namespace std;

int main()
{
    Animal *Anus[4];
    Anus[0] = new Animal;
    Anus[1] = new Dog;
    Anus[2] = new Cat;
    Anus[3] = new Korean;
    for(int i = 0; i < 4; i++)
    {
         cout<<Anus[i]<<endl;
    }
    return 1;
}
Or something in that direction.
Basicly virtual functions can be overwritten by classes that inherit from it. I used it in my D3D menu.
GUIButton, GUITextbox and GUILabels etc all inherited GUIObject which had some generic virtuals:
void Render();
bool TestClick(int x, int y, bool bDouble);

and things like that. that way i could just iterate over the GUIObject vector to test clicks and have everything rendered in just 4 lines(since each object implemented its own rendering system)
nvm, turns out my book didnt get the point across quite well.
Posts 1–10 of 10 · Page 1 of 1

Post a Reply

Tags for this Thread

None

Talk with us