CS 240: Data Structures Tuesday, June 19 th ADT Requirements (Card), I/O.

Slides:



Advertisements
Similar presentations
Operator Overloading. Introduction Operator overloading –Enabling C++’s operators to work with class objects –Using traditional operators with user-defined.
Advertisements

Stack and Queue Dr. Bernard Chen Ph.D. University of Central Arkansas.
CS 240: Data Structures Thursday, June 21 th Lists – Array based, Link based Dynamic vs Static.
COSC 120 Computer Programming
Chapter 14: Overloading and Templates
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 12A Separate Compilation and Namespaces For classes this time.
1 Chapter 11 Structured Types, Data Abstraction and Classes Dale/Weems/Headington.
CS 240: Data Structures Thursday, June 21 th Vector, Linked List.
17 File Processing. OBJECTIVES In this chapter you will learn:  To create, read, write and update files.  Sequential file processing.  Random-access.
CS-341 Dick Steflik Introduction. C++ General purpose programming language A superset of C (except for minor details) provides new flexible ways for defining.
Review on pointers and dynamic objects. Memory Management  Static Memory Allocation  Memory is allocated at compiling time  Dynamic Memory  Memory.
1 Data Structures Data Structures Topic #2. 2 Today’s Agenda Data Abstraction –Given what we talked about last time, we need to step through an example.
Object Oriented Programming C++. ADT vs. Class ADT: a model of data AND its related functions C++ Class: a syntactical & programmatic element for describing.
Object Oriented Programming C++. ADT vs. Class ADT: a model of data AND its related functions C++ Class: a syntactical & programmatic element for describing.
C++ crash course Class 3 designing C++ programs, classes, control structures.
1 CSC241: Object Oriented Programming Lecture No 07.
Streams, Files. 2 Stream Stream is a sequence of bytes Input stream In input operations, the bytes are transferred from a device to the main memory Output.
C++ Functions. 2 Agenda What is a function? What is a function? Types of C++ functions: Types of C++ functions: Standard functions Standard functions.
Stream Handling Streams - means flow of data to and from program variables. - We declare the variables in our C++ for holding data temporarily in the memory.
By – Tanvir Alam.  This tutorial offers several things.  You’ll see some neat features of the language.  You’ll learn the right things to google. 
Chapter 8 Friends and Overloaded Operators. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Slide 2 Overview Friend Function (8.1) Overloading.
More on Input Output Input Stream : A sequence of characters from an input device (like the keyboard) to the computer (the program running). Output Stream.
1 Programs Composed of Several Functions Syntax Templates Legal C++ Identifiers Assigning Values to Variables Declaring Named Constants String Concatenation.
ITEC 320 C++ Examples.
Copyright  Hannu Laine C++-programming Part 1 Hannu Laine.
C++ Review (3) Structs, Classes, Data Abstraction.
CS107 References and Arrays By Chris Pable Spring 2009.
1 CS161 Introduction to Computer Science Topic #13.
FILE HANDLING IN C++.
1 Advanced Issues on Classes Part 3 Reference variables (Tapestry pp.581, Horton 176 – 178) Const-reference variables (Horton 176 – 178) object sharing:
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
1 C++ Programming Basics Chapter 1 Lecture CSIS 10A.
Monday, Mar 31, 2003Kate Gregory with material from Deitel and Deitel Week 12 Labs 4 and 5 are back File IO Looking ahead to the final.
An Introduction to Programming with C++ Sixth Edition Chapter 14 Sequential Access Files.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
CSE 332: C++ IO We’ve Looked at Basic Input and Output Already How to move data into and out of a program –Using argc and argv to pass command line args.
CS 11 C++ track: lecture 1 Administrivia Need a CS cluster account sysadmin/account_request.cgi Need to know UNIX (Linux)
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays.
Templates “Generic Programming” ECE Templates A way to write code once that works for many different types of variables –float, int, char, string,
Chapter 11 Friends and Overloaded Operators. Introduction to function equal // Date.h #ifndef _DATE_H_ #define _DATE_H_ class CDate { public: CDate();
1 Today’s Objectives  Announcements Homework #3 is due on Monday, 10-Jul, however you can earn 10 bonus points for this HW if you turn it in on Wednesday,
Classes & Objects Lecture-6. Classes and Objects A class is a 'blueprint' for all Objects of a certain type (defined by ADT) class defines the attributes.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 15: Overloading and Templates.
1 CS161 Introduction to Computer Science Topic #16.
Chapter 9 Separate Compilation and Namespaces. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Slide 2 Overview Separate Compilation (9.1)
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 12 Separate Compilation and Namespaces.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 12 Separate Compilation and Namespaces.
1 What is a Named Constant? A named constant is a location in memory that we can refer to by an identifier, and in which a data value that cannot be changed.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 15. Dictionaries (1): A Key Table Class.
Sahar Mosleh California State University San MarcosPage 1 One Dimensional Arrays: Structured data types.
Java & C++ Comparisons How important are classes and objects?? What mechanisms exist for input and output?? Are references and pointers the same thing??
Manipulator example #include int main (void) { double x = ; streamsize prec = cout.precision(); cout
Learning Objectives Fundamentals of Operator Overloading. Restrictions of Operator Overloading. Global and member Operator. Overloading Stream-Insertion.
72 4/11/98 CSE 143 Abstract Data Types [Sections , ]
Std Library of C++ Part 2. vector vector is a collection of objects of a single type vector is a collection of objects of a single type Each object in.
Files To read a file – We must know its name – We must open it (for reading) – Then we can read – Then we must close it That is typically done implicitly.
Fundamental Programming Fundamental Programming Introduction to Functions.
17-1 Computing Fundamentals with C++ Object-Oriented Programming and Design, 2nd Edition Rick Mercer Franklin, Beedle & Associates, 1999 ISBN
CS 240Chapter 10 – TreesPage Chapter 10 Trees The tree abstract data type provides a hierarchical to the representation of certain types of relationships.
Module 5: I/O and Strings #1 2000/01Scientific Computing in OOCourse code 3C59 Module 5: I/O and STRINGS In this module we will cover The C++ input and.
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.
Chapter 12 Classes and Abstraction
Chapter 14: Sequential Access Files
Lecture 9 Files, Pointers
Basic Input and Output Operations
File I/O in C++ I.
CS150 Introduction to Computer Science 1
CHAPTER 4 File Processing.
Class rational part2.
File I/O in C++ I.
Presentation transcript:

