Software Design Abstract Data Types

Similar documents
Inheritance and Polymorphism

Unified Modeling Language a case study

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

Lists. linking nodes. constructors. chasing pointers. MCS 360 Lecture 11 Introduction to Data Structures Jan Verschelde, 17 September 2010.

CSCE 110 PROGRAMMING FUNDAMENTALS

Abstract Data Types (ADT) and C++ Classes

Operator Overloading

selectors, methodsinsert() andto_string() the depth of a tree and a membership function

Graphs. directed and undirected graphs weighted graphs adjacency matrices. abstract data type adjacency list adjacency matrix

Classes: A Deeper Look, Part 2

CS 1337 Computer Science II Page 1

Queue Implementations

B-Trees. nodes with many children a type node a class for B-trees. an elaborate example the insertion algorithm removing elements

Expression Trees and the Heap

EECE.3220: Data Structures Spring 2017

Unit Testing. Contents. Steven Zeil. July 22, Types of Testing 2. 2 Unit Testing Scaffolding Drivers Stubs...

Unit Testing. Steven Zeil. July 22, Types of Testing 2. 2 Unit Testing Scaffolding Drivers Stubs...

CSCE 110 PROGRAMMING FUNDAMENTALS

Computer Programming with C++ (21)

A Deeper Look at Classes

the Stack stack ADT using the STL stack are parentheses balanced? algorithm uses a stack adapting the STL vector class adapting the STL list class

Lab 2: ADT Design & Implementation

Encapsulation. Contents. Steven Zeil. July 17, Encapsulation Encapsulation in C Classes 4. 3 Hiding Attributes 8

Due Date: See Blackboard

ADTs & Classes. An introduction

Introduction to Programming session 24

Classes and Data Abstraction. Topic 5

l A class in C++ is similar to a structure. - It allows you to define a new (composite) data type. l A class contains the following: - variables AND

Templates and Vectors

Elements of C in C++ data types if else statement for loops. random numbers rolling a die

! Data is stored in variables. - Perhaps using arrays and structs. ! Program is a collection of functions that perform

Iterators. node UML diagram implementing a double linked list the need for a deep copy. nested classes for iterator function objects

Operator Overloading

! Data is stored in variables. - Perhaps using arrays and structs. ! Program is a collection of functions that perform

Jordan University of Science & Technology Department of Computer Science CS 211 Exam #1 (23/10/2010) -- Form A

1 // Fig. 6.13: time2.cpp 2 // Member-function definitions for class Time. 3 #include <iostream> Outline. 4 5 using std::cout; 6 7 #include <iomanip>

The University Of Michigan. EECS402 Lecture 09. Andrew M. Morgan. Savitch Ch Compiling With Multiple Files Make Utility.

Arrays. Week 4. Assylbek Jumagaliyev

Classes: A Deeper Look

the Queue queue ADT using the STL queue designing the simulation simulation with STL queue using STL list as queue using STL vector as queue

CSE 333 Midterm Exam Nov. 3, 2017 Sample Solution

Agenda. The main body and cout. Fundamental data types. Declarations and definitions. Control structures

Getting started with C++ (Part 2)

! Data is stored in variables. - Perhaps using arrays and structs. ! Program is a collection of functions that perform

Linked List using a Sentinel

Data Structures and Algorithms

Assignment 2 Solution

Due Date: See Blackboard

Practice Problems CS2620 Advanced Programming, Spring 2003

2.1 Introduction UML Preliminaries Class diagrams Modelling delegation... 4

CS 216 Fall 2007 Midterm 1 Page 1 of 10 Name: ID:

Chapter 18 - C++ Operator Overloading

CMSC 202 Midterm Exam 1 Fall 2015

Due Date: See Blackboard

! Data is stored in variables. - Perhaps using arrays and structs. ! Program is a collection of functions that perform

Stack Applications. parsing a postfix expression evaluating postfix expressions. converting infix sums from infix to postfix

CSE 333 Midterm Exam July 24, Name UW ID#

