1 2 Data Design and Implementation Chapter 2 Data Design and Implementation.

Slides:



Advertisements
Similar presentations
Programming Languages and Paradigms
Advertisements

1 Classes and Data Abstraction Chapter What a Class ! ! Specification and implementation Private and public elements Declaring classes data and.
Chapter 9 Imperative and object-oriented languages 1.
Chapter 6 Structured Data Types Arrays Records. Copyright © 2007 Addison-Wesley. All rights reserved. 1–2 Definitions data type –collection of data objects.
Chapter 14: Overloading and Templates
Chapter 3 Data Abstraction: The Walls. © 2005 Pearson Addison-Wesley. All rights reserved3-2 Abstract Data Types Modularity –Keeps the complexity of a.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
1 Chapter 11 Structured Types, Data Abstraction and Classes Dale/Weems/Headington.
ISBN Chapter 6 Data Types: Structured types.
© 2006 Pearson Addison-Wesley. All rights reserved4-1 Chapter 4 Data Abstraction: The Walls.
Chapter 2 Data Design and Implementation. Homework You should have already read section 2.2 You should have already read section 2.2 Read Section 2.3.
1 C++ Plus Data Structures Nell Dale Chapter 2 Data Design and Implementation Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus.
Dynamic Structures & Arrays.
Review of C++ Programming Part II Sheng-Fang Huang.
1 Chapter 14-2 Object- Oriented Software Development Dale/Weems.
1 Using Classes Object-Oriented Programming Using C++ Second Edition 5.
Using Classes Object-Oriented Programming Using C++ Second Edition 5.
Programming Languages and Paradigms Object-Oriented Programming.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes.
Data Structures Using C++ 2E Chapter 3 Pointers and Array-Based Lists.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes.
C++ Plus Data Structures Data Design and Implementation
Chapter 2 Data Design and Implementation. Data The representation of information in a manner suitable for communication or analysis by humans or machines.
CISC Data Structures C/C++ Review Fall
1 C++ Plus Data Structures Nell Dale David Teague Chapter 2 Data Design and Implementation Slides by Sylvia Sorkin, Community College of Baltimore County.
1 Basic Concepts of Object-Oriented Design. 2 What is this Object ? There is no real answer to the question, but we ’ ll call it a “ thinking cap ”. l.
Chapter 6 Structures and Classes. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-2 Structures  2 nd aggregate data type: struct  Recall:
1 Chapter 2 Data Design and Implementation. 2 Data The representation of information in a manner suitable for communication or analysis by humans or machines.
Overloading Binary Operators Two ways to overload –As a member function of a class –As a friend function As member functions –General syntax Data Structures.
1 © 2002, Cisco Systems, Inc. All rights reserved. Arrays Chapter 7.
1 Using Structures and Classes COSC 1557 C++ Programming Lecture 4.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 13 Introduction to Classes.
Learners Support Publications Classes and Objects.
CSE 425: Data Types I Data and Data Types Data may be more abstract than their representation –E.g., integer (unbounded) vs. 64-bit int (bounded) A language.
1 Chapter Structured Types, Data Abstraction and Classes Dale/Weems.
Data Structures Using C++ 2E Chapter 3 Pointers. Data Structures Using C++ 2E2 Objectives Learn about the pointer data type and pointer variables Explore.
Data Structures Using C++ 2E1 Inheritance An “is-a” relationship –Example: “every employee is a person” Allows new class creation from existing classes.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes.
Object Oriented Software Development
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
Introduction to c++ programming - object oriented programming concepts - Structured Vs OOP. Classes and objects - class definition - Objects - class scope.
Views of Data Data – nouns of programming world the objects that are manipulated information that is processed Humans like to group information Classes,
Structures and Classes Version 1.0. Topics Structures Classes Writing Structures & Classes Member Functions Class Diagrams.
Chapter 10: Classes and Data Abstraction. Objectives In this chapter, you will: Learn about classes Learn about private, protected, and public members.
EGR 2261 Unit 11 Classes and Data Abstraction  Read Malik, Chapter 10.  Homework #11 and Lab #11 due next week.  Quiz next week.
Introduction to Object-Oriented Programming Lesson 2.
1 Chapter 12 Arrays. 2 C++ Data Types structured array struct union class address pointer reference simple integral enum char short int long bool floating.
Fall 1999CS Composite Data Types. Fall 1999CS Topics to be Covered l Abstract data types l One-dimensional arrays l Two-dimensional arrays.
Data Design and Implementation. Definitions Atomic or primitive type A data type whose elements are single, non-decomposable data items Composite type.
Fall 2013CISC2200 Yanjun Li1 Review. Fall 2013CISC2200 Yanjun Li2 Outline Array Pointer Object-Oriented Programming.
Chapter 8 Arrays. A First Book of ANSI C, Fourth Edition2 Introduction Atomic variable: variable whose value cannot be further subdivided into a built-in.
Chapter 1 Data Abstraction: The Walls CS Data Structures Mehmet H Gunes Modified from authors’ slides.
CPS120: Introduction to Computer Science Lecture 16 Data Structures, OOP & Advanced Strings.
Slide 1 Chapter 6 Structures and Classes. Slide 2 Learning Objectives  Structures  Structure types  Structures as function arguments  Initializing.
Chapter 10: Classes and Data Abstraction. Classes Object-oriented design (OOD): a problem solving methodology Objects: components of a solution Class:
1 Classes and Data Abstraction Chapter What a Class ! ! Specification and implementation Private and public elements Declaring classes data and.
Object-Oriented Programming Using C++ Third Edition Chapter 7 Using Classes.
1 Chapter 12 Classes and Abstraction. 2 Chapter 12 Topics Meaning of an Abstract Data Type Declaring and Using a class Data Type Using Separate Specification.
Data Design and Implementation
Programming Logic and Design Seventh Edition
Chapter 2 Data Design and Implementation
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
Andy Wang Object Oriented Programming in C++ COP 3330
Chapter 1 Data Abstraction: The Walls
About the Presentations
Classes and Data Abstraction
CPS120: Introduction to Computer Science
Introduction to Data Structure
CPS120: Introduction to Computer Science
Chapter 9 Introduction To Classes
Presentation transcript:

