introduction course outline and references grading and policy object oriented concept revision ...

71
File Organization & processing By: [email protected] Dr. Mohamamd El-Ramly Many slides by Dr. Hussien CS 215 Lecture 1 Introduction Cairo University FCI 2014

Upload: barry-logan

Post on 14-Jan-2016

213 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Introduction  Course outline and references  Grading and policy  Object oriented concept revision  String manipulation revision Dr. Hussien M. Sharaf

File Organization & processing

By: [email protected]

Dr. Mohamamd El-RamlyMany slides by Dr. Hussien Sharaf

CS 215

Lecture 1 Introduction

Cairo UniversityFCI

2014

Page 2: Introduction  Course outline and references  Grading and policy  Object oriented concept revision  String manipulation revision Dr. Hussien M. Sharaf

IntroductionCourse outline and referencesGrading and policy

Object oriented concept revision

String manipulation revision

Lecture Outline

Dr. Hussien M. Sharaf 2

Page 3: Introduction  Course outline and references  Grading and policy  Object oriented concept revision  String manipulation revision Dr. Hussien M. Sharaf

النية جدد

له الله سهل علما فيه يلتمس طريقا سلك منالجنة إلى طريقا

.... بيت يهدم والجهل لــه عماد ال Rبيتا يرفع العلموالكرم العز

يفتح ونبراس المتخلفة العقول يضيء نور العلمالمظلمة الدروب

لندن طاعون قصة اصنعه لكن و التغيير تنتظر ال الخيول مزرعة صاحب قصة

Page 4: Introduction  Course outline and references  Grading and policy  Object oriented concept revision  String manipulation revision Dr. Hussien M. Sharaf

I am ……

Mohammad El-Ramly Assistant Professor of Computer Sciences Specialization: Software Engineering B.Sc. of Computer Engineering, Ain Shams

University, Cairo. M.Sc. of Operations Research, Cairo

University. Ph.D. of Computer Science, University of

Alberta, Canada. Who are you? … … ….

Page 5: Introduction  Course outline and references  Grading and policy  Object oriented concept revision  String manipulation revision Dr. Hussien M. Sharaf

Where are we ?

This course is in the second batch (3 courses) of software development track.

◦Programming 1 (CS112)◦Programming 2 (CS213)◦Data Structures (CS214)◦File Organization and Processing (CS215)◦Databases Systems 1 (IS211)◦Software Engineering 1 (CS251)◦Software Engineering 2 (CS352)

Page 6: Introduction  Course outline and references  Grading and policy  Object oriented concept revision  String manipulation revision Dr. Hussien M. Sharaf

Where are we ?

Data Structures (CS214)

◦Organization of data in the main memory for faster access.

File Organization and Processing (CS215)

◦Organization of data on secondary storage for faster access.

Page 7: Introduction  Course outline and references  Grading and policy  Object oriented concept revision  String manipulation revision Dr. Hussien M. Sharaf

Salaries ?

Page 8: Introduction  Course outline and references  Grading and policy  Object oriented concept revision  String manipulation revision Dr. Hussien M. Sharaf
Page 9: Introduction  Course outline and references  Grading and policy  Object oriented concept revision  String manipulation revision Dr. Hussien M. Sharaf
Page 10: Introduction  Course outline and references  Grading and policy  Object oriented concept revision  String manipulation revision Dr. Hussien M. Sharaf

Do not like programming ?

Tester, Quality Assurance Engineer, … System Administrator, Storage Admin, … Network engineer, … Customer support, configuration,

customization, …. Database developer, administrator,

consultant, … Game developer, designer, tester, etc … IT governance, auditing, contracting, etc …. Education, training, etc …. …………………………… homemaking

Page 11: Introduction  Course outline and references  Grading and policy  Object oriented concept revision  String manipulation revision Dr. Hussien M. Sharaf
Page 12: Introduction  Course outline and references  Grading and policy  Object oriented concept revision  String manipulation revision Dr. Hussien M. Sharaf

IntroductionCourse outline and referencesGrading and policy

