PASCAL PROGRAMMING 

CLASS AND OBJECT (OBJECT ORIENTED PROGRAMMING)


SOFTWARE PROGRAMS USING OOP

By standard, there are three (3) ways to write codes in any programming language, most especially the ones that support Object Oriented Programming (OOP).

The first is using simple logic approach or methodology (that involves using simple variables and datatypes).

The second is using procedure or function approach or methodology (this allows you to reuse your code without recreating a new one).

The third is class and object approach or methodology (this merges both simple logic and procedure approach into a single item). OOP is a methodology which helps to simplify and design a software program using classes, objects, procedure / function, variables, datatypes and other OOP concepts like inheritance, polymorphism, encapsulation, abstraction, data binding, data hiding, interface / methods. We also have concepts like aggregation, association, composition, cohesion, and coupling. The third approach is used in the software industry.

A class is a user-defined datatype, blueprint and/or template for creating objects and for defining / describing what an object will look like. Class consist of attributes or member variables and implementation of behaviour or member functions. Object is an instance of the class.

Class: class is a blueprint or template for creating and managing objects, providing initial values for state (member variables or attributes), and implementations of behavior (member functions or methods). The user-defined objects are created using the class keyword. Class acts as a pointer to an object. Object: object is an instance created from a particular class. Object allocates memory when it is created and it can be created many times.

Syntax to declare a class in Pascal

type

className = object

//method or field declarations;

//procedure or function declarations;

end;

TIP: Don’t put semicolon at the end of the className declaration. Object is a keyword use for declaring a class.

PROGRAMS WITH EXAMPLES

Let us demonstrate these approaches by writing and running a pascal program to compute the average of three courses for a student. The program will accept the scores of the user and display the average in the nearest whole number.

TIP: We are solving a problem using three different approaches with same result 

 // Simple logic approach

program AvgCourses;

 

//declare variables

var

csc401, csc402, csc403 : integer;

average : real;

begin

//user action and accept input

  writeln ('Insert the score of three courses');

  readln(csc401, csc402, csc403);

 

//process the formula

average := (csc401 + csc402 + csc403) / 3;

 

//display output to the nearest whole number

writeln('The average of three courses is:: ', average:0:0)

end.

 


//procedure approach

program AvgCourses;

 

//declare variables

var

c1, c2, c3 : integer;

average : real;

 

//create the procedure and pass your parameters - avg, csc401, csc402, csc403

procedure xavg (var avg:real; csc401:integer; csc402:integer; csc403:integer);

begin

//process the formula

avg := (csc401 + csc402 + csc403) / 3;

end;

 

//start main

begin

//user action and accept input

  writeln ('Insert the score of three courses');

  readln(c1, c2, c3);

//call the procedure and pass your arguments

xavg(average, c1, c2, c3);

//display output to the nearest whole number

writeln('The average of three courses is:: ', average:0:0);

end.

 


//class and object approach

program AvgCourses;

//create the class

type

avgrec = object       //do not put semicolon (;) here

//declare memebers (variables) for the class

csc401, csc402, csc403 : integer;

//create a variable for the procedure to handle input and output

procedure  getinput;

procedure  setoutput;

end;

 

//create procedure to handle user action and accept input

procedure avgrec.getinput;

begin

writeln ('Insert the score of three courses');

readln(csc401, csc402, csc403);

end;

 

//create procedure to handle display of output

procedure avgrec.setoutput;

var

average : real;

begin

average := (csc401 + csc402 + csc403) / 3;

writeln('The average of three courses is:: ', average:0:0);

end;

  

//start main

var

//create an instance (object) of the class

r1 : avgrec;

begin

r1.getinput;

r1.setoutput;

end.


HINT: A class is a way of organizing information about a type of data so a programmer can reuse elements when making multiple instances of that data type—for example, r1 is an instance of avgrec. We can create more instances or objects from the class avgrec.


Check greatest

//without using AND, OR operator

//pascal program to return the largest number among three initialized numbers

program maxNumber;

var

a, b, c, result : integer;

begin

    a := 50;

    b := 70;

    c := 93;

    if (a>b) then

    result := a        //store the value of 'a' in result, if a is greater than b

    else

    result :=b;        //store the value of 'b' in result, if b is greater than a

    if (c>result)then    //compare c and result value, if c is greater store the value of 'c' in result

    result := c;   

//display the output

  writeln ('The greatest among the three numbers is:: ', result);

end.

Comments

Popular posts from this blog

Basic Web Page - phase 1