Introduction to Classes

Due Date: See Blackboard

! Data is stored in variables. - Perhaps using arrays and structs. ! Program is a collection of functions that perform

CS 247: Software Engineering Principles. Modules

Due Date: See Blackboard

what are strings today: strings strings: output strings: declaring and initializing what are strings and why to use them reading: textbook chapter 8

III. Classes (Chap. 3)

l A class in C++ is similar to a structure. - It allows you to define a new (composite) data type. l A class contains the following: - variables AND

l Determine if a number is odd or even l Determine if a number/character is in a range - 1 to 10 (inclusive) - between a and z (inclusive)

2. First Program Stuff

Abstraction in Software Development

IV. Stacks. A. Introduction 1. Consider the 4 problems on pp (1) Model the discard pile in a card game. (2) Model a railroad switching yard

this Pointer, Constant Functions, Static Data Members, and Static Member Functions this Pointer (11.1) Example of this pointer

Simulations. buffer of fixed capacity using the STL deque. modeling arrival times generating and processing jobs

Wentworth Institute of Technology COMP201 Computer Science II Spring 2015 Derbinsky. Stacks and Queues. Lecture 11.

CS 132 Exam #1 - Study Suggestions

Copying Data. Contents. Steven J. Zeil. November 13, Destructors 2

Hashing. mapping data to tables hashing integers and strings. aclasshash_table inserting and locating strings. inserting and locating strings

! Determine if a number is odd or even. ! Determine if a number/character is in a range. - 1 to 10 (inclusive) - between a and z (inclusive)

moretosearch = (location < length);

Priority Queues and Huffman Trees

CS 240 Data Structure Spring 2018 Exam I 03/01/2018

CSI33 Data Structures

C How to Program, 6/e by Pearson Education, Inc. All Rights Reserved. 1

Outline. Computer programming. Usage of time and date functions. Date and Time: time.h. Date and Time: time.h. Time functions:

CSI33 Data Structures

Review Questions for Final Exam

EX3 Code Review. Instructor: Tsung-Che Chiang

Computer programming. "An expert is a man who has made all the mistakes which can be made, in a narrow field." Niels Bohr

Implementing an ADT with a Class

! Determine if a number is odd or even. ! Determine if a number/character is in a range. ! Assign a category based on ranges (wind speed)

C++ For Science and Engineering Lecture 15

Ich habe in allen Klassen konsequent ClassInvariant umgesetzt, als Beispiel dient hier eine der komplexeren Versionen, nämlich die in CHouse:

double d0, d1, d2, d3; double * dp = new double[4]; double da[4];

Object Oriented Design

Programming in C/C Lecture 3

BITG 1233: Introduction to C++

Variables. Data Types.

Operator Overloading in C++ Systems Programming

Classes: A Deeper Look. Systems Programming

Datastructuren André Deutz

Week 3: Pointers (Part 2)

Hashing. inserting and locating strings. MCS 360 Lecture 28 Introduction to Data Structures Jan Verschelde, 27 October 2010.

Transcription:

Software Design Abstract Data Types 1 Software Design top down, bottom up, object-oriented abstract data types 2 Specifying a ClassClock date and time in a C++ program encapsulating C code public and private parts, a friend operator 3 Implementing the ClassClock the.cpp file thethis pointer MCS 360 Lecture 4 Introduction to Data Structures Jan Verschelde, 6 September 2017 Introduction to Data Structures (MCS 360) Abstract Data Types L-4 6 September 2017 1 / 31

Abstract Data Types 1 Software Design top down, bottom up, object-oriented abstract data types 2 Specifying a ClassClock date and time in a C++ program encapsulating C code public and private parts, a friend operator 3 Implementing the ClassClock the.cpp file thethis pointer Introduction to Data Structures (MCS 360) Abstract Data Types L-4 6 September 2017 2 / 31

