4. 1. Introduction to file management and OO Revision.
2. File organization and fundamental file/streams
processing operations(opening, reading, writing,
seeking).
3. Reading/writing fields of a single record.
4. List different methods for field organization.
5. Reading/writing records: using fixed length and
variable length.
6. Reading/writing records: delimiters and simple
Index. Buffering of records.
Mid-Term exam (Lec 1- 5)
Dr. Hussien M. Sharaf 4
5. 7. Linear/sequential and binary searching on files.
8. Reclaiming space in files (as a result for deletes
and updates).
9. Key sorting and simple indexing.
10. Record operations with existence of Indexing.
11. Composite Indexing (primary and secondary
indexes).
12. Multilevel indexing and introduction to B trees.
13. Using Hashing to locate records.
Dr. Hussien M. Sharaf 5
6. Main Text book is
Michael J. Folk, Bill Zoellick, Greg
Riccardi; File Structures: An
Object-oriented Approach
with C++; Pearson Education
These are other text books
1. Steve Teale; C++ IOStreams
handbook; Addison-Wesley,
1993
2. D. S. Malik; Data Structures
Using C++; Cengage
Learning, Jul 31, 2009
Dr. Hussien M. Sharaf 6
7.
8. A class is a data
structure that holds
data and functions.
It can be seen as a
container of related
variables and
functions.
int x=500;
char c=‘A’;
string g;
void incrementX(int x);function
variables
Dr. Hussien M. Sharaf 8
9. An object is an instantiation of a class.
Class format:
class class_name {
access_specifier_1:
member1;
access_specifier_2:
member2;
...
} object_names;
Where:
class_name: is a valid identifier for the class.
access_specifier: is one of the following three
keywords: private, public or protected.
members: can be data or function declarations.
object_names: is an optional list of names for
objects of this class.
Variable definition
int x;
Class definition
class class_name;
Dr. Hussien M. Sharaf 9
10. Now let’s make a simple class for a rectangle.
class Crectangle
{
public:
int area ();
int width, height;
};
Crectangle class contains two data members (width,
and height), and one public member function area
that calculates area.
Dr. Hussien M. Sharaf 10
int height;
int width;
int area();
Crectangle
11. 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.
Dr. Hussien M. Sharaf 11
12. The following
figure shows the
scope of each
data member in
different classes.
Whole program
class cl1{
private x;
public y;
};
private x;
public y;
int main() {
…..
}
class cl2{
private a;
public b;
};
private a;
public b;
Dr. Hussien M. Sharaf 12
13. 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.
Dr. Hussien M. Sharaf 13
14. Data members
Constructor
Get: within the class
(like: area ())
Or
Set: outside it (like:
set_values (int,int))
#include <iostream>
using namespace std;
class CRectangle {
int width, height;
public:
CRectangle(){width=1;}
int area () {return (x*y);}
void set_values (int,int);
};
void CRectangle::set_values
(int a, int b) {
width = a;
height = b;
}
Dr. Hussien M. Sharaf 14
15. #include <iostream>
using namespace std;
class CRectangle {
//Declaration section
public:
int width, height;
//Definition section
int area ()
{return (width * height);}
void set_values (int,int);
};
void CRectangle::set_values
(int a, int b) {
width = a;
height = b;
}
Output:
rectangle’s area is: 20
int main () {
CRectangle rect;
rect.width = 4;
rect.height = 5;
//OR
rect.set_values(4,5);
cout << “rectangle’s area is: "
<< rect.area();
return 0;
}
Dr. Hussien M. Sharaf 15
16. 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.
Dr. Hussien M. Sharaf 16
17. 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.
- or Explicit: you define a constructor that takes no
arguments, but from inside its body you have to
initialize the data members of the class.
Dr. Hussien M. Sharaf 17
18. #include <iostream>
using namespace std;
class CRectangle {
int width, height;
public:
//Constructor without arguments
CRectangle (){width=1;height=0;};
//Constructor with arguments
CRectangle (int,int);
int area () {return (width*height);}
};
Dr. Hussien M. Sharaf 18
CRectangle:: CRectangle(int a,
int b)
{
width = a;
height = b;
}
19. int main () {
CRectangle rect_a(3,4);
CRectangle rect_b();
rect_b.width=90;
rect_b.height =80;
cout << “rect_a width/height are: "
<< rect_a.width<<“/”<< rect_a.height<<endl;
cout << “rect_b width/height are: "
<< rect_b.width<<“/”<< rect_b. height<<endl;
cout << “rect_a area is: " << rect_a.area() <<endl;
cout << “rect_b area is: " << rect_b.area() <<endl;
system("Pause");
return 0; }
Dr. Hussien M. Sharaf 19
20. You already use overloaded operators
string s1( “Happy Term" );
cout<< “I wish you “<<s1;
We need to write code to tell the compiler how to
deal with any new class that we build.
ofstream: Stream class to write on files
ifstream: Stream class to read from files
fstream: Stream class to both read and write
from/to files.
Dr. Hussien M. Sharaf 20
21. When overloading operators << and >> it is
better to use ostream and istream which are the
parents of ofstream and ifstream
Dr. Hussien M. Sharaf 21
ostream istream
iostream
fstream
ofstream
cout
ifstream
cin
26. Below are some functions that are used to do
different operations on strings.
insert substringAppend(+)length replacefind
String
Manipulation
Dr. Hussien M. Sharaf 26
27. Returns the length of the string.
Example:
string email = "d@h.com";
cout<<"Email length: "<<email.length()<<endl;
Output:
Email length: 7
Dr. Hussien M. Sharaf 27
28. 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
Dr. Hussien M. Sharaf 28
29. 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
Dr. Hussien M. Sharaf 29
30. 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 the 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
Dr. Hussien M. Sharaf 30
31. 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 = "d@h.com";
string domain = email.substr(2,email.length() -1);
cout<<"Domain is: "<<domain<<endl;
Output:
Domain is: h.com
Dr. Hussien M. Sharaf 31
32. 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
Dr. Hussien M. Sharaf 32
33. #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;
//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;
}
Dr. Hussien M. Sharaf 33
35. Run Example 1.3 using VS2008 or VS2010
Send the source code project (after zipping it) to my
email:
n.abdelhameed@fci-cu.edu.eg
With subject: “FO – Assignment#1.3”
Run Example 1.4 using VS2008 or VS2010
Send the source code project (after zipping it) to my
email:
n.abdelhameed@fci-cu.edu.eg
With subject: “FO – Assignment#1.4”
Dr. Hussien M. Sharaf 35
36. 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 36
37. 1. Delete the debug folder.
2. Compress the solution folder using
winrar.
3. Rename the compressed file as follows:
StudentName_ID_A1.rar
StudentName_ID_A2.rar
4. Email to: n.abdelhameed@fci-cu.edu.eg
5. Follow your marks at:
http://tinyurl.com/p6qwdme
Dr. Hussien M. Sharaf 37