Classes

From FizzFuzz

Jump to: navigation, search

In FizzFuzz, class definitions are denoted by the class keyword. Along side there, there are several class modifiers and access modifiers that can be used to alter aspects of how classes work.

Contents

Defining A Class

A class is defined by using the class keyword, preceded by an (optional) access modifier. If no access modifier is included, then it defaults to public.

//This creates a class that can be accessed by anything.
public class __class__

inherit Class Modifier

Main Article: Inheritance

The inherit class modifier indicates that a certain class inherits from another class, meaning it has variables and methods from that class. These derived variables and methods can be modified

#include <std.dflt.*>
using variable environment
 
//Creates the __class__ class.
class __class__
    //Creates a variable and method.
    string name = "Gordon"
 
    void Greeting()
        outputln("Hello, my name is [name].")
 
//Creates the __derived__ class.
class __derived__
    //Indicates the class inherits from __class__.
    inherit __class__
 
    /*
    As these are inherited from the __class__ class, they
    don't need to have a type set. This inherits from the
    parent class.
    */
    name = "Shauni"
 
    Greeting()
        outputln("Greetings, my name is [name].")

namespace Modifier

Main article: Namespaces

The namespace modifier creates a namespace, which is a specific group the class is tied to. This allows multiple classes of the same name to exist, while tied to a unique namespace.

#include <std.dflt.*>
using variable environment
 
//Creates a normally-accessible class called __a__.
class __a__
    void Boing()
        outputln("Boing")
 
//Creates a class that exists in the namespace classes,
//meaning it is distinct from the global __a__ namespace.
class __a__
    namespace classes
 
    void Bloop()
        outputln("Bloop.")
 
//Creates a class a __b__ class in the classes namespace.
class __b__
    void Blarg()
        outputln("Boing.")
 
void main()
    //This all runs properly.
    __a__ a = new
    classes:__a__ b = new
    classes:__b__ c = new
 
    a.Boing()
    b.Bloop()
    c.Blarg()

overload Modifier

Main Article: Operator Overloading

The overload operator is used for operator overloading, which allows operations performed on an instance of the class to act in a manner specified by the programmer, rather than the manner which they act, by default.

#include <std.dflt.*>
 
//Creates a class which just acts like a number.
class number_class
    //Overloads the operators for assignment and arithmetic-
    //based assignment.
    overload {$+=n} add_assign(n)
    overload {$-=n} sub_assign(n)
    overload {$*=n} mult_assign(n)
    overload {$/=n} div_assign(n)
    overload {$=n) assign(n)
 
    //Overloads general-case scenarios.
    overload {print} output()
    overload {employ} output()
 
    private number value = 0
 
    +number_class(number n = 0)
        assign(n)
 
    void add_assign(number n)
        value += n
 
    void sub_assign(number n)
        value -= n
 
    void mult_assign(number n)
        value *= n
 
    void div_assign(number n)
        value /= n
 
    void assign(number n)
        value = n
 
    number output()
        return value
 
void main()
    //Creates the number_class instance, and instantiates it with
    //a value of 3.
    number_class n = new(3)
    n += 3.5 //Adds 3.5 to it, meaning it now stores 6.5.
    n *= 2   //Multiplies it by 2, meaning it now stores 13.
    n -= 3   //Subtracts 3, meaning it now stores 10.
    n /= 5   //Divides it by 5, so it now stores 2.
 
    environment.outputln(n) //Outputs "2".
    environment.outputln(n + 5) //Outputs "7".
Personal tools
Navigation
functions
language