Object-oriented programming in G Dataflow uses concepts that are common in object-oriented languages, such as C++ and Java. Following object-oriented programming practices may not be necessary if you want to create a simple program with one or two VIs. However, using classes can improve the development of large and complex projects.
- Class—A collection of fields and methods that can access and manipulate the values the fields contain.
- Class Object—An instance of a class with a specific set of data values for the fields the class G Type document defines. A class object also has access to every method in the Member List of the class G Type document.
- Encapsulation—The concept of a class being a single unit made up of class fields and the methods that can access those class field values.
- Field—A data attribute of a class. Every instance of a class has its own instance of every field. A field is always private so only VIs that are members of the same class can access that field. Use the Data view of the class G Type document to define the fields of a class.
- Inheritance—a hierarchy of parent and child classes that allows you to use code from an existing class in your project as a starting point to build a, more specific class for the project.
- Method—A segment of code that can interact with the class data or use class data to perform an operation. Use the Member List of the Class (G Type) document to define the methods of a class.
- Object-Oriented Design—Planning the classes you need for your program, the methods each class should contain, and how the classes should interact before you begin writing code.
- Object-Oriented Programming—Developing code in a programming language that allows the user to adhere to object-oriented principles.
As an example of how these concepts work together, consider cars on a road. There are many car manufacturers, but all cars share several generic fields, such as the number of doors or current speed, that you can use to describe every car you see. Each car also has a set of common methods that enable it to function using the fields that define the car. When you accelerate, brake, or use a turn signal, the car implements a method to change the state of the car.
The encapsulation of these generic fields and methods into a single unit defines the car class. Each car on the road is an object of the car class with a specific set of fields and methods that the car can perform.
Using classes to create an object-oriented program can provide the following benefits.
Prevent changes you make to one class method from causing errors in other class methods by separating code into small, maintainable VIs.
Find the source of an error quickly by defining the access to data and methods of a class.
Reuse stable code in an existing class to create additional classes that handle more specific situations and functions by specifying inheritance relationships between classes.