Software Design C supports top down and bottom up design: top down: main program divided in tasks, the tasks are implemented by functions; bottom up: libraries of functions on data types e.g.: mathematical libraries for linear algebra (LINPACK, EISPACK, LAPACK). C++ is an object-oriented language: 1 model real-world entities by objects, 2 every object belongs to a class, 3 a class has data and functional attributes. Introduction to Data Structures (MCS 360) Abstract Data Types L-4 6 September 2017 3 / 31

Unified Modeling Language (UML) UML is a graphical language to model, design, and construct object-oriented software. A class diagram: name of class data methods Three principles of good design: 1 information hiding 2 low coupling and high cohesion 3 design for change Introduction to Data Structures (MCS 360) Abstract Data Types L-4 6 September 2017 4 / 31

class diagram for strings string char* string() string() =, ==, [], +, < length() find() substr() c_str() Introduction to Data Structures (MCS 360) Abstract Data Types L-4 6 September 2017 5 / 31

Abstract Data Types 1 Software Design top down, bottom up, object-oriented abstract data types 2 Specifying a ClassClock date and time in a C++ program encapsulating C code public and private parts, a friend operator 3 Implementing the ClassClock the.cpp file thethis pointer Introduction to Data Structures (MCS 360) Abstract Data Types L-4 6 September 2017 6 / 31

Abstract Data Types A class implements an abstract data type (ADT). An ADT defines the interface of a class, what is public of the data and methods. An ADT is a contract between software designer and the programmer who writes the code. For every operation we specify a precondition: requirements on the input parameters, a postcondition: properties of the values on output. If preconditions hold on input and the implementation of the function is correct, then the postconditions hold on output. Introduction to Data Structures (MCS 360) Abstract Data Types L-4 6 September 2017 7 / 31

specifyingfind The methodfind() on any string could be specified as follows: size_t find(const string& t, size_t p) const; /* Preconditions: t is target we search for in a string; p is the start position for the search, if omitted, then the search starts at 0. Postcondition: if r == s.find(t,p) then either s[r] == t if t occurs in s, or r == string::npos otherwise. */ Note: -1 size_t. s.find() does not change s: so const at end Introduction to Data Structures (MCS 360) Abstract Data Types L-4 6 September 2017 8 / 31

specifyingsubstr The methodsubstr() on any string could be specified as follows: string substr(size_t p, size_t n = string::nops) const; /* Preconditions: p is a start position in the string; n is the number of characters of the substring on return, may be omitted. Postcondition: if ss == s.substr(p) then ss[k] == s[p+k], k in 0..s.length()-p-1 if ss == s.substr(p,n) then ss[k] == s[p+k], k in 0..n-1. */ Introduction to Data Structures (MCS 360) Abstract Data Types L-4 6 September 2017 9 / 31

Abstract Data Types 1 Software Design top down, bottom up, object-oriented abstract data types 2 Specifying a ClassClock date and time in a C++ program encapsulating C code public and private parts, a friend operator 3 Implementing the ClassClock the.cpp file thethis pointer Introduction to Data Structures (MCS 360) Abstract Data Types L-4 6 September 2017 10 / 31

date and time Typingdate at the command prompt of a Unix(-like) OS show date and the current time. We can get this information in a C++ program. $ current_time Sun Aug 29 16:01:35 2010 the raw time is 1283115695 The current date is 2010/08/29. The current time is 16:01:35. $ Introduction to Data Structures (MCS 360) Abstract Data Types L-4 6 September 2017 11 / 31

date as a string #include<ctime> #include<iostream> #include<iomanip> using namespace std; int main() { long int now = time(0); // current time string s = asctime(localtime(&now)); cout << s; Introduction to Data Structures (MCS 360) Abstract Data Types L-4 6 September 2017 12 / 31

