OOP Example: Artificial Persons that Respond!

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
2
3
4
5
6
7
  TPerson = object
    public
      Name: string;
      GenderMale: Boolean;
      Age: byte;
      procedure WhoRU();
  end;

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
2
3
4
5
6
7
8
9
10
11
12
13
14
// This procedure will let the 'person'
// speak their identity (through writeln).
// The actual magic happens here...
procedure TPerson.WhoRU;
var
  Gender: string;
begin
  if GenderMale then
    Gender := 'Male'
  else
    Gender := 'Female';

  WriteLn('I am ', Name, '. I am ', Age, ' year-old ', Gender);
end;

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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
var
  // we declare our test 'persons'
  John, Peter, Lily, Rachel: TPerson;

We use some random names. Now just under the main begin block enter:

  // we input the person information
  with John do begin
    Name:='John Hopkins';
    Age:=14;
    GenderMale:=True;
  end;

  with Peter do begin
    Name:='Peter Parker';
    Age:=19;
    GenderMale:=True;
  end;

  with Lily do begin
    Name:='Lily Allen';
    Age:=23;
    GenderMale:=False;
  end;

  with Rachel do begin
    Name:='Rachel Adams';
    Age:=27;
    GenderMale:=False;
  end;

We just entered some information for the instances. You could customize it as you see fit.

Now we want our objects to talk!

1
2
3
John.WhoRU();

Readln; // to keep the dos window from not closing.

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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
program object_persons;

{$mode objfpc}{$H+}

uses
  {$IFDEF UNIX}{$IFDEF UseCThreads}
  cthreads,
  {$ENDIF}{$ENDIF}
  Classes;

type

  { TPerson }

  TPerson = object
    public
      Name: string;
      GenderMale: Boolean;
      Age: byte;
      procedure WhoRU();
  end;

{ TPerson }

// This procedure will let the 'person'
// speak their identity (through writeln).
// The actual magic happens here...
procedure TPerson.WhoRU;
var
  Gender: string;
begin
  if GenderMale then
    Gender := 'Male'
  else
    Gender := 'Female';

  WriteLn('I am ', Name, '. I am ', Age, ' year-old ', Gender);
end;

var
  // we declare our test 'persons'
  John, Peter, Lily, Rachel: TPerson;

begin
  // we input the person information
  with John do begin
    Name:='John Hopkins';
    Age:=14;
    GenderMale:=True;
  end;

  with Peter do begin
    Name:='Peter Parker';
    Age:=19;
    GenderMale:=True;
  end;

  with Lily do begin
    Name:='Lily Allen';
    Age:=23;
    GenderMale:=False;
  end;

  with Rachel do begin
    Name:='Rachel Adams';
    Age:=27;
    GenderMale:=False;
  end;

  // We just entered the information
  // Now we want our object to talk!
  // And we won't help the classes by writing messages
  // ourselves.
  // They'll just respond and will create the message
  // themselves!
  // That's the beauty of classes!

  // Now, we ask John...
  John.WhoRU();

  // Try with Rachel, Lily and Peter as well

  ReadLn;
end.

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: