---
title: "Class"
canonical: "https://docs.cuesystem.com/space/CVCHELP/509018120/Class"
format: markdown
---
> Macro (toc)

---

# Class

A class is a data type that combines constants, variables, functions, and processes into one object. The class data type is defined by its ClassName. A class variable is declared via the ClassName (not by the keyword Class). A class can inherit program code from another class. 

#### Syntax of a Class data type declaration 

```visualbasic
[Accessibility] Class ClassName [Inherits ParentClassName] [WithAttributes {Attribute1[, Attribute2, ..., AttributeN]}] 
  [Friend FriendClassName | FriendFunctionName] 
  [Constant declarations] 
  [Variable declarations] 
  [Function declarations] 
  [Function Init declaration] 
  [Process declarations] 
End Class
```

#### Attribute: Info 

|  |  |  |
| --- | --- | --- |
| Value type | Meaning | Example |
| Text | Text which will be visible in a tooltip window for this class. | Info := "Any text " |

#### Example of a class type and a class variable 

```visualbasic
// class type 
Class CRectangle WithAttributes { Info:= "My special class for representing a rectangle" } 
  Var Width As Long 
  Var Height As Long 

  Public Function GetArea() As Long 
    Return Value Width * Height 
  End Function 
End Class 

// class variables 
Var Rectangle1 As CRectangle 
Var Rectangle2 As CRectangle
```

#### Class items 

A class' constants, variables, functions, and processes are accessible via a ClassVariableName and their names. A full stop separates the ClassVariableName and the constant/variable/function/process name. 

```visualbasic
// class type 
Class CRectangle 
  Var Width As Long 
  Var Height As Long 

  Public Function SetWidth(NewWidth As Long) 
    Width := NewWidth 
  End Function 

  Public Function SetHeight(NewHeight As Long) 
    Height := NewHeight 
  End Function 

  Public Function GetArea() As Long 
    Return Value Width * Height 
  End Function 
End Class 


Private Process Autoexec() 
  // class variable 
  Var Rectangle As CRectangle 
  Var Area As Long 

  // class function calling 
  Rectangle.SetWidth(10) 
  Rectangle.SetHeight(20) 
  Area := Rectangle.GetArea() 
End Process
```

# Inheritance

A class can inherit constants, variables, functions, and processes from another (parent) class. It is useful to structure a program code. The inherited items are operated the same way as they were declared in the class. 

#### Example of an inheritance* *

```visualbasic
Class CParent 
  Protected Var x As Long 
  Protected Var y As Long 
End Class 


// class CChild inherits variables x and y from class CParent 
Class CChild Inherits CParent 
  Protected Var z As Long 
End Class
```

#### Inheritance via another class 

A child’s class inherits directly from a parent class. A grandchild class inherits directly from the child class and indirectly via the child class inherits from a parent class. 

#### Example of inheritance via another class 

```visualbasic
Class ClassA 
  Protected Var an As Long 

  Function SetValA() 
    a := 1 
  End Function 
End Class 


Class ClassB Inherits ClassA 
  Protected Var b As Long 

  Function SetValB() 
    a := 2 // a is inherited from ClassA 
    b := 2 
  End Function 
End Class 


Class ClassC Inherits ClassB 
  Protected Var c As Long 

  Function SetValC() 
    a := 3 // a is inherited from ClassA via ClassB 
    b := 3 // b is inherited from ClassB 
    c := 3 
  End Function 
End Class
```

#### Inheritance accessibility 

The class's constants, variables, functions, and processes can be inherited with different accessibility: 

- **Private**: They are accessible only from within the class where they were declared. They are not accessible from an inherited class. They are not accessible from outside the class.
- **Protected**: They are accessible from within the class where they were declared and from within all classes which are inherited from this class. They are not accessible from outside the class.

- **Public**: They are accessible from within the class where they were declared and from within all classes which are inherited from this class. They are accessible from outside the class as well.
- **Global**: They are accessible from within the class where they were declared and from within all classes which are inherited from this class. They are accessible from outside the class as well.

Read more in the chapter Accessibility. 

#### Example of inheritance accessibility 

```visualbasic
Class CParent 
  Var PrivateX As Long 
  Protected Var ProtectedX As Long 
  Public Var PublicX As Long 
  Global Var GlobalX As Long 
End Class 

Class CChild Inherits CParent 
  Function OperateValues() 
    // PrivateX - private item is not accessible 
    ProtectedX := 0 // protected item is accessible 
    PublicX := 0 // public item is accessible 
    GlobalX := 0 // global item is accessible 
  End Function 
End Class 
 

Private Process Autoexec() 
  Var Parent As CParent 
  Var Child As CChild 

  // Parent.PrivateX - private item is not accessible 
  // Parent.ProtectedX - protected item is not accessible 
  Parent.PublicX := 0 // public item is accessible 
  Parent.GlobalX := 0 // global item is accessible 

  // Child.PrivateX - private item is not accessible 
  // Child.ProtectedX - protected item is not accessible 
  Child.PublicX := 0 // public item is accessible 
  Child.GlobalX := 0 // global item is accessible 
End Process
```