1 2 Data Design and Implementation Chapter 2 Data Design and Implementation

2 Goals Describe an ADT from three perspectives: logical level, application level, and implementation level Explain how a specification can be used to record an abstract data type Describe the component selector at the logical level and describe appropriate applications for the C++ built-in types: structs, classes, one-dimensional arrays, and two-dimensional arrays Declare a class object Implement the member functions of a class Manipulate instances of a class (objects)

3 Goals Define the three ingredients of an object-oriented programming language: encapsulation, inheritance, and polymorphism Distinguish between containment and inheritance Use inheritance to derive one class from another class Use the C++ exception handling mechanism Access identifiers within a namespace Explain the use of Big-O notation to describe the amount of work done b an algorithm

4 Different Views of Data Data The representation of information in a manner suitable for communication or analysis by humans or machines Data are the nouns of the programming world:  The objects that are manipulated  The information that is processed

5 Different Views of Data Data Abstraction Separation of a data type’s logical properties from its implementation LOGICAL PROPERTIESIMPLEMENTATION What are the possible values?How can this be done in C++? What operations will be needed? How can data types be used?

6 APPLICATION REPRESENTATION Different Views of Data Data Encapsulation The separation of the representation of data from the applications that use the data at a logical level; a programming language feature that enforces information hiding int y; y = 25;

7 Different Views of Data You don't need to know how a number is represented in order to use in a program

8 Different Views of Data Value range: INT_MIN.. INT_MAX Operations: + prefix - prefix + infix - infix * infix / infix % infix Relational Operators infix TYPE int (inside) Representation of int as 16 bits two’s complement + Implementation of Operations

9 Different Views of Data Abstract Data Type A data type whose properties (domain and operations) are specified independently of any particular implementation Data Structure A collection of data elements whose organization is characterized by accessing operations that are used to store and retrieve the individual elements; the implementation of the composite data members of an ADT

