Professional Documents
Culture Documents
LECTURE On COM 412
LECTURE On COM 412
TAUGHT/LEVEL
COM 412 OBJECT-ORIENTED COMPUTER
PASCAL SCIENCE/HND II
Ezeife, Joy------Course Lecturer
CONTINUATION ON
OBJECTS
Let us review what we discussed earlier as that will usher us into the next topic. In
my last lecture we treated Pascal objects where we saw how to define them and the
three visibility levels of objects...I hope that you can still remember. To follow me,
you need to keep your textbook handy.
We equally discussed constructors and destructors. What are they? Can you
remember?
If you look at the codes critically, you will see that Table Top object inherited all
the members(methods-procedures and functions) of the parent object.See the
explanation on page 64.
The following are some points which should be taken note of:
2.Table Top has an additional method(that shows that in inheritance, the child
object can have new methods).
3. Implicit instance called self was also used to depict the current instance(see this
on the first procedure on page 64).
Now instance means a given occurrence of an object such as the values of the
fields ----something like
length =4, width=7.....so self depicts that in Pascal objects. You can see that on that
first procedure on page 64.
Now look at that main program part of the program on page 64, you will notice
that length is 3 while width is 4.
The output equally shows the value of the instances......the value of a given
Rectangle dimension.
Remember that there is a control structure on the second procedure on that same
page. I mean the section having
Hence various lengths and widths (various instances) can be drawn using the Draw
method.
EXERCISES
4. What is inheritance?
ASSIGNMENT
Classes are similar to objects but they are pointers to the object and not the object
itself.
You remember in class when I wrote Tbutton , Tlabel and soon. This means that
each button object on the form belong to a given class----class Tbutton. Now ,
button 1, button 2,button 3 etc are objects.
Another difference between objects and classes is that classes are allocated on the
heap of a program. Which are is the heap? This is area of memory not managed by
the CPU- a more free-floating region of memory.
Heap is accessed using pointers, hence slower for reading and writing but heap
variables are global.
once.
2) Object allocates memory when it is created. Class doesn't allocate memory when
it is created.
However, objects are allocated on the stack (a special region of memory that stores
temporary variables created by each function).
Stack is automatically managed by the CPU and is also LIFO (Data Structure).
On page 65, you will see the syntax for defining Pascal classes The type
declaration is used. I hope that you can see the syntax---that is type class-
identifier=class. Then after this, we have the private and public declaration part.
The private part contains fields while the public part contains constructors,
procedures and functions. After that we see the variable declaration part. Note the
points of explanations under.
1. Where the class definition is placed (what is the answer?).
Class definition: under the type declaration part of the program only
2: class keyword
3. Fields are data items that exist in each instance of the class
A class is declared in the same way as an object, using the type declaration.
Now let us see how the class members are accessed. Visibility of class members
unlike pascal objects are 5 levels.
c.Strict private; Only methods of the class that defined them can access them. The
difference between this type and the private visibility is that other methods or
classes such as descendant classes cannot access them even if they are in the same
unit.
d. Protected visibility:In this type, any method or class in the same unit can access
the members including descendant classes.
Recall the usage of constructors from our discussion of Pascal objects. It is notable
to remember that we still create the constructor at the time of object creation.In the
example program on page 69, a constructor is created for a class called Books at
the time of object creation.
Examine the program and see the two directives for creating classes and for using
class constructors and destructors. Note that the procedure "set" and the function
"get" were also used.The program creates the titles and prices for several books in
the books class such as Physics, Chemisty and Maths. Note that the procedures are
for setting the titles and prices and functions for retrieving the values.Title is string
data type while price is real.
The string queue on page 49 will serve as a parent class as we create a new class
called an integer queue class as a child class. It will inherit the methods of string
queue and have additional methods.
To achieve this, a new unit is created and the string queue is added in its USES
clause.
Hence, TIntqueue inherits from Tqueue. Also, on page 74 is a new class, the Novel
class created from the Books class seen on page 69 which also inherits the methods
of the Books class.Note the inherited keyword of Novels.create constructor, the
virtual method of Books.Display procedure, the protected visibility of Books class
and the overload operators of the two constructors.
Let us see some other terms in Pascal such as interfaces and abstract classes.
What are interfaces? They are methods that can be implemented by a class.They
can be seen as skeletons of a program to be implemented later on.
All defined interfaces such as procedures and functions ,etc should be implemented
in a program to avoid error.
Abstract classes:These are also called existential classes. They cannot be
instantiated( the data members such as the methods and fields cannot have
different parameters) but can only be inherited.The inherited methods must have
the same visibility. Note the keyword ABSTRACT in the syntax on page 77.
Static keyword: These class members do not need to be accessed with instantiation.
If a member such as a constant is defined with the keyword "static" then it cannot
be accessed with an instantiated class.However, a static data member (fields or
methods) can be accessed by an instatiated object.
Now take note of this explanation: Classes must be created using one of their
constructors (there can be multiple constructors). Remember that a class is a
pointer to an object on the heap. When a variable of some class is declared, the
compiler just allocates room for this pointer, not the entire object. The constructor
of a class returns a pointer to an initialized instance of the object on the heap. So,
to initialize an instance of some class, one would do the following :
EXERCISES
ASSIGNMENT
You have been using units in creating your pascal programs. This short chapter is
simply highlighting the uniqueness of pascal from some other languages----the
presence of units.
What are unit? Simply explained, they are modules of a Pascal program.It is made
up of blocks that contain variables,type declarations,statements,methods and so
on.There are built in units and user-defined units. The former have default meaning
or function in the compiler while the later are named or created by the
programmer. The USES clause are used to include the two types of units in Pascal
programs.
One common built in unit is the CRT(from the word cathode ray tube) used to
denote that certain things will be displayed on the screen.The keyword Unit is
written followed by the name of the unit. Note that the main unit's name is also the
name of the file. That is ,take for instance,Unit FindAverage should be stored in
the the file FindAverage .pas.
a. Unit name
You may run those programs discussed in your compiler in your own interest and
ask your sincere questions before the group is locked against messages later in the
day.
I hope that you are not whiling away your time in this lock down. Be intentional in
all you do for that is the secret of excellence. The heights which great men reached
and kept were not attained by sudden flights but they, while their companions slept
were toiling through the night.
Today, we are going to discuss Pascal program basics. There are many components
of a Pascal program such as the following:
As we discuss them, please keep your textbooks handy for better understanding
and also take note of the various syntax on your book.
d. Case sensitivity-In Pascal, any case can be used-both capital or small letters are
allowed. var is the same as Var.
e.Pascal statements-These specify specific jobs such as
declaration.assignment,reading of data, logical decisions or program flow control
transfer.Examples are: title:=P, price:=T
Examples are seen on page 84 such as Else, Const, Var and so on.
Let us now see the data types we have in Pascal. They are classified into three:
scalar, pointer and structured
Scalar types are the most common data types, such as integer, real, boolean,
character,subrange and enumerated types. We shall see them one after another.
Structured types are combination of some scalar types. Examples are arrays,
records, files and sets.
Pointer types make use of pointers which are dynamic variables whose value is the
address of another variable in memory---hence the name pointer.
Did you encounter data types in other languages which you have studied? Can you
name some and the data types they allow?
So, most of the ones we have in Pascal are also seen in the languages you have
studied and should not sound strange.
Data types make use of type declarations---these are used to declare the data type
of each identifier. Note the syntax in your book Type-identifier 1, type identifier
2,...=type. Examples are shown on page 85.
Let us discuss the types one after another.
a. Integer-These are whole numbers. They have no decimals. There are various
standard integer types too according to their range of sizes such as cardinal,
Integer, shortint, smallint and others as shown on the table on page 86. Their
respective types,minimum value, maximum value and format are also shown.
It uses only assignment operators and relational operators .Operators are treated on
pages 94-99 as we shall see later.
From the syntax of enumerated type(don't forget to note the various syntax as I
earlier informed you),we see that under the type declaration part of a program, we
write a given identifier for the enumerated type and list all the items in it.
d.Subranges or subrange types-From the name, we see that the values of a variable
lie within a given range. Eg.VAR Age:21-55, we notice that there is a lower unit
(21) and an upper limit(55).
[4:09 AM, 5/20/2020] Joy: Note the syntax. Also in the declaration example on
page 87, we see that from the numbers 1-100, we have created a subrange :18---90
representing P---Q(P is 18,that is the lower limit while q is 90,the upper limit).
Apart from character, integer, real, boolean, enumerated , subrange and string,there
are other data types such pointer, array,records,,sets and files.
What is the different between a variable declaration and a type declaration? The
former shows the type of values a variable may have and the variable name also
shows the memory location in which the variable will be stored. However, type
declaration is only specifying a given data type of the variable, no emphasis on the
actual memory reservation (as seen in variable declaration).
EXERCISES
ASSIGNMENT