The real power of object oriented programming comes out when you start to add member functions to your objects. This takes a structure and makes it into an object that can actually do something other than just store data. Let's continue with our person example and assume we are only going to store one person for each Person derived object we create. In other words, the member variables will not be arrays themselves. Now lets make it so our object actually does something with this information. Lets have it print its contents:
class Person
local string name
local string address
local string zip
function PrintMe()
? name
? address
? zip
endfunction
endclass
Now lets say we created two instances of this object like we did above and put those instances into People[0] and People[1]. If we did:
People[0].PrintMe()
we would see in the Command / Alert:
Frank Smith
123 Main St
12345
and if we did:
People[1].PrintMe()
we would see:
George Jones
123 Charles St
54321
This is a pretty basic example, but hopefully you can see how this works. The functions are just like sequence functions and can take parameters and return values if desired. You can, of course, have multiple member functions in your class, just bracket each with function / endfunction. If your class derives from another class, you can call the functions of the parent class just like they were functions of your class. If your class has a member function with the same name as one the parent class' member functions, your class is said to have overridden the parent class function and only your function is called. This is really where the power of polymorphism comes in:
class Person
local string name
local string address
local string zip
function PrintMe()
? name
? address
? zip
endfunction
endclass
class PersonPhone parent Person
local string phone
function PrintMe()
? name
? address
? zip
? phone
endfunction
endclass
People[0] = new(Person)
People[0].name = "Frank Smith"
People[0].address = "123 Main St"
People[0].zip = "12345"
People[1] = new(PersonPhone)
People[1].name = "George Johnson"
People[1].address = "123 Charles St"
People[1].zip = "54321"
People[1].phone = "555-1212"
for (private.x = 0, x < numrows(People), x++)
People[x].PrintMe()
endfor
The above code will print this to the command/alert window:
FrankSmith
123 Main St
12345
George Johnson
123 Charles St
54321
555-1212
You can see that the PersonPhone class overrode the PrintMe() function of the parent Person class and printed the phone number as well, but we didn't have to specify which version to use inside the for loop.
If you wish to call the parent version of a function, use the parent keyword. So, we could rewrite the PrintMe() function of PersonPhone to be much simpler, since the first three statements are the same as the parent version of the function we can just call that function:
function PrintMe()
parent.PrintMe()
? phone
endfunction
parent is not an object reference since the parent of an object isn't an object in itself. parent is simply a directive to the compiler to use the parent form of the function. So, while you can do: myObject.parent.PrintMe() to call the parent version of an object referenced by the myObject variable, you cannot do: myParentObject = myObject.parent. So, basically parent has to be followed by a function call.