cs215 lec 1 introduction

38

Upload: arab-open-university-and-cairo-university

Post on 27-May-2015

470 views

Category:

Education


1 download

DESCRIPTION

File organization course: Introduction and history of File Management, and disk operations.

TRANSCRIPT

Page 1: CS215 Lec 1   introduction
Page 2: CS215 Lec 1   introduction

� Grading and policy

� Course outline and references

�Object oriented concept revision

�String manipulation revision.

Dr. Hussien M. Sharaf 2

Page 3: CS215 Lec 1   introduction

� Assignments and Projects: 22%

� Mid-Term Exam: 8%

� Lab: 10%

� Final Exam: 60%

Dr. Hussien M. Sharaf 3

Page 4: CS215 Lec 1   introduction

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

Page 5: CS215 Lec 1   introduction

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

Page 6: CS215 Lec 1   introduction

Main Text book isMichael J. Folk, Bill Zoellick, Greg

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

These are other text books1. Steve Teale; C++ IOStreams

handbook; Addison-Wesley, 1993

2. D. S. Malik; Data Structures Using C++; CengageLearning, Jul 31, 2009

Dr. Hussien M. Sharaf 6

Page 7: CS215 Lec 1   introduction
Page 8: CS215 Lec 1   introduction

� 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

Page 9: CS215 Lec 1   introduction

� 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 threekeywords: 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

Page 10: CS215 Lec 1   introduction

� 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

Page 11: CS215 Lec 1   introduction

� 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

Page 12: CS215 Lec 1   introduction

� 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

Page 13: CS215 Lec 1   introduction

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

Page 14: CS215 Lec 1   introduction

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

Page 15: CS215 Lec 1   introduction

#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;//ORrect.set_values(4,5);cout << “rectangle’s area is: " << rect.area();return 0;

}

Dr. Hussien M. Sharaf 15

Page 16: CS215 Lec 1   introduction

� 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

Page 17: CS215 Lec 1   introduction

� 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

Page 18: CS215 Lec 1   introduction

#include <iostream>using namespace std;class CRectangle {

int width, height;public://Constructor without argumentsCRectangle (){width=1;height=0;};//Constructor with argumentsCRectangle (int,int);int area () {return (width*height);}

};

Dr. Hussien M. Sharaf 18

CRectangle:: CRectangle(int a, int b)

{width = a;height = b;

}

Page 19: CS215 Lec 1   introduction

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

Page 20: CS215 Lec 1   introduction

� You already use overloaded operatorsstring 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

Page 21: CS215 Lec 1   introduction

� 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

ifstreamcin

Page 22: CS215 Lec 1   introduction

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

Page 23: CS215 Lec 1   introduction

#include <iostream>using namespace std;class CRectangle {

int width, height;public://Constructor without argumentsCRectangle (){width=1;height=0;};

//Constructor with argumentsCRectangle (int,int);int area () {return (width*height);}//overload the in and out operatorsfriend istream& operator >> (istream & stream, CRectangle & p);friend ostream& operator<<(ostream & stream, CRectangle & p);

};

Dr. Hussien M. Sharaf 23

CRectangle::CRectangle(int a, int b) {width = a;

height = b;}istream & operator >> (istream &

stream, CRectangle & p){stream>>p.width;

stream.get(); // skip a char

OR stream.ignore(); //skip a char

stream>>p.height;return stream;}ostream & operator<<(ostream &

stream, CRectangle & p){stream<< p.width<<“,”<< p.height<<endl;return stream;}

Page 24: CS215 Lec 1   introduction

int main () {CRectangle rect_a(3,4);CRectangle rect_b;rect_b.width=90;rect_b.height =80;cout << “rect_a is: " << rect_a <<endl;cout << “rect_b is: " << rect_b<<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 24

Page 25: CS215 Lec 1   introduction

Dr. Hussien M. Sharaf 25

Page 26: CS215 Lec 1   introduction

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

insert substringAppend(+)length replacefind

String Manipulation

Dr. Hussien M. Sharaf 26

Page 27: CS215 Lec 1   introduction

� Returns the length of the string.

Example:

string email = "[email protected]";

cout<<"Email length: "<<email.length()<<endl;

Output:

Email length: 7

Dr. Hussien M. Sharaf 27

Page 28: CS215 Lec 1   introduction

� 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

Page 29: CS215 Lec 1   introduction

� 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

Page 30: CS215 Lec 1   introduction

� 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: 3second occurrence of e: 7

Dr. Hussien M. Sharaf 30

Page 31: CS215 Lec 1   introduction

� 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

Dr. Hussien M. Sharaf 31

Page 32: CS215 Lec 1   introduction

� 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

Page 33: CS215 Lec 1   introduction

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

int main (){string myname = "Hussien";//appendmyname.append("Sharaf");//insertmyname.insert(7, " M ");cout<< "My name is: "

<<myname <<endl;//lengthcout<< "My full name length is: "

<<myname.length() <<endl;

//findint spos1 = myname.find("s");int spos2 = myname.find("s", spos1+1);cout<< "positions of s in " << myname << " are at: " << spos1<< ", " <<spos2 <<endl;//substrint space_pos = myname.find(' ');string firstName = myname.substr(0,space_pos);cout<<"My first name is: " <<firstName<<endl;//replacefirstName.replace(4,2,"ei");cout<<"My first name can be written as: "<<firstName<<endl;

system ("pause");return 0;}

Dr. Hussien M. Sharaf 33

Page 34: CS215 Lec 1   introduction

Dr. Hussien M. Sharaf 34

Page 35: CS215 Lec 1   introduction

� Run Example 1.3 using VS2008 or VS2010� Send the source code project (after zipping it) to my

email:[email protected] subject: “FO – Assignment#1.3”

� Run Example 1.4 using VS2008 or VS2010� Send the source code project (after zipping it) to my

email:[email protected] subject: “FO – Assignment#1.4”

Dr. Hussien M. Sharaf 35

Page 36: CS215 Lec 1   introduction

� 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

Page 37: CS215 Lec 1   introduction

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: [email protected]

5. Follow your marks at: http://tinyurl.com/p6qwdme

Dr. Hussien M. Sharaf 37

Page 38: CS215 Lec 1   introduction