Classes
(→Overloading []) |
(→Overloading []) |
||
Line 369: | Line 369: | ||
} | } | ||
// Overload the [] operator | // Overload the [] operator | ||
− | // Note that you | + | // Note that you don't get the value that is going to |
− | // to the [] this is because Sputnik | + | // to the [] this is because Sputnik doesn't yet know |
// what is going inside it however it knows the array | // what is going inside it however it knows the array | ||
// must be extended to allow something to go inside it. | // must be extended to allow something to go inside it. | ||
Line 377: | Line 377: | ||
Operator "[]" | Operator "[]" | ||
{ | { | ||
+ | // & means return a pointer to new index | ||
return &$this->$Values[]; | return &$this->$Values[]; | ||
} | } | ||
Line 384: | Line 385: | ||
my $Testy = new Testy(); | my $Testy = new Testy(); | ||
− | $Testy[] = "One"; | + | // Now we use use the new index that was returned and place stuff in it |
− | $Testy[] = "Two"; | + | // However it is being returned as a POINTER so to make use of it we |
− | $Testy[] = "Three"; | + | // resolve it back into a variable using the * symbol. |
+ | // * Causes a pointer to become as if it was the actual object. | ||
+ | *$Testy[] = "One"; | ||
+ | *$Testy[] = "Two"; | ||
+ | *$Testy[] = "Three"; | ||
$Testy->PrintMe(); | $Testy->PrintMe(); | ||
Unset($Testy); | Unset($Testy); |
Revision as of 13:23, 9 August 2013
NOTE - Everything listed on this page DOES work even tho there are not examples for everything they do indeed work if you wish to try it out.
Regular class :
Class <name> { statements ... functions ... operator overloads ... casting overloads ... };
Extender class that adds functions and features to an existing class :
Class extends <name> { statements ... functions ... operator overloads ... casting overloads ... };
Inheritance class that inherits functions and features from an existing class :
Class <name> extends <parentname> { statements ... functions ... operator overloads ... casting overloads ... };
Inheritance class that inherits functions and features from multiple existing classes :
Class <name> extends <parentname>, <parentname>, <parentname>... { statements ... functions ... operator overloads ... casting overloads ... };
Description
Features
Statements
Variables
Static Variables
Functions
Static Functions
Operator Overloads
Cast Overloads
Remarks
Examples
Creating Classes
Using Classes
Inheriting Classes
Extending Classes
Multiple Inheritance Classes
Cast Overloading
Sputnik allows you to overload all the castings such as (int)value etc this is useful if you have a class that uses multiple variables and you would like them all added together each time you use (float)$myclass.
Warning - Cast overloading does not apply to the function cast such as int( value ) since that is a function designed to accept an expression and return it converted it is not a cast.
Overloading Cast: char
Overloading Cast: byte
Overloading Cast: sbyte
Overloading Cast: ushort
Overloading Cast: uint16
Overloading Cast: uint
Overloading Cast: uint32
Overloading Cast: uint64
Overloading Cast: ulong
Overloading Cast: short
Overloading Cast: int16
Overloading Cast: int
Overloading Cast: int32
Overloading Cast: int64
Overloading Cast: long
Overloading Cast: float
Overloading Cast: double
Overloading Cast: string
Class Account { my $Name; my $Credits; Function __construct($Name = "", $Credits = 0) { $this->$Name = $Name; $this->$Credits = $Credits; } Operator "string" // This will be done whenever somebody uses (string)$ourclass { return "Account '$Name' Credits '$Credits'"; } }; $nacc = New Account("FoX", 777); println( (string)$nacc ); // Prints Account 'FoX' Credits '777'
Operator Overloading
Sputnik allows you to overload a vast array of operators on your classes this is very helpful for all kinds of things example imagine you have a class that contains 3 varibles X Y and Z and you want to add another classes variables X Y Z onto yours creating a += operator you could quite simply do just that example:
Without overloads:
$vec1->$x += $vec2->$x; $vec1->$y += $vec2->$y; $vec1->$z += $vec2->$z;
As you can see we needed 3 lines of code to do that and doing this over and over in many places of the code will cause a lot of repeat code also what if later we decide we need to add a third variable after z? We would need to go back and change everything...
However if we overload the += operator we can do this:
$vec1 += $vec2;
See how much easier that was? And if we add a new variable or even several later we can just fix our single += overload function and it will automatically fix every single peice of += in your code that uses it.
See the examples below for what you can overload and exactly how to do just that.
Overloading =
This cannot be overloaded!
Overloading +=
Overloading -=
Overloading *=
Overloading **=
Overloading /=
Overloading %=
Overloading .=
Overloading ..=
Overloading ^=
Overloading &=
Overloading |=
Overloading >>=
Overloading <<=
Overloading |
Overloading ^
Overloading &
Overloading +
Overloading -
Overloading *
Overloading **
Overloading /
Overloading %
Overloading .
Overloading <<
Overloading >>
Overloading ++
Class Vec3 { my $x = 0; my $y = 0; my $z = 0; Function __construct($x1 = 0, $y1 = 0, $z1 = 0) { $this->$x = $x1; $this->$y = $y1; $this->$z = $z1; } Operator "++" { $this->$x++; $this->$y++; $this->$z++; } }; $cat1 = new Vec3(10, 20, 30); println("BEFORE ++"); println("Class variable X: " . $cat1->$x); println("Class variable Y: " . $cat1->$y); println("Class variable Z: " . $cat1->$z); $cat1++; println("AFTER ++"); println("Class variable X: " . $cat1->$x); println("Class variable Y: " . $cat1->$y); println("Class variable Z: " . $cat1->$z); // Prints // BEFORE ++ // Class variable X: 10 // Class variable Y: 20 // Class variable Z: 30 // AFTER ++ // Class variable X: 11 // Class variable Y: 21 // Class variable Z: 31
Overloading --
Class Vec3 { my $x = 0; my $y = 0; my $z = 0; Function __construct($x1 = 0, $y1 = 0, $z1 = 0) { $this->$x = $x1; $this->$y = $y1; $this->$z = $z1; } Operator "--" { $this->$x--; $this->$y--; $this->$z--; } }; $cat1 = new Vec3(10, 20, 30); println("BEFORE --"); println("Class variable X: " . $cat1->$x); println("Class variable Y: " . $cat1->$y); println("Class variable Z: " . $cat1->$z); $cat1++; println("AFTER --"); println("Class variable X: " . $cat1->$x); println("Class variable Y: " . $cat1->$y); println("Class variable Z: " . $cat1->$z); // Prints // BEFORE -- // Class variable X: 10 // Class variable Y: 20 // Class variable Z: 30 // AFTER -- // Class variable X: 9 // Class variable Y: 19 // Class variable Z: 29
Overloading ==
Overloading !=
Overloading <
Overloading <=
Overloading >
Overloading >=
Overloading <>
Overloading eq
Overloading eqi
Overloading neq
Overloading neqi
Overloading ||
Overloading &&
Overloading []
Class Testy { my $Values; Function __Construct() { $this->$Values = array(); println("A new Testy() class was made"); } Function __Deconstruct() { $this->$Values = array(); println("A Testy() class was destroyed"); } Function PrintMe() { println("Values BELOW"); printr($Values); println("Values ABOVE"); } // Overload the [] operator // Note that you don't get the value that is going to // to the [] this is because Sputnik doesn't yet know // what is going inside it however it knows the array // must be extended to allow something to go inside it. // So we simply extend the array and return a pointer to it // This will allow the array element to be set later Operator "[]" { // & means return a pointer to new index return &$this->$Values[]; } }; my $Testy = new Testy(); // Now we use use the new index that was returned and place stuff in it // However it is being returned as a POINTER so to make use of it we // resolve it back into a variable using the * symbol. // * Causes a pointer to become as if it was the actual object. *$Testy[] = "One"; *$Testy[] = "Two"; *$Testy[] = "Three"; $Testy->PrintMe(); Unset($Testy);