10 Application (or user) level modeling real- life data in a specific context Logical (or ADT) level abstract view of the domain and operations Implementation level specific representation of the structure to hold the data items, and the coding for operations Different Views of Data

11 Different Views of Daa What is the application view? The logical view? The implementation view?

12 Different Views of Data How do the application and implementation view communicate?

13 Different Views of Data Application (or user) level Library of Congress, or Baltimore County Public Library Logical (or ADT) level domain is a collection of books; operations include: check book out, check book in, pay fine, reserve a book Implementation level representation of the structure to hold the “books” and the coding for operations

14 Different Views of Data Classes of Operators Constructors Operation that creates new instances of an ADT; usually a language feature Transformers (mutators) Operations that change the state of one or more data values in an ADT Observers Operations that allow us to observe the state of the ADT Iterators Operations that allow us to access each member of a data structure in turn

15 Abstraction and Built-In Types Composite data type A data type that allows a collection of values to be associated with an object of that type Unstructured data type The components of the collection are not organized with respect to each other Structured data type The components of the collection are organized and the organization determines the accessing methods

Abstraction and Built-In Types Components are not organized with respect to one another The organization determines method used to access individual data components UNSTRUCTURED STRUCTURED EXAMPLES: EXAMPLES: arrays classes and structs 16

17 C++ Built-In Data Types Composite array struct union class Address pointer reference Simple IntegralFloating char short int long enum float double long double

18 Abstraction and Built-In Types Record (logical level) A composite data type made up of a finite collection of not necessarily homogeneous elements called members or fields struct CarType { int year; char maker[10]; float price; }; CarType myCar myCar.price dot struct member variable selector

19 Abstraction and Built-In Types Can you define a struct at the application level?

20 Abstraction and Built-In Types Memory configurations

21 Abstraction and Built-In Types Abstract implementation level

22 Abstraction and Built-In Types One-dimensional array A structured composite data type made up of a finite, fixed size collection of ordered homogeneous elements to which direct access is available Logical level int numbers[10]

23 Abstraction and Built-In Types float values[5]; //assume element size is 4 bytes Implementation level This ACCESSING FUNCTION gives position of values[Index] Address(Index) = BaseAddress + Index * SizeOfElement Base Address values[0] values[1] values[2] values[3] values[4] Indexes

24 Abstraction and Built-In Types char name[10]; // assume element size is 1 byte name[0] name[1] name[2] name[3] name[4]..... name[9] Base Address This ACCESSING FUNCTION gives position of name[Index] Address(Index) = BaseAddress + Index * SizeOfElement

25 Abstraction and Built-In Types A two-dimensional array A structured composite data type made up of a finite, fixed size collection of homogeneous elements having relative positions and to which there is direct access logical level int data table[10][6];

26 Abstraction and Built-In Types Application Level Can you think of other applications for two-dimensional arrays ?

27 const int NUM_STATES = 50 ; const int NUM_MONTHS = 12 ; int stateHighs [ NUM_STATES ] [ NUM_MONTHS ] ; C++ stores arrays in row order 12 highs for state 0 12 highs for state 1 etc. Alabama Alaska first row second row Base Address STORAGE... rows columns

28 Abstraction and Built-In Types stateHighs[ 0 ] [ 0 ] stateHighs[ 0 ] [ 1 ] stateHighs[ 0 ] [ 2 ] stateHighs[ 0 ] [ 3 ] stateHighs[ 0 ] [ 4 ] stateHighs[ 0 ] [ 5 ] stateHighs[ 0 ] [ 6 ] stateHighs[ 0 ] [ 7 ] stateHighs[ 0 ] [ 8 ] stateHighs[ 0 ] [ 9 ] stateHighs[ 0 ] [10 ] stateHighs[ 0 ] [11 ] stateHighs[ 1 ] [ 0 ] stateHighs[ 1 ] [ 1 ] stateHighs[ 1 ] [ 2 ] stateHighs[ 1 ] [ 3 ]. To locate an element such as stateHighs [ 2 ] [ 7] the compiler needs to know that there are 12 columns in this two-dimensional array Base Address 8000 If int needs 2 bytes, at what address will stateHighs[2][7] be found?

