PASCAL - OBJECT ORIENTED

Similar documents
Inheritance, and Polymorphism.

Introduction to Classes

OOPS Viva Questions. Object is termed as an instance of a class, and it has its own state, behavior and identity.

3.Constructors and Destructors. Develop cpp program to implement constructor and destructor.

Chapter 13: Introduction to Classes Procedural and Object-Oriented Programming

PASCAL - RECORDS. To define a record type, you may use the type declaration statement. The record type is defined as

Object-Oriented Programming

CLASSES AND OBJECTS IN JAVA

INHERITANCE: CONSTRUCTORS,

CGS 2405 Advanced Programming with C++ Course Justification

Object Oriented Programming. Assistant Lecture Omar Al Khayat 2 nd Year

10. Abstract Data Types

C++ Inheritance and Encapsulation

Data Structures and Other Objects Using C++

Java Object Oriented Design. CSC207 Fall 2014

CMSC 132: Object-Oriented Programming II

9/21/2010. Based on Chapter 2 in Advanced Programming Using Visual Basic.NET by Bradley and Millspaugh

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

Microsoft Visual Basic 2005: Reloaded

Data Structures (list, dictionary, tuples, sets, strings)

ITI Introduction to Computing II

OBJECT ORIENTED PROGRAMMING USING C++ CSCI Object Oriented Analysis and Design By Manali Torpe

Programming, numerics and optimization

More C++ : Vectors, Classes, Inheritance, Templates

And Even More and More C++ Fundamentals of Computer Science

Lecture 18 Tao Wang 1

ITI Introduction to Computing II

Chapter 11. Categories of languages that support OOP: 1. OOP support is added to an existing language

Paytm Programming Sample paper: 1) A copy constructor is called. a. when an object is returned by value

INHERITANCE. Spring 2019

Object-Oriented Languages and Object-Oriented Design. Ghezzi&Jazayeri: OO Languages 1

More C++ : Vectors, Classes, Inheritance, Templates. with content from cplusplus.com, codeguru.com

Chapter 1: Object-Oriented Programming Using C++

C++ (classes) Hwansoo Han


Principles of Software Construction: Objects, Design, and Concurrency. Objects (continued) toad. Spring J Aldrich and W Scherlis

Data Structures using OOP C++ Lecture 3

C++ Important Questions with Answers

Oops known as object-oriented programming language system is the main feature of C# which further support the major features of oops including:

Intro to OOP Visibility/protection levels and constructors Friend, convert constructor, destructor Operator overloading a<=b a.

System Design and Programming II

Chapter 6 Introduction to Defining Classes

Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub

M301: Software Systems & their Development. Unit 4: Inheritance, Composition and Polymorphism

C++ PROGRAMMING LANGUAGE: CLASSES. CAAM 519, CHAPTER 13

JAVA MOCK TEST JAVA MOCK TEST II

PROGRAMMING LANGUAGE 2

C++ (Non for C Programmer) (BT307) 40 Hours

CS 6456 OBJCET ORIENTED PROGRAMMING IV SEMESTER/EEE

C# MOCK TEST C# MOCK TEST II

What are the characteristics of Object Oriented programming language?

Lecture 13: Object orientation. Object oriented programming. Introduction. Object oriented programming. OO and ADT:s. Introduction

Subtyping (Dynamic Polymorphism)

Lecture Notes on Programming Languages

Chapter 5 Object-Oriented Programming

Data Structure. Recitation III

CPS 506 Comparative Programming Languages. Programming Language

Short Notes of CS201

Advanced Programming Using Visual Basic 2008

Software Architecture (Lesson 2) Object-Oriented Paradigm (1)

Objects and Classes. Amirishetty Anjan Kumar. November 27, Computer Science and Engineering Indian Institue of Technology Bombay

EMBEDDED SYSTEMS PROGRAMMING OO Basics

Chapter 2. Building Multitier Programs with Classes The McGraw-Hill Companies, Inc. All rights reserved. McGraw-Hill

CS201 - Introduction to Programming Glossary By

Java Fundamentals (II)

Inheritance, Polymorphism and the Object Memory Model

ENCAPSULATION. private, public, scope and visibility rules. packages and package level access.

Object Oriented Paradigm Languages

G Programming Languages - Fall 2012

Cpt S 122 Data Structures. Course Review Midterm Exam # 2

Inheritance. Lecture 11 COP 3252 Summer May 25, 2017

ECE 3574: Dynamic Polymorphism using Inheritance

CS-202 Introduction to Object Oriented Programming

Chapter 10 Introduction to Classes

OBJECT ORİENTATİON ENCAPSULATİON

CS250 Final Review Questions

Overview of OOP. Dr. Zhang COSC 1436 Summer, /18/2017

CSE 452: Programming Languages. Previous Lecture. From ADTs to OOP. Data Abstraction and Object-Orientation

Introduction to Programming Using Java (98-388)

INHERITANCE: EXTENDING CLASSES

1. Write two major differences between Object-oriented programming and procedural programming?

Module 10 Inheritance, Virtual Functions, and Polymorphism

CS 162, Lecture 25: Exam II Review. 30 May 2018

What is Polymorphism? Quotes from Deitel & Deitel s. Why polymorphism? How? How? Polymorphism Part 1

Object-Oriented Programming

Defining Classes and Methods

INHERITANCE PART 2. Constructors and Destructors under. Multiple Inheritance. Common Programming Errors. CSC 330 OO Software Design 1

Get Unique study materials from

Object Oriented Design

Introduction Of Classes ( OOPS )

Government Polytechnic, Muzaffarpur. Name of the Lab: OBJECT ORIENTED PROGRAMMING

QUIZ. How could we disable the automatic creation of copyconstructors

Inheritance. Benefits of Java s Inheritance. 1. Reusability of code 2. Code Sharing 3. Consistency in using an interface. Classes

Object Oriented Programming in Java. Jaanus Pöial, PhD Tallinn, Estonia

Chapter 12. OOP: Creating Object-Oriented Programs The McGraw-Hill Companies, Inc. All rights reserved. McGraw-Hill

G52CPP C++ Programming Lecture 13

Argument Passing All primitive data types (int etc.) are passed by value and all reference types (arrays, strings, objects) are used through refs.

Kapil Sehgal PGT Computer. Science Ankleshwar Gujarat Chapter 6 Inheritance Extending a Class

CS304 Object Oriented Programming Final Term

XII- COMPUTER SCIENCE VOL-II MODEL TEST I

Transcription:

PASCAL - OBJECT ORIENTED http://www.tutorialspoint.com/pascal/pascal_object_oriented.htm Copyright tutorialspoint.com We can imagine our universe made of different objects like sun, earth, moon, etc. Similarly, we can imagine our car made of different objects like wheel, steering, gear, etc. Same way, there are object-oriented programming concepts, which assume everything as an object and implement a software using different objects. In Pascal, there are two structural data types used to implement a real world object Object types Class types Object-Oriented Concepts Before we go in detail, let's define important Pascal terms related to Object-Oriented Pascal. Object An Object is a special kind of record that contains fields like a record; however, unlike records, objects contain procedures and functions as part of the object. These procedures and functions are held as pointers to the methods associated with the object's type. Class A Class is defined in almost the same way as an Object, but there is a difference in way they are created. The Class is allocated on the Heap of a program, whereas the Object is allocated on the Stack. It is a pointer to the object, not the object itself. Instantiation of a class Instantiation means creating a iable of that class type. Since a class is just a pointer, when a iable of a class type is declared, there is memory allocated only for the pointer, not for the entire object. Only when it is instantiated using one of its constructors, memory is allocated for the object. Instances of a class are also called 'objects', but do not confuse them with Object Pascal Objects. In this tutorial, we will write 'Object' for Pascal Objects and 'object' for the conceptual object or class instance. Member Variables These are the iables defined inside a Class or an Object. Member Functions These are the functions or procedures defined inside a Class or an Object and are used to access object data. Visibility of Members The members of an Object or Class are also called the fields. These fields have different visibilities. Visibility refers to accessibility of the members, i.e., exactly where these members will be accessible. Objects have three visibility levels:, and protected. Classes have five visibility types:,, strictly, protected and published. We will discuss visibility in details. Inheritance When a Class is defined by inheriting existing functionalities of a parent Class, then it is said to be inherited. Here child class will inherit all or few member functions and iables of a parent class. Objects can also be inherited. Parent Class A Class that is inherited by another Class. This is also called a base class or super class. Child Class A class that inherits from another class. This is also called a subclass or derived class. Polymorphism This is an object-oriented concept where same function can be used for different purposes. For example, function name will remain same but it may take different number of arguments and can do different tasks. Pascal classes implement polymorphism. Objects do not implement polymorphism. Overloading It is a type of polymorphism in which some or all of operators have different implementations depending on the types of their arguments. Similarly functions can also be overloaded with different implementation. Pascal classes implement overloading, but the Objects do not. Data Abstraction Any representation of data in which the implementation details are

hidden abstracted. Encapsulation Refers to a concept where we encapsulate all the data and member functions together to form an object. Constructor Refers to a special type of function which will be called automatically whenever there is an object formation from a class or an Object. Destructor Refers to a special type of function which will be called automatically whenever an Object or Class is deleted or goes out of scope. Defining Pascal Objects An object is declared using the type declaration. The general form of an object declaration is as follows type object-identifier = object field1 : field-type; field2 : field-type;... procedure proc1; function f1(): function-type; object : object-identifier; Let us define a Rectangle Object that has two integer type data members - length and width and some member functions to manipulate these data members and a procedure to draw the rectangle. type Rectangle = object length, width: integer; constructor init; destructor done; procedure setlength(l: inteter); function getlength(): integer; procedure setwidth(w: integer); function getwidth(): integer; r1: Rectangle; pr1: ^Rectangle; After creating your objects, you will be able to call member functions related to that object. One member function will be able to process member iable of related object only. Following example shows how to set lengths and widths for two rectangle objects and draw them by calling the member functions. r1.setlength(3); r1.setwidth(7); writeln(' Draw a rectangle: ', r1.getlength(), ' by ', r1.getwidth()); r1.draw; new(pr1); pr1^.setlength(5); pr1^.setwidth(4);

writeln(' Draw a rectangle: ', pr1^.getlength(), ' by ',pr1^.getwidth()); pr1^.draw; dispose(pr1); Following is a complete example to show how to use objects in Pascal program exobjects; type Rectangle = object length, width: integer; procedure setlength(l: integer); function getlength(): integer; procedure setwidth(w: integer); function getwidth(): integer; r1: Rectangle; pr1: ^Rectangle; procedure Rectangle.setlength(l: integer); length := l; procedure Rectangle.setwidth(w: integer); width :=w; function Rectangle.getlength(): integer; getlength := length; function Rectangle.getwidth(): integer; getwidth := width; procedure Rectangle.draw; i, j: integer; for i:= 1 to length do for j:= 1 to width do write(' * '); r1.setlength(3); r1.setwidth(7); writeln('draw a rectangle:', r1.getlength(), ' by ', r1.getwidth()); r1.draw; new(pr1); pr1^.setlength(5); pr1^.setwidth(4); writeln('draw a rectangle:', pr1^.getlength(), ' by ',pr1^.getwidth()); pr1^.draw;

