Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 12

COURSE CODE COURSE TITLE DEPARTMENT

TAUGHT/LEVEL
COM 412 OBJECT-ORIENTED COMPUTER
PASCAL SCIENCE/HND II
Ezeife, Joy------Course Lecturer

COURSE DESCRIPTION: This is the continuation of the course Object-


Oriented Pascal. It is very necessary that you keep the textbook,”Object-Oriented
Pascal handy in order to understand the e-learning process. Exercises and
assignments have been included to enable appraisal of the entire process. You can
contact me, the course lecturer on 08061126745.

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.

That’s the public, private and protected visibility levels

We equally discussed constructors and destructors. What are they? Can you
remember?

Special type of methods that are called automatically whenever an object is


created. It has the method name-Init

Destructors: methods called during the destruction of an object

Constructor are special objects called automatically when an object is created

Destructors are called during the destruction of an object

Lest you forget, destruction is deallocation.


Recall inheritance for Pascal objects too where we compared the parent object -that
Rectangle object and the child object-that Table Top object on page 60

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:

1.Table Top inherited all the members of the Rectangle object(however in


inheritance, it is not a must that everything, that is every method must be
inherited).

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

for i:=1 to length do and for i:=1 to width do

Hence various lengths and widths (various instances) can be drawn using the Draw
method.
EXERCISES

1. Discuss the components of a Pascal object.

2. Discuss the visibility levels of Pascal objects.

3. How can Pascal objects be defined?

4. What is inheritance?

ASSIGNMENT

Write and execute a program to show inheritance.


CLASSES

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.

There are differences existing between objects and classes.

I have already given you one difference.

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.

An object is an instance of a class.

Heap is accessed using pointers, hence slower for reading and writing but heap
variables are global.

1) Object is created many times as per requirement. Class is declared

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?).

2. The keyword for defining class (what is it?).

3. Fields in each instance of a class.

4. The methods such as procedures and functions.

5. The presence of the constructor,"create".

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.

Methods are declared within the definitions of a class.

Now let us see how the class members are accessed. Visibility of class members
unlike pascal objects are 5 levels.

a. Public: These members(procedures,functions and other methods) can be


accessed by any part of the program.

b. Private:Only the units or program module(section/subprogram)that contains the


class definition can access these members.Hence other methods that are in the
same unit can access them.

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.

e. Published; This visibility is used with a constructor only hence differentiates it


from a public which may not have a constructor. Also, the compiler generates
information for the usage of the classes.
Let us now discuss Constructors and destructors for Pascal 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.

Let us now discuss Inheritance in Pascal classes;

Note the syntax: type child class-identifier=class(base class-identifier) <member>


end;

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 :

ClassVar := ClassType.ConstructorName; Hence class instantiation redirects to


object instantiation.

EXERCISES

1. What is a Pascal class?

2. Differentiate between Pascal classes and Pascal objects.

3. Explain the terms constructors and destructors.

4. What is inheritance in Pascal classes?

5. Discuss the visibility levels of Pascal classes.

ASSIGNMENT

Run the program on page 74 of your text to show inheritance.


PASCAL UNITS

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.

What are the contents of a Pascal unit?

a. Unit name

b. Interface(that contains the methods to be defined later)

c.Implementation(where the contents of the interface such as the procedures,


functions,etc) are now defined.

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.

a. Variables-These are program entities whose values do not change during


execution and are declared just before the begin keyword. However, global
variables can be declares after the program header.

b.Procedures and functions-Procedures are basically different from functions in


such a way that they do not have a return value like functions. However, both are
sets of instructions. Params in the syntax means parameters.

c.Comments-Note that {...}are for multilines while {...}are used to comment


singly.

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

f. Reserved Words-These words have their default or pre-defined meaning in


Pascal compiler. They must be used as such else there will be a compile error.

Examples are seen on page 84 such as Else, Const, Var and so on.

g.Character set/Identifiers-Pascal character set include the alphabets (in all


cases),digits and special characters(+*.= and so on

What are identifiers?

They are sequence of letters or digits (not special characters)which serves as a


name to variables, constants, types, functions, procedures and other program
entities. The data type of an identifier is very crucial as it will denote the meaning,
storage and other types of entities that it can work with.

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.

b. Constants-These values remain fixed at the start of a pascal program.Pascal


constants can be of 4 types. What are they? They are -numeric, logical, string and
character types

Hence a given identifier is given a fixed value

c. Enumerated types-These are also called user-defined data types. Why?It is


because the programmer names the types to suit what he wants to do. The types are
not among Pascal default data types.

This data type enumerates or lists values, hence the name.

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.

SUMMER contains 5 items (months), COLORS contain 8 items while


TRANSPORT has 4 items. Notice that these data types:MONTHS, SUMMER and
TRANSPORT are not among the default data types hence are user-defined.

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).

Again, the example on page 88 shows that we create subranges(two seasons-


SUMMER and WINTER in this case) from the general 12 months of the year.
The 12 months represent the enumerated type. The subrange SUMMER ranges
from April to August while the other subrange WINTER ranges from October to
Decenber.

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

1. What is a Pascal unit?

2. Differentiate between built-in units and user-defined units.

3. What are the steps in creating a unit?

ASSIGNMENT

Run the program on page 79 of your text to show units.

You might also like