# Friend

A friend class/function is useful possibility when you need to use private or protected items of another class. The class/function is declared as a friend inside the class. The friends can use private and protected items of the class the same way as they are public. 

#### Syntax 

```visualbasic
[Friend FriendClassName | FriendFunctionName]
```

#### Example* *

```visualbasic
Class CMath 
  Friend CDisplay // class CDisplay can use Private and Protected items of CMath 
  Friend FDisplay // function FDisplay can use Private and Protected items of CMath 

  Function GetSum(a As Long, b As Long) As Long 
    Return Value a + b 
  End Function 
End Class 


Class CDisplay 
  Var Math As CMath 

  Public Function DisplayResult() 
    DebugPrint("CDisplay: I am friend of CMath, so I can use it's private function GetSum.", Math.GetSum(1, 2)) 
  End Function 
End Class 
 

Function FDisplay() 
  Var Math As CMath 

  DebugPrint("FDisplay: I am a friend of CMath, so I can use its private function GetSum.", Math.GetSum(3, 4)) 
End Function 
 

Private Process Autoexec() 
  Var Display As CDisplay 

  Display.DisplayResult() 
  FDisplay() 
End Process
```

It prints to the debug window: 

```visualbasic
CDisplay: I am a friend of CMath, I can use its private function GetSum. 
3 
FDisplay: I am a friend of CMath, I can use its private function GetSum. 
7
```

# Virtual functions

A child class can contain a function with the same name as the name of the inherited function. It is useful when you need a child's function with the same name as the name of the parent's function, but with different behavior. 

Note: A child class cannot contain a variable with the same name as the name of the inherited variable. 

#### Example 

```visualbasic
Class CParent 
  Protected Function f() 
    // special function for a parent class 
  End Function 
End Class 


Class CChild Inherits CParent 
  // the same name as a Parent's function but with a different behavior 
  Protected Function f() 
    // special function for a child class 
  End Function 
End Class
```

If a class has its function with the same name as an inherited function, the own function is executed when the function name is called. If the class has not got own function, the inherited function is executed when the function name is called. 

#### Example 

```visualbasic
Class CParent 
  Public Function Start() 
    DebugPrint("Start anything") 
  End Function 

  Public Function Stop() 
    DebugPrint("Stop anything") 
  End Function 
End Class 
 

Class CChild Inherits CParent 
  Public Function Start() 
    DebugPrint("Start child") 
  End Function 
End Class 
 

Private Process Autoexec() 
  Var Parent As CParent 
  Var Child As CChild 

  Parent.Start() // Parent's own function is called 
  Parent.Stop() // Parent's own function is called 
  Child.Start() //  
  Child.Stop() // inherited function is called 
End Process
```

It prints to the debug window: 

```visualbasic
Start anything 
Stop anything 
Start child 
Stop anything
```

# Init

The Init function is used for the automatic initialization of a class. The class can contain this optional function. The Init function is automatically executed at the start of the system. Use the Init function to initialize the class. 

#### Example of automatic initializing a class variable 

```visualbasic
Class CRoom 
  Var Temperatures[6] As Long 

  Function Init() 
    Temperatures[1] := 20 
    Temperatures[2] := 16 
    Temperatures[3] := 22 
    Temperatures[4] := 23 
    Temperatures[5] := 19 
    Temperatures[6] := 18 
  End Function 
End Class
```

If a class inherits from another class, the parent's Init is automatically executed at first the child's Init is automatically executed then. 

#### Example 

```visualbasic
Class CParent 
  Function Init() 
    DebugPrint("Init Parent") 
  End Function 
End Class 
 

Class CChild Inherits CParent 
  Function Init() 
    DebugPrint("Init Child") 
  End Function 
End Class 
 

Private Process Autoexec() 
  Var Child As CChild 
End Process
```

It prints to the debug window: 

```visualbasic
Init Parent 
Init Child
```

# Assignment 

A class variable on a left side of an assignment operator must be the same type as a variable that is on the right side (Parent1 := Parent2) or it must be inherited from a class that is on the right side (Child := Parent). 

#### Example 

```visualbasic
Class CParent 
  // ... 
End Class 
 

Class CChild Inherits CParent 
  // ... 
End Class 
 

Private Process Autoexec() 
  Var Parent1 As CParent 
  Var Parent2 As CParent 
  Var Child As CChild 

  Parent1 := Parent2 // OK 
  Child := Parent1 // OK 

  // Parent1 := Child - assignment from a child to a parent is not allowed 
End Process
```

# Me

It is an address of a class instance. Instance of the class is a variable of the type class. 

#### Example 

```visualbasic
Class CPoint 
  Var x As Long 
  Var y As Long 

// ... 
  Public Function SetPoint(x As Long, y As Long) 
    Me.x := x 
    Me.y := y 
  End Function  
End Class
```