CS 240: Data Structures Tuesday, June 19 th ADT Requirements (Card), I/O

Terminology Here is some stuff to memorize: Here is some stuff to memorize: “Container Class” “Container Class” mycontainer is a type of “Container Class” mycontainer is a type of “Container Class” “Sequential Container” “Sequential Container” An array is a sequential container so is mycontainer. An array is a sequential container so is mycontainer. “Coercion” and “Casting” “Coercion” and “Casting”

Some Material This is a list of some material which I can cover if you want but already expect you all to know as part of the background courses. This is a list of some material which I can cover if you want but already expect you all to know as part of the background courses. Binary representation Binary representation Also, real values (you do not need to translate, just understand that the binary is an abstraction) Also, real values (you do not need to translate, just understand that the binary is an abstraction) Logic Logic And, or, xor, nor, etc And, or, xor, nor, etc I may draw gates, but I’ll give you a key I may draw gates, but I’ll give you a key

ADTs An ADT is a collection of data that represents some sort of abstraction. An ADT is a collection of data that represents some sort of abstraction. A person is represented by: A person is represented by: Name, age, height, etc Name, age, height, etc A book “can” be represented by: A book “can” be represented by: Title, ISBN, author, year Title, ISBN, author, year

ADTs However, an ADT isn’t too useful if it just stores data and nothing else. However, an ADT isn’t too useful if it just stores data and nothing else. All this would do is allow you to organize data better: All this would do is allow you to organize data better:

Book ADT #ifndef BOOK_H #define BOOK_H class Book {public: string title; string ISBN; string author; int year; };#endif This just stores data. This just stores data. A “storage only” ADT can have public variables/members. A “storage only” ADT can have public variables/members. A “storage only” ADT has no associated functions; therefore, it has no cpp file. A “storage only” ADT has no associated functions; therefore, it has no cpp file. (Optional): In C, this instead be a “struct” (Optional): In C, this instead be a “struct”

