April 18, 2024, 07:11:06 AM

News:

IonicWind Snippit Manager 2.xx Released!  Install it on a memory stick and take it with you!  With or without IWBasic!


Perhaps a Stupid Question About Class Inheritence

Started by Steven Picard, October 13, 2006, 12:53:14 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Steven Picard

Here some very simple code for me to use in order to demonstrate my question below:


class myClass {

// class constructors
declare _myClass();
declare myClass();

// member functions
declare virtual getValue(),string;
declare virtual setValue(string theString);

// private member variables
string m_value;
}

myClass :: getValue (), string
{
return m_value;
}

myClass :: setValue (string theString)
{
m_value = theString;
print("--- A value was set in myClass through the method 'setValue'");
}

myClass :: myClass()
{
m_value = "--- SIMPLE DEFAULT VALUE";
}

myClass :: _myClass()
{
print("myClass is being destroyed!!");
}

class mySecondClass : myClass
{
declare virtual getValue(),string;
declare virtual setValue(string theString);
}

mySecondClass :: getValue(), string
{
return m_value;
// How would I call the class I am inheriting from here?
}

mySecondClass :: setValue(string theString)
{
m_value = theString;
// How would I call the class I am inheriting from here?
}

global sub main( )
{
// Create instances of the two classes
myClass objClass;
mySecondClass objClass2;

// This outputs the default value
print(objClass.getValue());

// Now I set the value plus this function will print a message to the screen
objClass.setValue("This is the value I put into objClass");

// Now I print the new value
print(objClass.getValue());

// Add one blank lines
print("\n");

// Now for the second class the inherits the first I show the default value
print(objClass2.getValue());

// Now I set a value in the second class
objClass2.setValue("This is the value I put into objClass 2");

// Now I print the new value out
print(objClass2.getValue());

// Wait until the user presses a key
waitForKey();
}

sub waitForKey()
{
ÂÃ,  ÂÃ, print("\n\npress a key to close");
ÂÃ,  ÂÃ, While GetKey() = "";ÂÃ,  ÂÃ, 
}



In mySecondClass I declare setValue as virtual but I would like to execute some code in mySecondClass and then call the inherited class's (myClass) setValue function as well?ÂÃ,  How would I do that?ÂÃ,  And if I can, would it affect my member variable m_value in mySecondClass?
Sorry, I couldn't think of better example code to illustrate my point and hopefully my question doesn't sound too confusing.


Parker

baseclass!!method. For example,
class Base
{
    declare virtual m() { print( "Base::m" ); }
}

class Derived : Base
{
    declare virtual m( ) { print( "Derived::m" ); Base!!m( ); }
}

sub main( )
{
    Derived d;
    d.m( ); // Prints "Derived::m" <newline> "Base::m"
}

Steven Picard

October 13, 2006, 01:30:07 PM #2 Last Edit: October 13, 2006, 01:39:02 PM by stevenp
Oh, okay. Thanks Parker!

Now, that pretty much just creates a new instance of the Base class which wouldn't affect my member variables.ÂÃ,  I was hoping I could use the inherited class's methods and properties as though they were also members of the Derived class (so the function calls directly affect internal variables.)  Kind of like I can just expand on a derived class's functions with additional code.

Parker

Hmm? You mean, the Base method will affect the member variables? It will, or at least it should. You're not creating a new Base class, it passes the same this pointer, but just calls a different method.

Steven Picard

All right, I see it now. *slaps self on forhead*  :D

Steven Picard

BTW, thanks Parker, that's what I was looking for.  :)

Parker