29 Higher-Level Abstraction Class An unstructured type that encapsulates a fixed number of data components (data members) with the functions (member functions) that manipulate them; its predefined operations on an instance of a class are whole assignment and component access Client Software that declares and manipulates objects (instances) of a particular class In the Fraction case study, what was the client?

30 Higher-Level Abstraction Class specification A specification of the class members (data and functions) with their types and/or parameters Class implementation The code that implements the class functions Why would you want to put them in separate files?

31 Higher-Level Abstraction If a class has a binary function where are the two parameters? What do we mean by "self?" What is the difference between a class and a strut?

32 Object-Oriented Programming objects, sending a message, methods, instance variables…. Object An instance of a class Method A public member function of a class Instance variable A private data member of a class

33 Object-Oriented Programming Three ingredients in any object-oriented language  encapsulation  inheritance  polymorphism Just as a capsule protects its contents, the class construct protects its data members, but what are inheritance and polymorphism ?

34 Object-Oriented Programming Inheritance A mechanism used with a hierarchy of classes in which each descendant class inherits the properties (data and operations) of its ancestor class Base class The class being inherited from Derived class the class that inherits

35 Object-Oriented Programming Inheritance is an "is-a" relationship: a wheeled vehicle is a vehicle; a bicycle is a wheeled vehicle a four-door car is a car…

36 Object-Oriented Programming Binding time The time at which a name or symbol is bound to the appropriate code Static binding The compile-time determination of which implementation of an operation is appropriate Dynamic binding The run-time determination of which implementation of an operation is appropriate

37 Object-Oriented Programming Overloading Giving the same name to more than one function or using the same operation symbol for more than one operation; usually associated with static binding Polymorphism The ability to determine which of several operations with the same name is appropriate; a combination of static and dynamic binding What is the root of the word polymorphism?

38 Object-Oriented Programming Person Employee Manager Each class has a method Print Person.Print just prints the name Employee.Print prints the name and job title Manager.Print prints name, job title, and department Print is overloaded Static binding is when the compiler can tell which Print to use; dynamic binding is when the determination cannot be made until run time

39 Object-Oriented Programming Inheritance and polymorphism work together How? They combine to allow the programmer to build useful hierarchies of classes that can be put into a library to be reused in different applications

40 Constructs for Program Verification Recall: An exception is an unusual situation that occurs when the program is running. Exception Management  Define the error condition  Enclose code containing possible error (try)  Alert the system if error occurs (throw)  Handle error if it is thrown (catch)

41 Constructs for Program Verification try { // code that contains a possible error // try code and throw // string(“Error has occurred in function // …”); } catch (string message) { std::cout << message << std::endl; return 1; }

42 Constructs for Program Verification namespace mySpace { // All variables and functions within // this block must be accessed using // the scope resolution operator (::) } Purpose: Avoid namespace pollution.

43 Constructs for Program Verification Three Ways to Access Members within a Namespace Qualify each reference mySpace::name with every reference Using declaration using mySpace::name; All future references to name refer to mySpace::name Using directive: using namespace mySpace; All members of mySpace can be referenced without qualification

44 Book uses: Qualify names in prototypes and/or function definitions If name used more than once in a function block, use a using declaration If more than one name is used from a namespace, use a using directive Constructs for Program Verification

CALLING BLOCK FUNCTION CALLED Pass-by-value sends a copy of the contents of the actual parameter SO, the actual parameter cannot be changed by the function 45 C++ Tips

CALLING BLOCK FUNCTION CALLED 46 Pass-by-reference sends the location (memory address) of the actual parameter SO, the actual parameter can be changed by the function

47 C++ Tips Arrays as parameters Because all arrays are passed by reference, the & does not have to appear on the parameter list Whenever an array is passed as a parameter, its base address is sent to the called function The size of all dimensions except the first must be included in the function heading and prototype. The sizes of those dimensions for the formal parameter must be exactly the same as in the actual array Why must they be the same?

48 Go back and re-read the Scope Rules of C++ C++ Tips