thetm structure struct tm *tm_ptr; // tm_ptr = gmtime(&now); // Greenwich Mean time tm_ptr = localtime(&now); cout << "the raw time is " << now << endl; cout << setfill( 0 ); cout << "The current date is " << 1900 + tm_ptr -> tm_year << "/" << setw(2) << 1 + tm_ptr -> tm_mon << "/" << setw(2) << tm_ptr -> tm_mday << "." << endl; cout << "The current time is " << setw(2) << tm_ptr -> tm_hour << ":" << setw(2) << tm_ptr -> tm_min << ":" << setw(2) << tm_ptr -> tm_sec << "." << endl; Introduction to Data Structures (MCS 360) Abstract Data Types L-4 6 September 2017 13 / 31

Abstract Data Types 1 Software Design top down, bottom up, object-oriented abstract data types 2 Specifying a ClassClock date and time in a C++ program encapsulating C code public and private parts, a friend operator 3 Implementing the ClassClock the.cpp file thethis pointer Introduction to Data Structures (MCS 360) Abstract Data Types L-4 6 September 2017 14 / 31

the main C++ program $ time_with_clock The current time is 17:35:32. $ #include "a_class_clock.h" #include<iostream> using namespace std; int main() { Clock c; cout << "The current time is " << c << "." << endl; } return 0; Introduction to Data Structures (MCS 360) Abstract Data Types L-4 6 September 2017 15 / 31

defining a class #ifndef A_CLASS_CLOCK_H #define A_CLASS_CLOCK_H class Clock { public: // exported data and members }; private: // hidden class attributes #endif Introduction to Data Structures (MCS 360) Abstract Data Types L-4 6 September 2017 16 / 31

.h and.cpp files The header filea_class_clock.h contains the prototypes of all member functions. In the filea_class_clock.cpp: #include "a_class_clock.h" If we save the main program intime_with_clock.cpp then we compile at the command prompt$as $ g++ -c a_class_clock.cpp $ g++ -o time_with_clock \ a_class_clock.o time_with_clock.cpp Introduction to Data Structures (MCS 360) Abstract Data Types L-4 6 September 2017 17 / 31

a makefile defines the compilation instructions For our example we define amakefile: gpp=/usr/bin/g++ time_with_clock: $(gpp) -c a_class_clock.cpp $(gpp) -c time_with_clock.cpp $(gpp) a_class_clock.o time_with_clock.o \ -o /tmp/time_with_clock Then typingmake time_with_clock at the command line will execute the compilation and linking commands. Withgpp = /usr/bin/g++ we define a specific compiler. Instead ofg++ we write$(gpp) to compile. Observe the TABs after before the compilation statements. Introduction to Data Structures (MCS 360) Abstract Data Types L-4 6 September 2017 18 / 31

Abstract Data Types 1 Software Design top down, bottom up, object-oriented abstract data types 2 Specifying a ClassClock date and time in a C++ program encapsulating C code public and private parts, a friend operator 3 Implementing the ClassClock the.cpp file thethis pointer Introduction to Data Structures (MCS 360) Abstract Data Types L-4 6 September 2017 19 / 31

public part ofclock public: Clock(); /* sets the clock to the current time */ void set_clock(int h, int m, int s); /* sets clock to hour given in h, minutes to m, and seconds to s. */ int get_hours() const; /* returns the value of hours */ int get_minutes() const; /* returns the value of minutes */ int get_seconds() const; /* returns the value of seconds */ Introduction to Data Structures (MCS 360) Abstract Data Types L-4 6 September 2017 20 / 31

a friend method To define the insertion operator: friend std::ostream& operator<< (std::ostream& os, const Clock& c); /* defines output of an object clock */ Afriend method does not apply to an object to the class, but has access to all attributes of the object. Introduction to Data Structures (MCS 360) Abstract Data Types L-4 6 September 2017 21 / 31

private parts private: int hours; /* hours in 24 hour format integer in the range 0..23 */ int minutes; /* minutes in range 0..59 */ int seconds; /* integers in range 0..61 allowing for leap second */ Principle of information hiding: only throughget_hours() do we get the value ofhours changing of hours is done only via methods defined in the class. Introduction to Data Structures (MCS 360) Abstract Data Types L-4 6 September 2017 22 / 31

