July 05, 2024, 05:20:37 AM

News:

Own IWBasic 2.x ? -----> Get your free upgrade to 3.x now.........


Noob questions #2

Started by Protected, May 19, 2006, 09:27:10 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Protected

Sorry for being annoying about this, just wanted to make a few things clear ~_~

First, is it perfectly okay NOT to define methods that are NOT called by me, like OnCreate for example?

Second, do I have to declare all the methods in my class? Or is that just when they are defined outside the class definition? It's possible to define the methods inside the braces for the class, right?

Third, can one class inherit from two or more classes?

Mike Stefanik

You don't have to implement any virtual methods that you don't explicitly use. For those that you don't, the derived class will simply call the base class implementation of that method.

You do need to declare all of the methods, and Aurora doesn't support inline methods. They're declared in the class definition, and then implemented outside of the class. In other words, you can't do this:


class MyClass
{
    myFunction(int param1, int param2), int
    {
        return (param1 + param2);
    }
}


You have to implement it as:


class MyClass
{
    declare MyFunction(int param1, int param2), int;
}

MyClass::MyFunction(int param1, int param2), int
{
    return (param1 + param2);
}


At this point Aurora doesn't support multiple inheritance.
Mike Stefanik
www.catalyst.com
Catalyst Development Corporation

Protected

Thanks. Multiple inheritance was just for curiosity. About the other questions - I asked about the OnCreate, etc because I wasn't exactly sure what they were. For example, is OnCreate called in the constructor? If I replace the constructor, will I have to call OnCreate inside it? Or are they called separately? Is the constructor a virtual method that can be implemented? Is that why it can't have arguments? I'm trying to understand how OOP works in this language, I'm wondering if custom virtual methods/constructors/event handlers work the same way, or what the differences between them are.

Mike Stefanik

For CWindow derived classes, OnCreate is called whenever the WM_CREATE message is sent to the Window. Most of the On.. methods are essentially like "event handlers" that allow you to implement code in response to specific Windows messages. Although it has the word 'create' in it, it actually has nothing to do with the class constructor. And you'd generally never call OnCreate yourself. You'd implement it and it would be called for you when the message is sent to your window (to really understand how that all works, you also need to understand how the Windows messaging system works).

When an instance of a derived class is created, the constructor for the base class is called, then the constructor for the derived class. When that instance is deleted, then the destructor for the derivced class is called, followed by the destructor for the base class. Try this program out and you'll see what I mean:


class CBase
{
    declare CBase();        // CBase constructor
    declare _CBase();       // CBase destructor
}

CBase::CBase()
{
    writeln("CBase constructor has been called\n");
    return;
}

CBase::_CBase()
{
    writeln("CBase destructor has been called\n");
    return;
}

class CDerived : CBase
{
    declare CDerived();     // CDerived constructor
    declare _CDerived();    // CDerived destructor
}

CDerived::CDerived()
{
    writeln("CDerived constructor has been called\n");
    return;
}

CDerived::_CDerived()
{
    writeln("CDerived destructor has been called\n");
    return;
}


global sub main
{
    CDerived *pClass;

    pClass = new(CDerived, 1);  // Create an instance of CDerived
    delete pClass;              // Delete the instance of CDerived

    while (GetKey() == "");
    return;
}


The reason constructors can't have arguments is because the compiler doesn't support method overloading. In other words, you can't do this:


class MyClass
{
    declare MyFunction(), int;
    declare MyFunction(int nParam), int;
    declare MyFunction(string strParam), int;
}


In other OOP languages like C++, that sort of thing is allowed, where the same function name is used but it has different arguments and the compiler automatically picks the correct implementation based on the arguments that you pass.
Mike Stefanik
www.catalyst.com
Catalyst Development Corporation