Object oriented concept revision

String manipulation revision

Lecture Outline

Dr. Hussien M. Sharaf 12

Page 13: Introduction  Course outline and references  Grading and policy  Object oriented concept revision  String manipulation revision Dr. Hussien M. Sharaf

1. OOP C++ Revision 2. Introduction to File Structures1

3. Fundamental File Processing Operations2

4. Secondary Storage Devices3

5. Fundamental File Structure4

6. Managing Files of Records5

7. Organizing Files for Performance6

8. Indexing7

9. Multi-level Indexing with B-Trees9

10. Intro. to Computer Forensics (if time permits)

Course outline and references

13

Page 14: Introduction  Course outline and references  Grading and policy  Object oriented concept revision  String manipulation revision Dr. Hussien M. Sharaf

Main Textbook is: Michael J. Folk, Bill

Zoellick, Greg Riccardi; File Structures: An Object-oriented Approach with C++; Pearson Education

Other useful textbooks Steve Teale; C++ IOStreams

handbook; Addison-Wesley, 1993

Any C++ reference.

References

14

Page 15: Introduction  Course outline and references  Grading and policy  Object oriented concept revision  String manipulation revision Dr. Hussien M. Sharaf

Coursework: 40%◦Lab work 6%◦Assignments / Quizzes ~ (4 x 6) = 24%

◦ Midterm 10%Final Exam: 60%

Grading

15

Page 16: Introduction  Course outline and references  Grading and policy  Object oriented concept revision  String manipulation revision Dr. Hussien M. Sharaf

Revision of OO Concept

Page 17: Introduction  Course outline and references  Grading and policy  Object oriented concept revision  String manipulation revision Dr. Hussien M. Sharaf

slide 17

C++ Object System

Classes define new types Objects are instances of classes Inheritance

◦Ability to define new classes from existing ones

◦Single and multiple inheritance Encapsulation

Page 18: Introduction  Course outline and references  Grading and policy  Object oriented concept revision  String manipulation revision Dr. Hussien M. Sharaf

18

What is OO ?

Object-orientation is a way of thinking about problems using models built from real-world concepts.

The fundamental unit is the Object An object has data and behavior OO Software means we write our program in

terms of objects, each tightly integrates data and operations on the data

In Structured programming, data and operations on the data were separated or loosely related.

Page 19: Introduction  Course outline and references  Grading and policy  Object oriented concept revision  String manipulation revision Dr. Hussien M. Sharaf
Page 20: Introduction  Course outline and references  Grading and policy  Object oriented concept revision  String manipulation revision Dr. Hussien M. Sharaf

20

C++ Data Types

structured

array struct union class

address

pointer reference

simple

integral enum

char short int long bool

floating

float double long double

Page 21: Introduction  Course outline and references  Grading and policy  Object oriented concept revision  String manipulation revision Dr. Hussien M. Sharaf

struct Point

struct Point {int x;int y;

};

A point object has two fields, attributes or members

You need to define functions to operate on points and pass them copies of this structure

Page 22: Introduction  Course outline and references  Grading and policy  Object oriented concept revision  String manipulation revision Dr. Hussien M. Sharaf

struct Point

Point p;

Use the dot operator to access members

p.x = 2; p.y = 3;

Page 23: Introduction  Course outline and references  Grading and policy  Object oriented concept revision  String manipulation revision Dr. Hussien M. Sharaf

Operations on struct Point

Point movePoint (int newX, int newY, Point& point) {

point.x = newX; point.Y = newY;

}; If we want to perform operations, methods

or behaviors on points, we write them as separate functions and pass the designated point to them.

Page 24: Introduction  Course outline and references  Grading and policy  Object oriented concept revision  String manipulation revision Dr. Hussien M. Sharaf

class Point

class Point { private: int x;

int y; public: ..... methods .... };

A class combines attributes and behavior

y Pointer to class Point

Page 25: Introduction  Course outline and references  Grading and policy  Object oriented concept revision  String manipulation revision Dr. Hussien M. Sharaf