Book ADT #ifndef BOOK_H #define BOOK_H class Book {public: string title; string ISBN; string author; int year; };#endif #include<iostream>#include”Book.h” using namespace std; int main() { Book mylibrary[10]; string intitle,inISBN,inauthor; int inyear; for(int i=0;i<10;i++) { cin >> intitle >> inISBN; cin >> inauthor >> inyear; mylibrary[i].title=intitle;mylibrary[i].ISBN=inISBN;mylibrary[i].author=inauthor;mylibrary[i].year=inyear;} cout << mylibrary[0].title <<endl; cout << mylibrary[0].ISBN <<endl; }

Book ADT Manipulating this book can become annoying. Manipulating this book can become annoying. Therefore, we turn it into a Data Structure by associating functions with it. Therefore, we turn it into a Data Structure by associating functions with it.

Book ADT How can we simplify the input loop? How can we simplify the input loop? It would be annoying if every time we wanted to use the Book class we would have to copy that entire set of assignment instructions. It would be annoying if every time we wanted to use the Book class we would have to copy that entire set of assignment instructions. There are two approaches to this: There are two approaches to this: 1) Create an input function (low-level) 1) Create an input function (low-level) 2) Overload input to allow direct access (high-level) 2) Overload input to allow direct access (high-level)

#ifndef BOOK_H #define BOOK_H #include<iostream> using namespace std; class Book {public: void input(istream & in); void display(ostream & out); private: string title; string ISBN; string author; int year; }; istream & operator >>(istream & in, Book & target); ostream & operator <<(ostream & out, Book & target); #endif void Book::input(istream & in) { in >> title; in >> ISBN; in >> author; in >> year; } void Book::display(ostream & out) { out << title << “ “ << ISBN << “ “; out << author << “ “ << year <<endl; } istream? istream stands for “input stream” We can tell the function what input stream to use: cin, or some other stream. The syntax of operator >> requires memorization.

#ifndef BOOK_H #define BOOK_H #include<iostream> using namespace std; class Book {public: void input(istream & in); void display(ostream & out); private: string title; string ISBN; string author; int year; }; istream & operator >>(istream & in, Book & target); ostream & operator <<(ostream & out, Book & target); #endif istream & operator >>(istream &in, Book & target) {target.input(in); return in; } ostream & operator <<(ostream & out, Book & target) {target.display(out); return out; } Really, we should also be able to return a string but we won’t worry about that for this example.

Now we can update our code: Instead of: Instead of:#include<iostream>#include”Book.h” using namespace std; int main() { Book mylibrary[10]; string intitle,inISBN,inauthor; int inyear; for(int i=0;i<10;i++) { cin >> intitle >> inISBN; cin >> inauthor >> inyear; mylibrary[i].title=intitle;mylibrary[i].ISBN=inISBN;mylibrary[i].author=inauthor;mylibrary[i].year=inyear;} cout << mylibrary[0].title <<endl; cout << mylibrary[0].ISBN <<endl; } Now: Now:#include<iostream>#include”Book.h” using namespace std; int main() { Book mylibrary[10]; string intitle,inISBN,inauthor; int inyear; for(int i=0;i<10;i++) { cin >> mylibrary[i]; } for(int i=0;i<10;i++) { cout << mylibrary[i]; } return 0; }

Or: Or:#include<iostream>#include”Book.h” using namespace std; int main() { Book mylibrary[10]; string intitle,inISBN,inauthor; int inyear; for(int i=0;i<10;i++) {mylibrary[i].input(cin);} {mylibrary[i].display(cout);} return 0; }

Rationale Why do we do the second method? Why do we do the second method? Well, it allows us to see more readily what is going on. Well, it allows us to see more readily what is going on. We call the input method with cin. We call the input method with cin. We call the display method with cout. We call the display method with cout. Alternatively, we can give these functions other streams. Alternatively, we can give these functions other streams.

I/O Streams The streams we have been exposed to so far: The streams we have been exposed to so far: cin and cout cin and cout However, there are other streams we can use: However, there are other streams we can use: ifstream and ofstream ifstream and ofstream These are for file input and file output These are for file input and file output

File I/O Example ifstream myfile; string toopen = “filename.txt”; myfile.open(toopen.c_str(),ios::in); string input; myfile >> input; myfile.close();

File I/O Example ofstream myfile; string toopen = “filename.txt”; myfile.open(toopen.c_str(),ios::out); string output = “I’m writing data”; myfile << output; myfile.close();

