introduction course outline and references grading and policy object oriented concept revision ...
TRANSCRIPT
File Organization & processing
Dr. Mohamamd El-RamlyMany slides by Dr. Hussien Sharaf
CS 215
Lecture 1 Introduction
Cairo UniversityFCI
2014
IntroductionCourse outline and referencesGrading and policy
Object oriented concept revision
String manipulation revision
Lecture Outline
Dr. Hussien M. Sharaf 2
النية جدد
له الله سهل علما فيه يلتمس طريقا سلك منالجنة إلى طريقا
.... بيت يهدم والجهل لــه عماد ال Rبيتا يرفع العلموالكرم العز
يفتح ونبراس المتخلفة العقول يضيء نور العلمالمظلمة الدروب
لندن طاعون قصة اصنعه لكن و التغيير تنتظر ال الخيول مزرعة صاحب قصة
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? … … ….
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)
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.
Salaries ?
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
IntroductionCourse outline and referencesGrading and policy
Object oriented concept revision
String manipulation revision
Lecture Outline
Dr. Hussien M. Sharaf 12
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
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
Coursework: 40%◦Lab work 6%◦Assignments / Quizzes ~ (4 x 6) = 24%
◦ Midterm 10%Final Exam: 60%
Grading
15
Revision of OO Concept
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
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.
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
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
struct Point
Point p;
Use the dot operator to access members
p.x = 2; p.y = 3;
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.
class Point
class Point { private: int x;
int y; public: ..... methods .... };
A class combines attributes and behavior
y Pointer to class Point
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
class Point
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
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.
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
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
class Point
int getX() {return x;
} int getY() {
return y; } Getter (accessors) methods are used to access
class’s private data.
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.
Separating Interface from Implementation – Point.h
#ifndef _point_h #define _point_h
#include <string> #include <iostream>
class Point {
private: int x, y;
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
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; }
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(); }
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(); }
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; }
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();
}
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.
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.
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;}
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);
}
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
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();
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);
}};
vector Operations
vector Operations
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*);
53
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
http://www.cplusplus.com/reference/iostream/
IOstream Library
String manipulation
57
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
Returns the length of the string.
Example:
string email = "[email protected]";cout << "Email length: " << email.length() << endl;
Output:
Email length: 7
Length
59
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
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
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
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
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
#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
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
Output of the program
67
Assignment #1 Objective
Reviewing C++ & learning about storage technologies and careers
Work in groups 3~5 Each team member solves a different
problem ????????????????
69
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
Thank you