Access specifiers modify the access rights that the members following them acquire.

Private members: are accessible only from within other members of the same class.

Public members: are accessible from anywhere where the object is visible.

If none of the two words exist before any member of the class, then the default is private.

Access Specifiers

Dr. Hussien M. Sharaf 25

Page 26: Introduction  Course outline and references  Grading and policy  Object oriented concept revision  String manipulation revision Dr. Hussien M. Sharaf

class Point

Page 27: Introduction  Course outline and references  Grading and policy  Object oriented concept revision  String manipulation revision Dr. Hussien M. Sharaf

A. Data members are variables declared inside the class definition.

B. Methods:1. Constructors initialize data members if needed.2. set function stores the entered parameter into

the corresponding data member.3. get function returns the value of each data

member, so each data member should have a get function that returns its value.

Data members, set and get functions

27

Page 28: Introduction  Course outline and references  Grading and policy  Object oriented concept revision  String manipulation revision Dr. Hussien M. Sharaf

class Point

Point () { // Default constructor

x = y = 0; } Point (int newX, int newY){

x = newX; y = newY;

} Constructor is a function that is called

automatically when a Point object is created. Default constructor takes no parameters.

Page 29: Introduction  Course outline and references  Grading and policy  Object oriented concept revision  String manipulation revision Dr. Hussien M. Sharaf

Constructor is a special member function that must be defined with the same name as the class.

Constructor is used to initialize data members of the class.

Constructor only executed when a new object of the class is created.

Constructors cannot return values. Constructors are declared public.

Constructors

Dr. Hussien M. Sharaf 29

Page 30: Introduction  Course outline and references  Grading and policy  Object oriented concept revision  String manipulation revision Dr. Hussien M. Sharaf

Default constructor is a constructor with no parameters.

It can be either: ◦ Implicit: the compiler provides a default

constructor, if no constructor has defined.

◦ It does not initialize the class’s data members, so they contain garbage data.

◦ Explicit: you define a constructor that takes no arguments, but from inside its body you have to initialize the data members of the class.

Default constructors

Dr. Hussien M. Sharaf 30

Page 31: Introduction  Course outline and references  Grading and policy  Object oriented concept revision  String manipulation revision Dr. Hussien M. Sharaf

class Point

int getX() {return x;

} int getY() {

return y; } Getter (accessors) methods are used to access

class’s private data.

Page 32: Introduction  Course outline and references  Grading and policy  Object oriented concept revision  String manipulation revision Dr. Hussien M. Sharaf

class Point

void setX(int newX) {x = newX;

} void setY(int newY) {

y = newY; } Setter (mutators) are methods used to change

class’s private data (if needed). Classes with not setters are immutable.

Page 33: Introduction  Course outline and references  Grading and policy  Object oriented concept revision  String manipulation revision Dr. Hussien M. Sharaf
Page 34: Introduction  Course outline and references  Grading and policy  Object oriented concept revision  String manipulation revision Dr. Hussien M. Sharaf

Separating Interface from Implementation – Point.h

#ifndef _point_h #define _point_h

#include <string> #include <iostream>

class Point {

private: int x, y;

Page 35: Introduction  Course outline and references  Grading and policy  Object oriented concept revision  String manipulation revision Dr. Hussien M. Sharaf

Separating Interface from Implementation

public: Point(); Point(int x, int y); int getX(); int getY(); std::string toString(); bool operator==(Point & p2); bool operator!=(Point & p2); };

std::ostream & operator<<(std::ostream & os, Point & pt); #endif

Page 36: Introduction  Course outline and references  Grading and policy  Object oriented concept revision  String manipulation revision Dr. Hussien M. Sharaf

Separating Interface from Implementation – Point.cpp

#include <cstdlib> #include <sstream> #include "Point.hpp" using namespace std;

Point::Point (){x = y = 0; }

Point::Point (int newX, int newY){ x = newX; y = newY; }

