190 likes | 380 Views
5 – Abstract Data Types. Overview:. 1. What is Data Abstraction? What is ADT? 2. Model for an Abstract Data Type 3 . Complex Number ADT Example 4. How Well are ADTs Supported in C? 5. C++ 6. ADT vs Object-Oriented Programming. 1.1 What is Data Abstraction?.
E N D
Overview: 1. What is Data Abstraction? What is ADT? 2. Model for an Abstract Data Type 3. Complex Number ADT Example 4. How Well are ADTs Supported in C? 5. C++ 6. ADT vs Object-Oriented Programming
1.1 What is Data Abstraction? • Concept of “Abstraction” • Allows us to consider the high-level characteristics of something without getting bogged down in the details • For example: Process abstraction in procedural programming like C, we can use (pre-defined) functions without concern how they really works inside. • Data Abstraction • We know what a data type can do • How it is done is hidden
1.2 What is an Abstract Data Type? • Abstract Data Type (ADT) • Defines a particular data structure in terms of data and operations • Offers an interface of the objects (instances of an ADT) • An ADT consists of • Declaration of data • Declaration of operations • Encapsulation of data and operations : data is hidden from user and can be manipulated only by means of operations
1.3 ADT Implementation • Implementaion of an Abstract Data Type (ADT) • Hidden from the user • Same ADT may be implemented in different ways in different languages • Some languages offer built-in ADTs and/or features to be used to implement ADTs (user-define types) • ADTs support modular design which is very important in software development
2.1 Model for an ADT Interface - Operations Data Structure System design with ADTs Identification of ADTs (identify data or attributes) Problem definiton Specify ADT interactions Specify ADT operations Identify object hierarchy (if using OOP) Implement ADTs
client ADT use interface implementation client manufacturer’sresponsibility client 2.2 Clients and Manufacturers
2.3 Benefits • Manufacturer Benefits: • easy to modify, maintain • profitable • reusable • Client Benefits: • simple to use, understand • familiar • cheap • component–based
3. Complex Number ADT Example • A complex number has a real part and an imaginary part. e.g.: 2+4i • Interface (operations) ? • create a complex number • add, subtract, multiply, divide • print a complex number • test to see if something is complex • etc.
Declare a complex number • Interface: Complex c1, c2, c3; • Possible Implementation (in C language ): struct complex { double real,imag; } typedef struct complex Complex;
Create a complex number • Interface: c1 = create_complex(2, 3);/* conceptually, c1 = 2+3i */
Implementation: Complex create_complex(double real, double imag) { Complex c; c.real = real; c.imag = imag; return c; }
Add two complex numbers • Interface: c3 = add_complex(c1, c2);/* conceptually, c3 = c1 + c2 */
Implementation: Complex add_complx(Complex c1, Complex c2) { Complex csum; csum.real = c1.real + c2.real; csum.imag = c1.imag + c2.imag; return csum; }
Using the Complex Number ADT #include <stdio.h>/* type implementation */struct complex { double real,imag; typedef struct complex Complex; /* operation interface */Complex create_complex(double,double);Complex add_complex(Complex, Complex); /* other Complex prototypes print_complex() . . . */ continued
Using the Complex Number ADT int main ( ){ Complex c1, c2, c3; c1 = create_complex(2,-3); c2 = create_complex(2,-3); c3 = add_complex(c1,c2); print_complex(c3); return 0; } /*Implementation of Complex functions */ :
4. How Well are ADTs Supported in C? • Does C enforce the use of the ADTs interface and the hiding of its implementation? • No
5. C++ and OOP • C++ is a superset of C, which has added features to support object oriented programming (OOP) • C++ offers STL (Standard Templates Library)providing a set generic data structureswhich allow programmers to easily implement standard data structures like queues, lists, and stacks. • C++ supports classes • things very like ADTs • Other OOP languages such as Java, Smalltalk
6. ADT vsObject-Oriented Programming (OOP) • ADTs are not a part of a particular programming language • Rather they are implemented by a programmer to solve a particular problem or some class of problems • In OOP, an ADT can be easily modeled as a class • An instance as an object • Data of ADT as properties or fields of a class • Operations as methods • ADT ≠ OOP • Classes in OOP offers more features than ADTs : Inheritance (Superclass-Subclass), Polymorphisms, etc.