Pascal Classes and Objects

objects
An object is but an extension of a record. Whereas in records only the members (fields) are declared, in objects, procedures and functions are also declared in addition to the meber declaration. The member declaration in an object in an object are called instance variables, where as member functions and procedures are called methods.

Encapsulation
Combining the instance variables and member functions / procedures in an object is called encapsulation. This is because they are enclosed (their code is bound together) in the object and they are not apparent The objects class name is the object identifier (the naming of the class should follow the rules of naming identifiers). The field declaration is member and method declaration is the procedure / function declaration.

Syntax: type classname  =   object field declarations; :                                            :                            method declaration; {method name} end; Example: stdrec =  object stdname :   string; stdNo  :  integer; procedure stdinputing; procedure stdoutputing; end;

The general form for the procedure (method) declaration inside an object is:
procedure classname.methodname  (variable list); begin statement; /              statement; end; Here, the classname is the object class name and the method name is the name of the procedure (method) as declared in the object, it is followed by a variable list to be passed to the procedure and terminated by a semi-colon.

The general form for the function (method) declaration inside an object is:
function  classname.methodname  (variable list) datatype; begin statement; /              statement; end; Here, the classname is the object class name and the method name is the name of the function (method) as declared in the object, it is followed by a variable list to be passed to the function, then the dataype to be returned by the function and terminated by a semi-colon.

Using an object
Before an object is used it should be declared in the var declaration section (for the memory to be reserved).

Syntax: var objectname  :   object class; Object name is the identifier to be used in the program code, while object class is the name which we already declared in the type declaration section. program objectexample (input, output); uses crt; type stdrec  =  object stdname  :    string; examno  :    integer; math, physics, chemistry :  integer; procedure  stdinputing; procedure  stdoutputing; end; procedure stdrec.stdinputing; begin clrscr; writeln('Enter student name '); readln(stdname); writeln('Enter Exam No'); readln(examno); writeln('Enter marks for Maths, Physics and Chemistry); readln(maths, physics, chemistry); end; procedure stdrec.stdoutputing; var    total   :   integer; begin writeln (' Name of the student  :  ', stdname); writeln(' Exam Number :', examno); total :=   math + physics + chemistry; writeln ('Total marks = ', total); end; var stdmarkrec :  stdrec; begin stdmarkrec.stdinputing; stdmarkrec.stdoutputing; readln; end.

Inheritance
This is the ability of a child object to acquire the characteristics or properties of a parent (already existing) object. e.g a sub-class, re-using code of the existing class. If we have object P and we want to declare two more objects (M and N) which contain some of the instance variables and methods that are present in object P, then instead of declaring each object with all these instance variables and methods (which are same). We therefore declare object P (which contains the common fields), with all its instance variables and methods and then declare object M and N with only the fields pertaining to each of them alone. Object P is therefore the parent class and objects M and N are subclasses.

Defining an object subclass
Syntax: type classname1 = object  {parent class} field declarations; method declaration; end; classname2 =  object (classname1) {subclass} field declaration; method declaration; end; Example: program subclass (input, output); uses crt; type studrec =  object studname : string; studexamno :  integer; procedure stinput; procedure stgetname; procedure stgetexamno; end; outcome = object (studrec) total : integer; procedure stgetmarks; procedure stdyear; end; procedure studrec.stinput; begin studname  := '     '; studexmno := 0; end; procedure studrec.stgetname; begin writeln('Enter Name'); readln(stdname); end; procedure studrec.stgetexamno; begin writeln('Enter Examno '); readln(studexamno); end; procedure outcome.stgetmarks; begin writeln('Enter Marks'); readln(total); end; procedure outcome.stdyear; var C :  string; begin if total < 350 then C := 'Second'; else C := 'First'; writeln(' Name             : ', studname); writeln(' Exam No      : ',studexamno); writeln('Total Marks : ', C); end; var resultobj  :   outcome; begin clrscr; resultobj.stinput; resultobj.stgetname; resultobj.stgetexamno; resultobj.stgetmarks; resultobj.stdyear; end.

Polymorphism
It is considered to be the most powerful tool of object oriented programming as it we are able to modify the methods of the parent class. It involves allowing a single definition to be used by different types of data(specially different classes and fuctions / procedures). Constructor:-> It is a special procedure (in oop pascal). It allows the performing of internal manipulations to enable the use of virtual methods in an object class. It signifies the birth of an object. Syntax of a polymorphic subclass: type class name = object (parent class) field declarations; constructor declarations; method declarations; virtual; end; Example studrec  =  object studname    :   string; stdexamno  :   integer; constructor  init; procedure stgetname; procedure stgetexamno; procedure stdisplay; virtual; end;