Abstract Data Types 1 Software Design top down, bottom up, object-oriented abstract data types 2 Specifying a ClassClock date and time in a C++ program encapsulating C code public and private parts, a friend operator 3 Implementing the ClassClock the.cpp file thethis pointer Introduction to Data Structures (MCS 360) Abstract Data Types L-4 6 September 2017 23 / 31

a_class_clock.cpp #include "a_class_clock.h" #include<ctime> #include<iomanip> Clock::Clock() { long int now = time(0); struct tm *tm_ptr; } tm_ptr = localtime(&now); hours = tm_ptr -> tm_hour; minutes = tm_ptr -> tm_min; seconds = tm_ptr -> tm_sec; Introduction to Data Structures (MCS 360) Abstract Data Types L-4 6 September 2017 24 / 31

the other methods void Clock::set_clock(int h, int m, int s) { hours = h; minutes = m; seconds = s; } int Clock::get_hours() const { return hours; } int Clock::get_minutes() const { return minutes; } int Clock::get_seconds() const { return seconds; } Introduction to Data Structures (MCS 360) Abstract Data Types L-4 6 September 2017 25 / 31

defining the output std::ostream& operator<< (std::ostream& os, const Clock& c) { os << std::setfill( 0 ) << std::setw(2) << c.get_hours() << ":" << std::setw(2) << c.get_minutes() << ":" << std::setw(2) << c.get_seconds(); return os; } Introduction to Data Structures (MCS 360) Abstract Data Types L-4 6 September 2017 26 / 31

Abstract Data Types 1 Software Design top down, bottom up, object-oriented abstract data types 2 Specifying a ClassClock date and time in a C++ program encapsulating C code public and private parts, a friend operator 3 Implementing the ClassClock the.cpp file thethis pointer Introduction to Data Structures (MCS 360) Abstract Data Types L-4 6 September 2017 27 / 31

defining equality Add topublic: of theclass Clock in the filea_class_clock.h: bool Clock::operator==(const Clock& c) const; /* returns true if the time stored at the clock equals the time stored at c */ Testing intime_with_clock.cpp: Clock d; d.set_clock(16,52,3); bool test_eq = (c == d); cout << c << " == " << d << " is " << test_eq << endl; Introduction to Data Structures (MCS 360) Abstract Data Types L-4 6 September 2017 28 / 31

thethis parameter this is a pointer to the object to which we send the member function: bool Clock::operator==(const Clock& c) const { if((this->hours == c.hours) && (this->minutes == c.minutes) && (this->seconds == c.seconds)) return true; else return false; } Notation: this->minutes == (*this).minutes. Note that*this == c can be interpreted as the comparison of a pointer with a reference. Introduction to Data Structures (MCS 360) Abstract Data Types L-4 6 September 2017 29 / 31

Summary + Exercises We started Chapter 1: Introduction to Software Design. Classes in C++ are a way to implement ADTs. Exercises: 1 Extend the classclock with a methodupdate() that sets the data attributes to the hours, minutes, and seconds of the current time. 2 Write a C++ program that prompts the user to hit the enter key twice. Show the elapsed (wall clock) time between the two hits. 3 Extend the classclock with the<operator. 4 Describe how to extend the classclock so it serves as an alarm clock. Introduction to Data Structures (MCS 360) Abstract Data Types L-4 6 September 2017 30 / 31

three more exercises 5 Consider a bubble gum dispenser. The dispenser releases one bubble gum at a time until empty. Filling of the dispenser adds a positive number of bubble gums. Write an Abstract Data Type for a bubble gum dispenser. Draw the UML class diagram. 6 Consider the bubble gum dispenser of the previous exercise. Define a C++ class for a bubble gum dispenser object. The number of bubble gums in the dispenser is private. 7 Consider again the bubble gum displenser of the previous exercise. Write an implementation for the class. Write a simple test program to demonstrate that your class is implemented correctly. Introduction to Data Structures (MCS 360) Abstract Data Types L-4 6 September 2017 31 / 31