Today we create bionic, robotic, futuristic human beings! That can even respond to your question! A virtual TPerson to be precise!
I hope you’ve read the Classes article that I posted earlier. It is fun working with Classes and objects and stuff. We can give life to our programs! That’s very cool.
If you have missed the article I strongly recommend you to read it, as example on this article is based on that article.
Today I was thinking more of an example of virtual persons. We store data in computers, right? We keep data of persons. But what will happen if we could get to create virtual instances of those persons from those data? What if those “virtual persons” could respond? That would be cooler! So let’s try that-
Start Lazarus.
Go ahead and create an Application Project (Project -> New Project -> Program -> OK).
After the uses
clause, enter the following code:
1 | TPerson = object |
Through this declaration, we create a structure for our virtual persons. We could’ve added a lot more information to our little persons. But we keep them simple for now. Name
, GenderMale
and Age
variables will hold our data. But our magic will come from the WhoRU()
procedure.
Now take you cursor inside the object declaration and press Ctrl+Shift+C. It will create the TPerson.WhoRU
procedure code automatically. Now you can enter the code:
1 | // This procedure will let the 'person' |
When writing this procedure keep in mind what kind of data can you encounter. Remember that every instance of TPerson
will have this same function running inside them. Just write accordingly. For example, you could encounter an instance GenderMale
of False
or True
. So I wrote code to generate the message accordingly.
So basically what it does is it will take information from itself (such as Name
, Age
etc.) and spit out a message according to them. So the message would be something like:
1 | 'I am {Name}. I am {Age} year-old {Gender}' |
So imagine if you just wrote this class and sent it to a friend. He can just include it in his code and use it with any data in the world and it would reply the message perfectly. That’s why OOP is fun. It is flexible and way more usable.
Now let’s test it. Declare some variable:
1 | var |
We just entered some information for the instances. You could customize it as you see fit.
Now we want our objects to talk!
1 | John.WhoRU(); |
Now Run the project (F9 or Run -> Run).
In the screen you will see “John” saying his identity. If you could Post not found: let-your-software-speak implement a TTS in this object, you will be amazed at what this simple code could do! (The objects would really speak through the speakers, instead of just writeln
.)
Now let’s see the full code:
1 | program object_persons; |
If you want to experiment, go ahead! But remember {$mode objfpc}
compiler directive is required to use OOP in your code. (See above code for usage.)
If you have any questions, feel free to ask in the comments. ;-)
Downloads
You can download the source code for the tutorial project and executable/EXE files from the links below: