Programming Languages: Encapsulation

Similar documents
Classes. Classes as Code Libraries. Classes as Data Structures. Classes/Objects/Interfaces (Savitch, Various Chapters)

Classes. Classes as Code Libraries. Classes as Data Structures

Class 15. Object-Oriented Development from Structs to Classes. Laura Marik Spring 2012 C++ Course Notes (Provided by Jason Minski)

NAMESPACES IN C++ You can refer the Programming with ANSI C++ by Bhushan Trivedi for Understanding Namespaces Better(Chapter 14)

Programming Language. Functions. Eng. Anis Nazer First Semester

Encapsulation in C++

Chapter 5 Methods. Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved.

Methods and Data (Savitch, Chapter 5)

Methods. Methods. Mysteries Revealed

Imperative Languages!

Function. specific, well-defined task. whenever it is called or invoked. A function to add two numbers A function to find the largest of n numbers

What does it mean by information hiding? What are the advantages of it? {5 Marks}

Programming Languages: OO Paradigm, Objects

Abstract Data Types (ADTs) 1. Legal Values. Client Code for Rational ADT. ADT Design. CS 247: Software Engineering Principles

Functions. Prof. Indranil Sen Gupta. Dept. of Computer Science & Engg. Indian Institute t of Technology Kharagpur. Introduction

Programming Languages: OO Paradigm, Polymorhism and Class Members

Bounding Object Instantiations, Part 2

Scope and Parameter Passing 1 / 19

Object Oriented Programming COP3330 / CGS5409

CS 247: Software Engineering Principles. ADT Design

EL2310 Scientific Programming

Lecture 7. Log into Linux New documents posted to course webpage

Kapil Sehgal PGT Computer. Science Ankleshwar Gujarat

STUDY NOTES UNIT 1 - INTRODUCTION TO OBJECT ORIENTED PROGRAMMING

Functions. Functions are everywhere in C. Pallab Dasgupta Professor, Dept. of Computer Sc & Engg INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR

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

Protection Levels and Constructors The 'const' Keyword

Part VII. Object-Oriented Programming. Philip Blakely (LSC) C++ Introduction 194 / 370

What are the characteristics of Object Oriented programming language?

Basic memory model Using functions Writing functions. Basics Prototypes Parameters Return types Functions and memory Names and namespaces

Functions in C. Lecture Topics. Lecture materials. Homework. Machine problem. Announcements. ECE 190 Lecture 16 March 9, 2011

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

Encapsulation. You can take one of two views of an object: internal - the structure of its data, the algorithms used by its methods

Object-Oriented Programming Concepts

CSE341, Fall 2011, Lecture 12 Summary

JAVA Programming Concepts

VB.NET. Exercise 1: Creating Your First Application in Visual Basic.NET

Dot and Scope Resolution Operator

Object-Oriented Programming

CIS 190: C/C++ Programming. Classes in C++

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

Functions. Autumn Semester 2009 Programming and Data Structure 1. Courtsey: University of Pittsburgh-CSD-Khalifa

C++ Review. CptS 223 Advanced Data Structures. Larry Holder School of Electrical Engineering and Computer Science Washington State University

To define methods, invoke methods, and pass arguments to a method ( ). To develop reusable code that is modular, easy-toread, easy-to-debug,

CSCE 110 PROGRAMMING FUNDAMENTALS

Types of Recursive Methods

About this exam review

Java Object Oriented Design. CSC207 Fall 2014

Chapter 1: Object-Oriented Programming Using C++

Constants, References

Object Oriented Programming in C#

CIT 590 Homework 6 Fractions

Classes - 2. Data Processing Course, I. Hrivnacova, IPN Orsay

COMS W3101 Programming Language: C++ (Fall 2015) Ramana Isukapalli

Benefits of Methods. Chapter 5 Methods

Programming Languages. Example 5. Example 4. CSE 130 : Fall type, can reuse code for all types! let rec cat l = match l with

Chapter 5 Methods / Functions

Programming in C. main. Level 2. Level 2 Level 2. Level 3 Level 3

INITIALISING POINTER VARIABLES; DYNAMIC VARIABLES; OPERATIONS ON POINTERS

CMPT 117: Tutorial 1. Craig Thompson. 12 January 2009

Object-Orientation. Classes Lecture 5. Classes. State and Behaviour. Instance Variables. Object vs. Classes


Classes. Christian Schumacher, Info1 D-MAVT 2013

CPSC 427a: Object-Oriented Programming

Object-oriented basics. Object Class vs object Inheritance Overloading Interface

Scope. Chapter Ten Modern Programming Languages 1

Functional Logic Design Patterns

OBJECTS. An object is an entity around us, perceivable through our senses. Types of Object: Objects that operate independently.

Chapter 11. Abstract Data Types and Encapsulation Concepts

Chapter 5 Methods. Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved.

10. Functions (Part 2)

2.4 Structuring programs

CS110: PROGRAMMING LANGUAGE I

An Introduction to C++

Container Vs. Definition Classes. Container Class

Object Oriented Technology

Object-Oriented Programming (OOP) Fundamental Principles of OOP

Chapter 5: Classes and Objects in Depth. Information Hiding

Lecture Static Methods and Variables. Static Methods

AN OVERVIEW OF C++ 1

Chapter 6 Methods. Dr. Hikmat Jaber

Lecture Static Methods and Variables. Static Methods

Ch. 10: Name Control

OBJECT ORİENTATİON ENCAPSULATİON