Page 37: Introduction  Course outline and references  Grading and policy  Object oriented concept revision  String manipulation revision Dr. Hussien M. Sharaf

Separating Interface from Implementation – Point.cpp

int Point::getX () { return x; }

int Point::getY () { return y; }

string Point::toString() { stringstream ss; ss << "(" << x << ", " << y << ")"; return ss.str(); }

Page 38: Introduction  Course outline and references  Grading and policy  Object oriented concept revision  String manipulation revision Dr. Hussien M. Sharaf

Separating Interface from Implementation – Point.cpp

bool Point::operator!=(Point & p2) { return (x != p2.getX() || y != p2.getY());

}

bool Point::operator==(Point & p2) { return (x == p2.getX() && y == p2.getY());

}

std::ostream & operator<<(std::ostream & os, Point & pt) {

os << pt.toString(); }

Page 39: Introduction  Course outline and references  Grading and policy  Object oriented concept revision  String manipulation revision Dr. Hussien M. Sharaf
Page 40: Introduction  Course outline and references  Grading and policy  Object oriented concept revision  String manipulation revision Dr. Hussien M. Sharaf

Operator Overloading

Operator overloading is the ability to extend the meaning of an existing C++ operator to work on new types.

Use cautiously and give an operator only meanings consistent with what it does.

bool Point::operator==(Point& p2){ if (x == p2.getX() && y == p2.getY()) return true; else return false; }

Page 41: Introduction  Course outline and references  Grading and policy  Object oriented concept revision  String manipulation revision Dr. Hussien M. Sharaf

Overloading << operator

The first parameter must be ostream So, << cannot be a member function. It must be

a free function or a friend function. It must also return an ostream to allow cascaded

printing.

ostream& operator << (ostream& os, Point & pt) {os << pt.toString();

}

Page 42: Introduction  Course outline and references  Grading and policy  Object oriented concept revision  String manipulation revision Dr. Hussien M. Sharaf
Page 43: Introduction  Course outline and references  Grading and policy  Object oriented concept revision  String manipulation revision Dr. Hussien M. Sharaf

Example: A stack in C

typedef struct { char s[SIZE]; int size;} Stack;

stack *create() { Stack *s; s = (Stack *)malloc(sizeof(Stack)); s->size= 0; return s;}

Creator function ensures stack is created properly.

Does not help for stack that is automatic variable.

Programmer could inadvertently create uninitialized stack.

                                                                                                    

        

Page 44: Introduction  Course outline and references  Grading and policy  Object oriented concept revision  String manipulation revision Dr. Hussien M. Sharaf

Example: A stack in C

char pop(Stack *stack) { if (stack->size = 0) error(“Underflow”);

return stack->s[--stack->size];}

void push(Stack *stack, char v) { if (stack->size == SIZE) error(“Overflow”);

stack->s[stack->size++] = v;}

Not clear these are the only stack-related functions.

Another part of program can modify any stack any way it wants to, destroying invariants.

Page 45: Introduction  Course outline and references  Grading and policy  Object oriented concept revision  String manipulation revision Dr. Hussien M. Sharaf

Example: A Better C stack

const int SIZE = 20;

struct Stack { char data[SIZE]; int size;};

Stack create() { Stack s; s.size = 0; return s;}

Page 46: Introduction  Course outline and references  Grading and policy  Object oriented concept revision  String manipulation revision Dr. Hussien M. Sharaf

Example: A Better C stackchar pop(Stack& s) { if (s.size = 0) error("Underflow"); return s.data[--(s.size)];}

void push(Stack& s, char v) { if (s.size == SIZE) error("Overflow"); s.data[s.size++] = v;}

void error (string message) {cout << "\n" << message << "\n";exit (1);

}

Page 47: Introduction  Course outline and references  Grading and policy  Object oriented concept revision  String manipulation revision Dr. Hussien M. Sharaf

C++ Solution: Classclass Stack { private:

char data[SIZE]; int size;

public: Stack () {size = 0}

char pop() { if (size == 0) error("Underflow"); return data[--size];

}

void push(char v) { if (size == SIZE) error("Overflow"); data[size++] = v;

}};

