Classes

From Sputnik Wiki
(Difference between revisions)
Jump to: navigation, search
Line 1: Line 1:
 +
Regular class :
 +
 
<pre>
 
<pre>
 
Class <name>
 
Class <name>
 +
{
 +
statements
 +
...
 +
functions
 +
...
 +
operator overloads
 +
...
 +
casting overloads
 +
...
 +
};
 +
</pre>
 +
 +
Extender class that adds functions and features to an existing class :
 +
 +
<pre>
 +
Class extends <name>
 +
{
 +
statements
 +
...
 +
functions
 +
...
 +
operator overloads
 +
...
 +
casting overloads
 +
...
 +
};
 +
</pre>
 +
 +
Inheritance class that inherits functions and features from an existing class :
 +
 +
<pre>
 +
Class <name> extends <parentname>
 +
{
 +
statements
 +
...
 +
functions
 +
...
 +
operator overloads
 +
...
 +
casting overloads
 +
...
 +
};
 +
</pre>
 +
 +
Inheritance class that inherits functions and features from multiple existing classes :
 +
 +
<pre>
 +
Class <name> extends <parentname>, <parentname>, <parentname>...
 
{
 
{
 
statements
 
statements
Line 20: Line 70:
  
 
==== Variables ====
 
==== Variables ====
 +
 +
==== Static Variables ====
  
 
==== Functions ====
 
==== Functions ====
 +
 +
==== Static Functions ====
  
 
==== Operator Overloads ====
 
==== Operator Overloads ====

Revision as of 22:05, 20 November 2011

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
	...
};

Contents

Description

Features

Statements

Variables

Static Variables

Functions

Static Functions

Operator Overloads

Cast Overloads

Remarks

Examples

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.

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 Vec3($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 Vec3($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 &&

Personal tools
Namespaces
Variants
Actions
Navigation
Toolbox