- In this lesson you will learn the following concepts
- A simple example
- Creating the class
- Declaring and manipulating objects
- Using the constructor
In this lesson you will learn the following concepts
- Concepts of Object-Oriented Programming.
- Examples of object declaration.
Estimated time: 40 mn
Object-Oriented Programming (OOP) is designed for better code reusability. Programs developed in OOP are structured: they are divided into modules, and each module manages a functionality. These modules can easily be reused in other software. They group a set of procedures (called methods) and encapsulate the data structure on which the methods will act.
To use object-oriented programming, you must declare classes, members and associated methods.
First, let's take a look at the main concepts of OOP.
contains the description of a data structure (the members) and the procedures (methods) that handle the members.
Therefore, a class defines a type of data and its behavior.
A class is used to create objects. Each created object owns the members described in its class and it can be handled via the methods of its class. An object is defined as an instance of the class.
When the class is declared, all you have to do is associate an object with a class in order for the object to be handled by all methods of this class.
A member is a piece of data (or parameter) of the object.
A method is used to act on the object, to modify its members for example.
A method is a procedure. Its operating mode is similar to the one of standard WLanguage procedures.
Inheritance makes it possible to include the characteristics of an existing class (base class) into a new class (derived class). Inheritance allows you to create a new data type from a known type in order to add features or to modify its behavior. Therefore, the base class will not be modified. A class can inherit from a class: it becomes a sub-class of this class.
Objects from a derived class can access all methods, members and properties of ancestor classes. It is just as if these methods, members and properties were part of the derived class.
Constructor and Destructor
Constructor and Destructor are two important concepts since they make it possible to automatically call initialization methods when the object that represents the class is created (instantiated) or destroyed (freed). These methods are automatically created when the class is created. By default, the code of these methods is empty.
- The Constructor method of the class is automatically called when an object of the same type as this class is declared (instantiated).
- The Destructor method of the class is automatically called when an object of the same type as this class is destroyed (freed). The execution of the Destructor method depends on the life span of the object according to how it was declared (global or local),
Data encapsulation ensures that the data belonging to the object is not accidentally modified by functions (methods) external to the object.
This allows you to prevent the user of an object from accessing some or all of its members. The members whose access is not allowed are called private members.
These private members can only be accessed from the methods designed for this purpose in the class.
Creating the class
We are going to create a class that represents a product and contains members and methods. This class will be instantiated and used by a procedure.
To perform the actions in this lesson, we will work on the "WLanguage" project that we used in the previous lesson.
- First we will create the CProduct class:
- On the "Home" pane, in the "General" group, click "New".
- In the window that appears, click "Code", then "Class".
- Specify the name of the class (CProduct in our example).
- Validate. The WLanguage code of the class is displayed. It consists of three sections:
- Type the following code in the "Declaration" process:
This declaration code contains:
CProduct is Class
mg_nStockWarningThreshold is int = 20
m_sReference is string
m_sDescription is string
m_nStock is int
// Stock status
EStockStatus is Enumeration
- The class declaration. This class contains:
- a public member that corresponds to the stock warning threshold. This member is public because this information is the same for all products.
- three private members: the product reference, its description and its quantity in stock.
- The declaration of an enumeration. This enumeration makes it possible to manage the stock status. This enumeration must be declared in the class declaration code so that it can used by one of the methods of the class.
- Save the class (Ctrl + S).
- To use private members from outside the class, they must be associated to properties (Getter and Setter). We will create these properties.
- To create the properties associated with the "m_sReference" member:
- In the "Project explorer" pane (to the left of the environment), expand "Classes" and expand "CProduct". The different elements of the class appear in the "Project explorer" pane.
- Position the cursor on "m_sReference".
- Open the context menu (right click) and select "Generate property". The window that appears allows you to define all the characteristics of the property. Write "Reference" for the name of the property and validate.
- Properties for reading and writing are automatically created with the corresponding WLanguage code.
- Repeat these steps to generate:
- the Description property associated with m_sDescription,
- the Stock property associated with m_nStock.
- We will now create the methods associated with the class. In our example, we will create 2 methods:
- A method for adding products to the current stock: AddStock.
- A method to know the status of the stock: StatusOfStock.
- To create the AddStock method:
- In the "Project explorer" pane, select the CProduct class.
- Open the context menu (right click) and select "New method". The window that appears allows you to define all the characteristics of the method. Write "AddStock" for the name of the method and validate.
|The syntax is the same for class methods and procedures. |
- The code editor shows the method. Type the following code:
Let's take a look at this WLanguage code:
PROCEDURE AddStock(LOCAL nAdditionalStock is int): boolean
// The stock passed as parameter must be valid
IF nAdditionalStock < 0 THEN
// Cannot add negative stock
// Increase current stock with additional stock
m_nStock += nAdditionalStock
// Added successfully
- The AddStock method expects an integer as parameter, an returns a boolean.
- The stock value is evaluated. If the stock is negative, the method returns False: the stock is not updated.
- If the value is positive, the stock is incremented.
- You can repeat these actions to create the StatusOfStock method. The code of this method is as follows:
This code uses the EStockStatus enumeration from the class declaration code.
PROCEDURE StatusOfStock(): EStockStatus
// If the stock is empty
IF m_nStock = 0 THEN
// If stock quantity is below warning threshold
IF m_nStock <= mg_nStockWarningThreshold THEN
// Stock is OK
- Our class is complete. Now, we will use it.
Declaring and manipulating objects
Objects are declared at the same time as the other variables. For our example, we will declare an object in the project initialization code.
- To open the project initialization code:
- Right-click the "P" button next to the open element tabs. The context menu appears.
- Select "Element code".
- The code editor displays the different events associated with the project.
- Clear the initialization process and write the following code:
gclAProduct is CProduct
gclAProduct.Reference = "REF-123"
gclAProduct.Description = "My product"
This code declares and instatiates an object. Values are assigned to properties via the following syntax:
<Object name>.<Property name> = Value
- Add the following lines to this code:
- Call to the AddStock method:
- Reading properties:
Trace(gclAProduct.Reference +" "+ gclAProduct.Description +" "+ gclAProduct.Stock)
- Use the enumeration:
IF gclAProduct.StatusOfStock() = CProduct.STOCK_OK THEN
Trace("Enough units in stock")
Trace("Not enough units in stock")
- Let's test this code: click among the quick access buttons.
- If necessary, open the "Debugger trace" pane to see the result: On the "Home" pane, in the "Environment" group, expand "Panes" and select "Debugger trace".
- The "Debugger trace" pane shows the following data:
We see the product details and the stock status.
REF-123 My product 500
Enough units in stock
Using the constructor
To finish this quick overview of OOP with WINDEV, we will modify our code to use the constructor. This constructor will allow us to initialize the warning threshold.
- Open the class code: double-click CProduct in the "Project explorer" pane, for example.
- Modify the class declaration. The code line:
mg_nStockWarningThreshold is int = 20
mg_nStockWarningThreshold is int
- Modify the class constructor.
PROCEDURE Constructor(nStockWarningThreshold is int)
mg_nStockWarningThreshold = nStockWarningThreshold
- Go to the project initialization code.
- We will modify:
- the class instantiation. The following code:
As already mentioned, the Constructor method is automatically called when the class is declared (instantiated). Now the constructor expects a parameter: we must pass the parameter value in the instantiation. In this example, the maximum quantity is set to 20.
gclAProduct is CProduct(20)
- the call to the method. The following code:
The quantity in stock is 10.
- Let's test our modifications (Click the "GO" icon among the quick access icons).
- The "Debugger trace" pane shows the following data:
REF-123 My product 10
Not enough units in stock
In this lesson, you have discovered how to create a class, its members, and how to use them.
The following lesson will present a few tips for using the code editor.
This page is also available for…
Click [Add] to post a comment