Definition of both representation and operations

Constructor: initializes

Public: visible outside the class

Member functions see object fields like local variables

Page 48: Introduction  Course outline and references  Grading and policy  Object oriented concept revision  String manipulation revision Dr. Hussien M. Sharaf

C++ Stack Class Natural to use

Stack st;st.push(‘a’); st.push(‘b’);char d = st.pop();

Stack *stk = new Stack;stk->push(‘a’); stk->push(‘b’);char d = stk->pop();

Page 49: Introduction  Course outline and references  Grading and policy  Object oriented concept revision  String manipulation revision Dr. Hussien M. Sharaf

Another C++ Solution – Same Interfaceclass Stack { private:

vector<char> data;

public: Stack () {

}

char pop() { if (v.empty()) error("Underflow"); char c = data[data.size()-1]; data.pop_back(); return c;

}

void push(char v) { data.push_back(v);

}};

Page 50: Introduction  Course outline and references  Grading and policy  Object oriented concept revision  String manipulation revision Dr. Hussien M. Sharaf

vector Operations

Page 51: Introduction  Course outline and references  Grading and policy  Object oriented concept revision  String manipulation revision Dr. Hussien M. Sharaf

vector Operations

Page 52: Introduction  Course outline and references  Grading and policy  Object oriented concept revision  String manipulation revision Dr. Hussien M. Sharaf

Class Implementation

C++ compiler translates to C-style implementation

C++

class Stack { char s[SIZE]; int sp;public: Stack() void push(char); char pop();};

Equivalent C implementation

struct Stack { char s[SIZE]; int sp;};

void st_Stack(Stack*);void st_push(Stack*, char);char st_pop(Stack*);

Page 53: Introduction  Course outline and references  Grading and policy  Object oriented concept revision  String manipulation revision Dr. Hussien M. Sharaf

53

Page 54: Introduction  Course outline and references  Grading and policy  Object oriented concept revision  String manipulation revision Dr. Hussien M. Sharaf
Page 55: Introduction  Course outline and references  Grading and policy  Object oriented concept revision  String manipulation revision Dr. Hussien M. Sharaf

When overloading operators << and >> it is better to use ostream and istream which are the parents of ofstream and ifstream

Overloading operators

55

ostream

istream

iostream

fstream

ofstream

cout

ifstreamcin

Page 56: Introduction  Course outline and references  Grading and policy  Object oriented concept revision  String manipulation revision Dr. Hussien M. Sharaf

http://www.cplusplus.com/reference/iostream/

IOstream Library

Page 57: Introduction  Course outline and references  Grading and policy  Object oriented concept revision  String manipulation revision Dr. Hussien M. Sharaf

String manipulation

57

Page 58: Introduction  Course outline and references  Grading and policy  Object oriented concept revision  String manipulation revision Dr. Hussien M. Sharaf

Below are some functions that are used to do different operations on strings.

String Manipulation

insertsubstri

ngAppend(

+)lengt

hrepla

cefind

String Manipulation

Dr. Hussien M. Sharaf 58

Page 59: Introduction  Course outline and references  Grading and policy  Object oriented concept revision  String manipulation revision Dr. Hussien M. Sharaf

Returns the length of the string.

Example:

string email = "[email protected]";cout << "Email length: " << email.length() << endl;

Output:

Email length: 7

Length

59

Page 60: Introduction  Course outline and references  Grading and policy  Object oriented concept revision  String manipulation revision Dr. Hussien M. Sharaf

Append Concatenates two chars/strings. We can use operator “+” to perform the append

operation directly.

Example:string myname = "Ahmed Yehia";string job = ", Computer Engineer";string mystr = myname + job;// Or mystr = myname.append(job);cout << mystr << endl;

Output:

Ahmed Yehia, Computer Engineer

60

Page 61: Introduction  Course outline and references  Grading and policy  Object oriented concept revision  String manipulation revision Dr. Hussien M. Sharaf