Card ADT Let us come up with a specification for a playing card. Then we can code it: Let us come up with a specification for a playing card. Then we can code it: We need to come up with the important abstractions that relate to a playing card. We need to come up with the important abstractions that relate to a playing card. We also need to take into consideration how the Card is managed in memory. We also need to take into consideration how the Card is managed in memory. We will also examine the three required methods of most ADTs We will also examine the three required methods of most ADTs Copy constructor, assignment operator and destructor Copy constructor, assignment operator and destructor Also, what would a container class of Card require? Also, what would a container class of Card require?

What is a primitive? A primitive is a built-in data type. A primitive is a built-in data type. Generally, it has complete functionality. Generally, it has complete functionality. They have a value and are located in memory. They have a value and are located in memory. During compilation, they have a name. During compilation, they have a name. Only a memory address is used while running. Only a memory address is used while running. Primitive Type: int Value (32 bits): ? Address: ? Primitive Type: char Value (8 bits): ? Address: ?

Data Manipulation Therefore, we use the address of the data to locate the value we want. Therefore, we use the address of the data to locate the value we want. How about an array of ints? How about an array of ints? Ok, so if we know where the array starts we can find successive data. Ok, so if we know where the array starts we can find successive data. Address:ii+4i+8i+12i+16i+20i+24 Value:

Data Manipulation An array is a form of a data abstraction. An array is a form of a data abstraction. class array { T value; memory_address next_data; memory_address own_location; }; Where next_data is always equal to the size of T. This is done automatically when we use array implementation (this abstraction only works in theory since the next_location would be offset by the other class members). If you created an array of this time (and set next_data to size of array instead of T), it would mirror the real implementation.

Data Manipulation These abstractions allow us to group data together so that we access data we can get more than 1 piece of information. These abstractions allow us to group data together so that we access data we can get more than 1 piece of information. Primitive Type: int Value (32 bits): ? Address: ? ADT: array (int) 96 bits (32 bits) -> int (32 bits) -> 12 (32 bits) -> *this

array testdata[3]; ADT: array (int) 96 bits (32 bits) -> int (5) (32 bits) -> 12 (32 bits) -> *this (X) Address: X ADT: array (int) 96 bits (32 bits) -> int (10) (32 bits) -> 12 (32 bits) -> *this (X+12) Address: X + 12 ADT: array (int) 96 bits (32 bits) -> int (15) (32 bits) -> 12 (32 bits) -> *this (X+24) Address: X + 24 This actually requires array to have a constructor to set: next_data = sizeof(array); own_location = *this; Therefore: &testdata[0] = &testdata[0].own_location; &testdata[1] = &testdata[1].own_location; and&testdata[1] = &testdata[0].own_location + testdata[0].next_data; This is a representation of how it is actually done in memory.

Data Manipulation You wouldn’t implement an array like we did in the last slide. However, we using it slightly differently we can achieve a differently goal. You wouldn’t implement an array like we did in the last slide. However, we using it slightly differently we can achieve a differently goal.

array testdata[3]; ADT: array (int) 96 bits (32 bits) -> int (5) (32 bits) -> 24 (32 bits) -> *this (X) Address: X ADT: array (int) 96 bits (32 bits) -> int (10) (32 bits) -> 12 (32 bits) -> *this (X+12) Address: X + 12 ADT: array (int) 96 bits (32 bits) -> int (15) (32 bits) -> -12 (32 bits) -> *this (X+24) Address: X + 24 If we use [ ], we will get all the data in the same order as before: However, if we use next_data: We now have a new representation – an array-based linked list

We don’t need an array… ADT: array (int) 96 bits (32 bits) -> int (5) (32 bits) -> Z-X (32 bits) -> *this (X) Address: X ADT: array (int) 96 bits (32 bits) -> int (10) (32 bits) -> -Y (32 bits) -> *this (Y) Address: Y ADT: array (int) 96 bits (32 bits) -> int (15) (32 bits) -> Y-Z (32 bits) -> *this (Z) Address: Z In our assignments we won’t use “our_location” We always know where the first item is and can find the remaining items by using next_item.

Without our_location ADT: array (int) 96 bits (32 bits) -> int (5) (32 bits) -> Z Address: X ADT: array (int) 96 bits (32 bits) -> int (10) (32 bits) -> 0 Address: Y ADT: array (int) 96 bits (32 bits) -> int (15) (32 bits) -> Y Address: Z If our first item is at address X then,(5) we find the second item by using next_item(15) and the third item by using next_item of the second(10) and since third_item.next_item == 0, we are done.