Classes

From Sputnik Wiki
(Difference between revisions)
Jump to: navigation, search
(Example)
(Overload Example)
Line 32: Line 32:
  
 
=== Overload Example ===
 
=== Overload Example ===
 +
 +
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:
 +
 +
<syntaxhighlight lang="sputnik">
 +
$vec1->$x += $vec2->$x;
 +
$vec1->$y += $vec2->$y;
 +
$vec1->$z += $vec2->$z;
 +
</syntaxhighlight>
 +
 +
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:
 +
 +
<syntaxhighlight lang="sputnik">
 +
$vec1 += $vec2;
 +
</syntaxhighlight>
 +
 +
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.
  
 
==== Overloading = ====
 
==== Overloading = ====

Revision as of 23:23, 19 November 2011

Class <name>
{
	statements
	...
	functions
	...
	operator overloads
	...
	casting overloads
	...
};

Contents

Description

Features

Statements

Variables

Functions

Operator Overloads

Cast Overloads

Remarks

Class Example

Overload Example

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.

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