PC SOFT

ONLINE HELP
 WINDEVWEBDEV AND WINDEV MOBILE

Home | Sign in | English EN
This page concerns the 270056D version of WINDEV 27. Update your version.
  • In this lesson you will learn the following concepts
  • Concepts
  • OOP concepts
  • A simple example
  • Creating the class
  • Declaring and manipulating objects
  • Using the constructor
  • Conclusion
Lesson 2.8. OOP
In this lesson you will learn the following concepts
  • Concepts of Object-Oriented Programming.
  • Examples of object declaration.
Lesson duration

Estimated time: 40 mn
Previous LessonTable of contentsNext Lesson
Concepts
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.

OOP concepts

Class
A class contains the description of a data structure (members) and the procedures (methods) that handle the members.
Therefore, a class defines a type of data and its behavior.
Object
A class is used to create objects. Each created object owns the members described in its class and can be manipulated via the methods of its class. An object is an instance of a class.
When a class is declared, an object must be associated with the class so it can be manipulated by all the methods of this class.
Members
A member is a piece of data (or parameter) of the object.
Methods
A method is used to act on the object, to modify its members for example.
A method is a procedure. It works like a standard WLanguage procedure.
Inheritance
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 and become a subclass.
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 (deleted). 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 (deleted). 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
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.
A simple example

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 complete the exercises in this lesson, we will work on the "WLanguage" project that we used in the previous lesson.
  • First we will create the CProduct class:
    1. On the "Home" tab, in the "General" group, click "New".
    2. In the window that appears, click "Code", then "Class".
    3. Specify the name of the class (CProduct in our example).
    4. Validate. The WLanguage code of the class is displayed. It consists of three sections:
      • Declaration
      • Constructor
      • Destructor
    5. Enter the following code in the "Declaration" event:
      CProduct is Class
       
      PUBLIC
      mg_StockWarningThreshold is int = 20
       
       PRIVATE
      m_Reference is string
      m_Description is string
      m_Stock is int
      END
       
      // Stock status
      StockStatus is Enumeration
      STOCK_EMPTY
      STOCK_WARNING
      STOCK_OK
      END
      This declaration code contains:
      • 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.
    6. 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_Reference" member:
    1. 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.
    2. Select the "m_Reference" member.
    3. Right-click to open the context menu 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.
    4. Properties for reading and writing are automatically created with the corresponding WLanguage code.
  • Repeat these steps to generate:
    • the Description property associated with the m_Description member,
    • the Stock property associated with the m_Stock member.
  • 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:
    1. In the "Project explorer" pane, select the CProduct class.
    2. Right-click to open the context menu and select "New method". The window that appears allows you to define all the characteristics of the method. Name the method "AddStock" and validate.

      Note

      The syntax is the same for class methods and procedures.
    3. The code editor shows the method. Type the following code:
      PROCEDURE AddStock(LOCAL AdditionalStock is int): boolean
       
      // The stock passed as parameter must be valid
      IF AdditionalStock < 0 THEN
      // Cannot add negative stock
      RESULT False
      END
       
      // Increase current stock with additional stock
      m_Stock += AdditionalStock
       
      // Added successfully
      RESULT True
      Let's take a look at this WLanguage code:
      • 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:
    PROCEDURE StatusOfStock(): StockStatus
     
    // If the stock is empty
    IF m_Stock = 0 THEN
    RESULT STOCK_EMPTY
    END
     
    // If stock quantity is below warning threshold
    IF m_Stock <= mg_StockWarningThreshold THEN
    RESULT STOCK_WARNING
    END
     
    // Stock is OK
    RESULT STOCK_OK
    This code uses the StockStatus enumeration from the class declaration code.
  • 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:
    1. Right-click the "P" button next to the open element tabs. The context menu appears.
    2. Select "Element code".
    3. The code editor displays the different events associated with the project.
    4. Clear the initialization process and write the following code:
      AProduct is CProduct
       
      AProduct.Reference = "REF-123"
      AProduct.Description = "My product"

      This code declares and instantiates an object. Values are assigned to properties via the following syntax:
      <Object name>.<Property name> = Value
    5. Add the following lines to this code:
      • Call to the AddStock method:
        AProduct.AddStock(500)
      • Reading properties:
        Trace(AProduct.Reference +" "+ AProduct.Description +" "+ AProduct.Stock)
      • Enumeration:
        IF AProduct.StatusOfStock() = CProduct.STOCK_OK THEN
        Trace("Enough units in stock")
        ELSE
        Trace("Not enough units in stock")
        END
    6. Let's test this code: click Test project in the quick access buttons.
    7. If necessary, open the "Debugger trace" pane to see the result: On the "Home" tab, in the "Environment" group, expand "Panes" and select "Debugger trace".
    8. The "Debugger trace" pane shows the following data:
      REF-123 My product 500
      Enough units in stock
      We see the product details and the stock status.

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 by replacing the following line of code:
    mg_StockWarningThreshold is int = 20
    with
    mg_StockWarningThreshold is int
  • Modify the class constructor.
    PROCEDURE Constructor(StockWarningThreshold is int)
    mg_StockWarningThreshold = StockWarningThreshold
  • Go to the project initialization code.
    1. We will modify:
      • the class instantiation. Replace the following line of code:
        AProduct is CProduct
        with:
        AProduct is CProduct(20)
        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.
      • the call to the method. Replace the following line of code:
        AProduct.AddStock(500)
        with:
        AProduct.AddStock(10)
        The quantity in stock is 10.
    2. Let's test our changes (Click "GO" in the quick access icons).
    3. The "Debugger trace" pane shows the following data:
      REF-123 My product 10
      Not enough units in stock
Conclusion
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.
Previous LessonTable of contentsNext Lesson
Minimum version required
  • Version 27
Comments
Click [Add] to post a comment