EL2310 Scientific Programming

Sri Vidya College of Engineering & Technology

FINAL TERM EXAMINATION SPRING 2010 CS304- OBJECT ORIENTED PROGRAMMING

Course Coding Standards

Global & Local Identifiers

EECS168 Exam 3 Review

Data Structures using OOP C++ Lecture 3

Introduction to Programming Using Java (98-388)

What is Class? Remember

EL2310 Scientific Programming

CSc 372 Comparative Programming Languages


Ch. 11: References & the Copy-Constructor. - continued -

EECS 211 Lab 6. Classes and Abstractation Winter Getting the code. Encapsulation and Abstraction. Classes

CSc 372. Comparative Programming Languages. 8 : Haskell Function Examples. Department of Computer Science University of Arizona

Module 7 b. -Namespaces -Exceptions handling

Transcription:

Programming Languages: Encapsulation Onur Tolga Şehitoğlu Computer Engineering 20 March 2008

1 Encapsulation 2 Packages 3 Hiding 4 Abstract Data Types 5 Class and Object Object Class

Encapsulation Encapsulation Managing the complexity Re-usable code and abstraction. Example: 50 lines no abstraction is essential, all in main() 500 lines function/procedure abstraction sufficient 5,000 lines function groups forming modules, modules are combined to form the application 500,000 lines heavy abstraction and modularization, all parts designed for reuse (libraries, components etc)

Encapsulation Modularization and Encapsulation Building an independent and self complete set of function and variable declarations (Packaging) Restricting access to this set only via a set of interface function and variables. (Hiding and Encapsulation) interface detail (functions, other application variables, algorithm)

Encapsulation Advantages of Encapsulation High volume details reduced to interface definitions (Ease of development/maintenance) Many different applications use the same module via the same interface ( Code re-usability) Lego like development of code with building blocks (Ease of development/maintenance) Even details change, applications do not change (as long as interface is kept same) (Ease of development/maintenance) Module can be used in following projects (Code re-usability)

Packages A group of declarations put into a single body. C has indirect way of packaging per source file. C++ namespace Trig { const double p i =3.14159265358979; double sin(double x) {... } double cos(double x) {... } double tan(double x) {... } double atan(double x) {... }... }; Trig::sin(Trig::pi/2+x)+Trig::cos(x) C++: (::) Scope operator. Identifier overlap is avoided. List::insert(...) ve Tree::insert(...) no name collisions.

Hiding Hiding A group of functions and variables hidden inside. The others are interface. Abstraction inside of a package: double taylorseries(double); double sin(double x); double pi=3.14159265358979; double randomseed; double cos(double x); double errorcorrect(double x); {-- only sin, pi and cos are accessible --} module Trig(sin,pi,cos) where t a y l o r s e r i e s x =... sin x =... pi =3.14159265358979 randomseed=... cos x =... e r r o r c o r r e c t x =...

Abstract Data Types Abstract data types Internals of the datatype is hidden and only interface functions provide the access. Example: rational numbers: 3/4, 2/5, 19/3 data Rational = Rat (Integer,Integer) x = Rat (3,4) add (Rat(a,b)) (Rat(c,d)) = Rat (a*d+b*c,b*d) 1 Invalid value? Rat (3,0) 2 Multiple representations of the same value? Rat (2,4) = Rat (1,2) = Rat(3,6) Solution: avoid arbitrary values by the user.

Abstract Data Types Main purpose of abstrac data types is to use them transparently (as if they were built-in) without loosing data integrity. module Rational(Rational,rat,add,subtract,multiply,divide) where data Rational = Rat (Integer,Integer) rat (x,y) = s i m p l i f y (Rat(x,y)) add (Rat(a,b)) (Rat(c,d)) = rat (a*d+b*c,b*d) subtract(rat(a,b)) (Rat(c,d)) = rat (a*d-b*c,b*d) multiply(rat(a,b)) (Rat(c,d)) = rat (a*c,b*d) d i v i d e (Rat(a,b)) (Rat(c,d)) = rat (a*d,b*c) gcd x y = if (x==0) then y else if (y==0) then x else if (x<y) then gcd x (y-x) else gcd y (x-y) simplify (Rat(x,y)) = if y==0 then Rat(div x y,1) else let a=gcd x y in Rat(div x a, div y a) Initial value? We need constructor function/values. (remember we don t have the data definition) rat (x,y) instead of Rat (x,y)

Class and Object Object Object Packages containing hidden variables and access is restricted to interface functions. Variables with state Data integrity and abstraction provided by the interface functions. Entities in software can be modelled in terms of functions (server, customer record, document content, etc). Object oriented design. Example (invalid syntax! imaginary C++) namespace Counter { private: int counter=0; public: int get() { return counter;} public: void increment() { counter++; } }; Counter:: get() Counter:: increment()

Class and Object Class Class The set of same typed objects form a class An object is an instance of the class that it belongs to (a counter type instead of a single counter) Classes have similar purposes to abstract data types Some languages allows both objects and classes C++ class declaration (valid syntax): class Counter { private: int counter; public: Counter() { counter=0; } int get() { return counter;} void increment() { counter++; } } men, v e h i c l e s; men. increment(); v e h i c l e s. increment(); men. get(); v e h i c l e s. get();

Class and Object Class Abstract data type interface (constructor, functions) Object interface (constructor, functions) detail (data type definition, auxiliary functions) detail (variables, auxiliary functions) Purpose preserving data integrity, abstraction, re-usable codes.