- This lesson will teach you the following concepts
- The classes
- The objects
- The members
- The methods
- Concept of inheritance
- Constructor and Destructor
- Data encapsulation
- Creating an object-oriented program
- Simple example
- Declaring a class
- Describing the methods
- Declaring and handling the objects
- UML diagram
This lesson will teach you the following concepts
- Concepts of Object-Oriented Programming.
- Examples of object declaration.
- Associated UML model.
Estimated time: 30 mn
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.
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 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.
A member is a data (or parameter) of the object.
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
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
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).
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.
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:
- Describe the class and the class members
- Specify all class methods.
- Declare the objects by associating them with a class ("instantiate a class").
- To illustrate these concepts, we will be using the training example named "WD Simple OOP".
- Open the training example named "WD Simple OOP".
- Run the test of this example.
- Click the different buttons corresponding to the animals in order to add them.
- 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:
- In the project explorer, select the "Classes" folder.
- Display the popup menu of this folder (right mouse click) and select "Create a class".
- 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.
- In the project explorer, select the "Classes" folder.
- Open the "Classes" folder (to do so, click the arrow found in front of the folder name).
- Double-click the cSavanna class.
- The class code is displayed in the code editor. The declaration code of the class is as follows:
In this code:
cSavanna is Class
// Name of Image control used for the drawing
m_sNameImageControlForDrawing is string
// List of savanna animals
m_arrTheAnimals is array of cAnimal dynamic
- "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:
- Right-click your class found in the project explorer.
- Select "New method" from the popup menu.
- In the window that is displayed, specify the method name and validate.
- Type the method code in the code editor.
- To display the AddAnimal method of cSavanna class:
- Click your class found in the project explorer.
- Click the little arrow found in front of the class name: the different class methods are displayed.
- 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!")
// Adds the animal to the list
// Draws the savanna
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
// Savanna object
gclTheSavanna is cSavanna(IMG_Landscape..FullName)
To refer to a member of "cSavanna" object, use the following syntax
- In our example, the object is handled:
- When initializing the window, to build the savanna.
// Draws the savanna
- 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)
We won't go into details about OOP in this tutorial.
The "WD Simple OOP" example is associated with an UML diagram.
- To display the UML diagram linked to the project:
- Double-click "UMLModel" in the project explorer:
- The UML diagram (class diagram) linked to the project is displayed.
- The different classes used by the "WD Simple OOP" project are found in this diagram.
|WINDEV allows you to create the 9 types of UML diagrams.|
To create a UML diagram:
- On the "Home" pane, in the "General" group, click "New".
- The window for creating a new element is displayed: click "Architecture" then "UML".
- 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).
Click [Add] to post a comment