Insert Inserts some additional content at a specific

location within the string content. It takes two inputs:1. Position from where to insert.2. String to insert.

Example:string myname = "Ahmed Yehia";myname.insert(6, "M ");cout << "My name is: " << myname << endl;

Output:

My name is: Ahmed M Yehia

61

Page 62: Introduction  Course outline and references  Grading and policy  Object oriented concept revision  String manipulation revision Dr. Hussien M. Sharaf

Find Searches the string for some specified content It takes two inputs (can take only the first):

1. The content to be matched.2. Position from where to start search.

It returns position of the first occurrence in the string.

Example:string myname = "Ahmed Yehia";int pos1 = myname.find("e");int pos2 = myname.find("e", 4);cout << "first occurrence of e: " << pos1<<endl;cout << "second occurrence of e: " << pos2 << endl;

Output: first occurrence of e: 3 second occurrence of e:

7

62

Page 63: Introduction  Course outline and references  Grading and policy  Object oriented concept revision  String manipulation revision Dr. Hussien M. Sharaf

Substring substr results in a subset of the full string. It takes two inputs:1.Position from where to start cutting the

string.2.Count of letters to extract.

Example: string email = "[email protected]";string domain = email.substr(2,email.length() -1);

cout << "Domain is: " << domain << endl;

Output: Domain is: h.com

63

Page 64: Introduction  Course outline and references  Grading and policy  Object oriented concept revision  String manipulation revision Dr. Hussien M. Sharaf

Replace Replaces a section of the current string by some

other specified content. It takes three inputs:1. Position from where to replace.2. Count of letters to replace. 3. The replacement string.

Example: string fname = "Ahmed Yehia";fname.replace(6,5, "Ali");cout<< "My friend’s name is: " <<fname <<endl;

Output: My friend’s name is: Ahmed Ali

64

Page 65: Introduction  Course outline and references  Grading and policy  Object oriented concept revision  String manipulation revision Dr. Hussien M. Sharaf

#include <iostream>#include <string>using namespace std;

int main (){ string myname = "Hussien";

//append myname.append("Sharaf");

//insert myname.insert(7, " M "); cout << "My name is: " << myname << endl;

//length cout << "My full name " << " length is: " << myname.length() << endl;

Ex 1.4 C++ program with string manipulation

65

Page 66: Introduction  Course outline and references  Grading and policy  Object oriented concept revision  String manipulation revision Dr. Hussien M. Sharaf

Example: C++ program with string manipulation

//find int spos1 = myname.find("s"); int spos2 = myname.find("s", spos1+1); cout << "positions of s in " << myname << " are at: " << spos1<< ", " <<spos2 <<endl;

//substr int space_pos = myname.find(' '); string firstName = myname.substr(0,space_pos); cout << "My first name is: " << firstName << endl;

//replace firstName.replace(4,2,"ei"); cout << "My first name can be written as: “ << firstName << endl;

system ("pause"); return 0;} 66

Page 67: Introduction  Course outline and references  Grading and policy  Object oriented concept revision  String manipulation revision Dr. Hussien M. Sharaf

Output of the program

67

Page 68: Introduction  Course outline and references  Grading and policy  Object oriented concept revision  String manipulation revision Dr. Hussien M. Sharaf
Page 69: Introduction  Course outline and references  Grading and policy  Object oriented concept revision  String manipulation revision Dr. Hussien M. Sharaf

Assignment #1 Objective

Reviewing C++ & learning about storage technologies and careers

Work in groups 3~5 Each team member solves a different

problem ????????????????

69

Page 70: Introduction  Course outline and references  Grading and policy  Object oriented concept revision  String manipulation revision Dr. Hussien M. Sharaf

Deadline Next week is the deadline. No excuses. Don’t wait until last day. I can help you to the highest limit within the

next 3 days.

Dr. Hussien M. Sharaf 70

Page 71: Introduction  Course outline and references  Grading and policy  Object oriented concept revision  String manipulation revision Dr. Hussien M. Sharaf

Thank you