PC SOFT

ONLINE HELP
FOR WINDEV, WEBDEV AND WINDEV MOBILE

Home | Sign in | English US
New WINDEV 24 feature!
  • This lesson will teach you the following concepts
  • Concepts
  • The classes
  • The objects
  • The members
  • The methods
  • Concept of inheritance
  • Constructor and Destructor
  • Data encapsulation
  • Example
  • Creating an object-oriented program
  • Simple example
  • Declaring a class
  • Describing the methods
  • Declaring and handling the objects
  • UML diagram
Lesson 7.9. The OOP
This lesson will teach you the following concepts
  • Concepts of Object-Oriented Programming.
  • Examples of object declaration.
  • Associated UML model.

Estimated time: 30 mn
Previous LessonTable of contentsNext Lesson
Concepts
The Object-Oriented Programming (OOP) is designed for better code reusability. The programs developed in OOP are structured: they include modules, each one managing a software feature. These modules can easily be re-used in other software. They group a set of procedures (called methods) and they encapsulate the data structure on which the methods will act.
To use the object-oriented programming, you must declare the classes, the members and the associated methods.

The classes

A class 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.

The objects

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 a class instance.
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.

The members

A member is a data (or parameter) of the object.

The methods

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.

Concept of inheritance

The inheritance is used to include the characteristics of an existing class (base class) into a new class (derived class). The inheritance allows you to create a new data type from a known type in order to add features to it 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.
The objects found in a derived class can access all methods, all members and all properties of ancestor classes ; it is as if the methods, the members and the properties of ancestor classes were part of the derived class.

Constructor and Destructor

The notions of Constructor and Destructor are important because they allow for an automatic call to initialization methods when creating an object and when destroying it.
The Constructor method associated with a class is automatically called when declaring a class object.
The Destructor method associated with a class is automatically called when deleting the object (exit from the procedure where the object was declared).

Data encapsulation

The data encapsulation is used to ensure 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 forbidden are called private members.
These private members can only be accessed from the methods designed for this purpose in the class.

Example

Let's take a simple example to apply these concepts:
  • Let's consider the PERSON class.
  • Florence is an instance of PERSON class.
  • The last name, first name and date of birth can be members of PERSON class.
  • The Age() method can be a method of PERSON class. It would calculate the age according to the "date of birth" member and according to todays' date (returned by DateSys).
  • Inheritance: A contact can be either a person, or a company.
    • PERSON could be a class derived from CONTACT.
    • COMPANY could be a class derived from CONTACT.

Creating an object-oriented program

To create an object-oriented program in WLanguage, you must:
  1. Describe the class and the class members
  2. Specify all class methods.
  3. Declare the objects by associating them with a class ("instantiate a class").
Simple example
  • To illustrate these concepts, we will be using the training example named "WD Simple OOP".
    1. Open the training example named "WD Simple OOP".
    2. Run the test of this example.
    3. Click the different buttons corresponding to the animals in order to add them.
    4. Stop the example test to go back to the editor.Note: We won't go into details about the syntax of OOP but we will present a simple example of an object-oriented program.

See Classes, members, methods and properties for more details.

Declaring a class

  • WINDEV allows you to easily declare the classes from the project explorer. To create a class:
    1. In the project explorer, select the "Classes" folder.
    2. Display the popup menu of this folder (right mouse click) and select "Create a class".
    3. In the window that is displayed, specify the class name (TestTUT for example) and validate.
  • Study the code of cSavanna class used in the example.
    1. In the project explorer, select the "Classes" folder.
    2. Open the "Classes" folder (to do so, click the arrow found in front of the folder name).
    3. Double-click the cSavanna class.
    4. The class code is displayed in the code editor. The declaration code of the class is as follows:
      cSavanna is Class
       
      PROTECTED
      // Name of Image control used for the drawing
      m_sNameImageControlForDrawing is string
      // List of savanna animals
      m_arrTheAnimals is array of cAnimal dynamic
      END
      In this code:
      • "cSavanna" is the class name.
      • "m_sNameImageControlForDrawing" and "m_arrTheAnimals" are class members.
      • The "PROTECTED" keyword is used to specify that these members can only be handled from a code of the class or from a code of a derived class.

Describing the methods

  • WINDEV allows you to easily declare the classes from the project explorer. To create a method:
    1. Right-click your class found in the project explorer.
    2. Select "New method" from the popup menu.
    3. In the window that is displayed, specify the method name and validate.
    4. Type the method code in the code editor.
  • To display the AddAnimal method of cSavanna class:
    1. Click your class found in the project explorer.
    2. Click the little arrow found in front of the class name: the different class methods are displayed.
    3. Double-click the method name:
      PROCEDURE AddAnimal(pclAnAnimal is cAnimal dynamic)
       
      // No more than 5 animals
      IF m_arrTheAnimals..Count = 5 THEN
      Error("No more than 5 animals!")
      RESULT False
      END
       
      // Adds the animal to the list
      Add(m_arrTheAnimals, pclAnAnimal)
       
      // Draws the savanna
      DrawSavanna()
       
      RESULT True

Declaring and handling the objects

In the window processes, an object is declared at the same time as the other variables:
// Global declarations of WIN_OOP
PROCEDURE WIN_OOP()
 
// Savanna object
gclTheSavanna is cSavanna(IMG_Landscape..FullName)
To refer to a member of "cSavanna" object, use the following syntax
<ObjectName>.<member name>
  • In our example, the object is handled:
    • When initializing the window, to build the savanna.
      // Draws the savanna
      gclTheSavanna.DrawSavanna()
    • When creating an animal, to position the animal in the savanna.
      // Declares a new elephant
      clElephant is cElephant("Elephant "+ GetIdentifier(), 13)
       
      // Adds the elephant into the savanna (this action refreshes the drawing)
      IF gclTheSavanna.AddAnimal(clElephant) THEN
      // End message
      ToastDisplay("Animal added", toastShort, vaMiddle, haCenter)
       
      END
We won't go into details about OOP in this tutorial.
UML diagram
The "WD Simple OOP" example is associated with an UML diagram.
  • To display the UML diagram linked to the project:
    1. Double-click "UMLModel" in the project explorer:
    2. The UML diagram (class diagram) linked to the project is displayed.
    3. The different classes used by the "WD Simple OOP" project are found in this diagram.

Note

WINDEV allows you to create the 9 types of UML diagrams.
To create a UML diagram:
  1. On the "Home" pane, in the "General" group, click "New".
  2. The window for creating a new element is displayed: click "Architecture" then "UML".
  3. The wizard for creating a UML model starts, allowing you to choose the model to create:
We won't go into details about the use of UML language with WINDEV.
See The UML model for more details).
Previous LessonTable of contentsNext Lesson
Minimum required version
  • Version 24
Comments
Click [Add] to post a comment