dispose(pr1); end. When the above code is compiled and executed, it produces the following result Draw a rectangle: 3 by 7 * * * * * * * * * Draw a rectangle: 5 by 4 Visibility of the Object Members Visibility indicates the accessibility of the object members. Pascal object members have three types of visibility Visibility Public Private Protected Accessibility The members can be used by other units outside the program unit The members are only accessible in the current unit. The members are available only to objects descended from the parent object. By default, fields and methods of an object are and are exported outside the current unit. Constructors and Destructors for Pascal Objects: Constructors are special type of methods, which are called automatically whenever an object is created. You create a constructor in Pascal just by declaring a method with a keyword constructor. Conventionally, the method name is Init, however, you can provide any valid identifier of your own. You can pass as many arguments as you like into the constructor function. Destructors are methods that are called during the destruction of the object. The destructor methods destroy any memory allocation created by constructors. Following example will provide a constructor and a destructor for the Rectangle class which will initialize length and width for the rectangle at the time of object creation and destroy it when it goes out of scope. program exobjects; type Rectangle = object length, width: integer; constructor init(l, w: integer); destructor done; procedure setlength(l: integer); function getlength(): integer; procedure setwidth(w: integer); function getwidth(): integer;

r1: Rectangle; pr1: ^Rectangle; constructor Rectangle.init(l, w: integer); length := l; width := w; destructor Rectangle.done; writeln(' Desctructor Called'); procedure Rectangle.setlength(l: integer); length := l; procedure Rectangle.setwidth(w: integer); width :=w; function Rectangle.getlength(): integer; getlength := length; function Rectangle.getwidth(): integer; getwidth := width; procedure Rectangle.draw; i, j: integer; for i:= 1 to length do for j:= 1 to width do write(' * '); r1.init(3, 7); writeln('draw a rectangle:', r1.getlength(), ' by ', r1.getwidth()); r1.draw; new(pr1, init(5, 4)); writeln('draw a rectangle:', pr1^.getlength(), ' by ',pr1^.getwidth()); pr1^.draw; pr1^.init(7, 9); writeln('draw a rectangle:', pr1^.getlength(), ' by ',pr1^.getwidth()); pr1^.draw; dispose(pr1); r1.done; end. When the above code is compiled and executed, it produces the following result Draw a rectangle: 3 by 7 * * * * * * * * * Draw a rectangle: 5 by 4

Draw a rectangle: 7 by 9 * * * * * * * Destructor Called Inheritance for Pascal Objects Pascal objects can optionally inherit from a parent object. The following program illustrates inheritance in Pascal Objects. Let us create another object named TableTop, which is inheriting from the Rectangle object. program exobjects; type Rectangle = object length, width: integer; procedure setlength(l: integer); function getlength(): integer; procedure setwidth(w: integer); function getwidth(): integer; TableTop = object (Rectangle) material: string; function getmaterial(): string; procedure setmaterial( m: string); procedure displaydetails; tt1: TableTop; procedure Rectangle.setlength(l: integer); length := l; procedure Rectangle.setwidth(w: integer); width :=w; function Rectangle.getlength(): integer; getlength := length; function Rectangle.getwidth():integer; getwidth := width; procedure Rectangle.draw;

i, j: integer; for i:= 1 to length do for j:= 1 to width do write(' * '); function TableTop.getmaterial(): string; getmaterial := material; procedure TableTop.setmaterial( m: string); material := m; procedure TableTop.displaydetails; writeln('table Top: ', self.getlength(), ' by ', self.getwidth()); writeln('material: ', self.getmaterial()); procedure TableTop.draw(); i, j: integer; for i:= 1 to length do for j:= 1 to width do write(' * '); writeln('material: ', material); tt1.setlength(3); tt1.setwidth(7); tt1.setmaterial('wood'); tt1.displaydetails(); writeln('calling the Draw method'); tt1.draw(); end. Following are the important points which should be noted down The object Tabletop has inherited all the members of the Rectangle object. There is a draw method in TableTop also. When the draw method is called using a TableTop object, TableTop's draw gets invoked. There is an implicit instance named self that refers to the current instance of the object. When the above code is compiled and executed, it produces the following result Table Top: 3 by 7 Material: Wood Calling the Draw Method * * * * * * * * * Material: Wood

Material: Wood Loading [MathJax]/jax/output/HTML-CSS/jax.js