ΤΜΗΜΑ ΠΛΗΡΟΦΟΡΙΚΗΣ Α.Π.Θ. ΕΡΓΑΣΤΗΡΙΟ C++ ΕΞΑΜΗΝΟ Γ Ακαδηµαϊκό Έτος 2006-2007 ΠΡΟΤΥΠΑ ΠΑΡΑ ΕΙΓΜΑ ηµιουργία πρότυπου στοίβας (stack) και στη συνέχεια δηµιουργία µιας στοίβας σηµείων. // point.h #ifndef POINT_H #define POINT_H using std::ostream; class Point public: Point(); Point( int, int ); Point(const Point & pt); ~Point(); Point& operator=(const Point &p1); void setx( int ); int getx() const; void sety( int ); int gety() const; void print() const; protected: int x; int y; ; #endif // point.cpp #include "point.h" // default constructor Point::Point() : x(0), y(0) Εργαστήριο Αντικειµενοστραφούς Προγραµµατισµού µε C++ - Μάθηµα 8 ο -1-
Point::Point( int xvalue, int yvalue ) : x(xvalue), y(yvalue) Point::Point(const Point & pt) : x(pt.x), y(pt.y) Point& Point::operator=(const Point &p1) if (this!= &p1) x = p1.x; y = p1.y; return *this; Point::~Point() void Point::setX( int xvalue ) x = xvalue; int Point::getX() const return x; void Point::setY( int yvalue ) y = yvalue; int Point::getY() const return y; void Point::print() const cout << '[' << x << ", " << y << "] "; // stack.h #ifndef STACK_H_ #define STACK_H_ class Stack public: Εργαστήριο Αντικειµενοστραφούς Προγραµµατισµού µε C++ - Μάθηµα 8 ο -2-
Stack(int=10); ~Stack(); bool isempty() const; bool isfull() const; void push(const T & item); // add item to stack bool pop(t & item); // pop top item ; private: T *items;// holds stack items int top; // index for top stack item int size; #include <cstdlib> // create a stack Stack<T>::Stack(int sz) : top(0), size(sz) if(sz>0) items=new T[sz]; else items = 0; if (items==0) cout << "Out of memory!\n"; exit(1); Stack<T>::~Stack() delete [] items; bool Stack<T>::isEmpty() const return top == 0; bool Stack<T>::isFull() const return top == size; void Stack<T>::push(const T & item) items[top++] = item; bool Stack<T>::pop(T & item) if (top >= size) return false; item = items[--top]; return true; Εργαστήριο Αντικειµενοστραφούς Προγραµµατισµού µε C++ - Μάθηµα 8 ο -3-
#endif // teststack.cpp using std::cin; #include "stack.h" #include "Point.h" int main() Stack<unsigned long> st; unsigned long po; for (int i=0; i < 5; i++) st.push(2*i+3); cout << "Processing unsigned long stack...\n"; while (!st.isempty()) st.pop(po); cout << po << " "; cout << endl; Stack<Point> point_stack; Point data[5] = Point(1,2), Point(3,4), Point(5,6), Point(7,8), Point(9,0) ; for (int i=0; i < 5; i++) point_stack.push(data[i]); cout << "\nprocessing Point stack...\n"; Point tpoint; while (!point_stack.isempty()) point_stack.pop(tpoint); tpoint.print(); cout << endl; Stack<Stack<Point> > point_stack1; return 0; // main Εργαστήριο Αντικειµενοστραφούς Προγραµµατισµού µε C++ - Μάθηµα 8 ο -4-
ΑΡΧΕΙΑ ΠΑΡΑ ΕΙΓΜΑ Α) Εγγραφή ενός αντικειµένου τύπου Point σε αρχείο και στη συνέχεια διάβασµα του αντικειµένου από το αρχείο. // testmain.cpp #include "point.h" using std::cin; using std::ios; #include <fstream> using std::ofstream; using std::ifstream; int main() char filename[80]; cout << "Please enter the file name: "; cin >> filename; ofstream fout(filename,ios::binary); if (!fout) cout << "Unable to open " << filename << " for writing.\n"; Point origin(50,100); fout.write((char*) &origin,sizeof(origin)); fout.close(); ifstream fin(filename,ios::binary); if (!fin) cout << "Unable to open " << filename << " for reading.\n"; Point point2(1,1); cout << "Point2 x: " << point2.getx() << endl; cout << "Point2 y: " << point2.gety() << endl; fin.read((char*) &point2, sizeof(point2)); cout << "Point2 x: " << point2.getx() << endl; cout << "Point2 y: " << point2.gety() << endl; fin.close(); return 0; Εργαστήριο Αντικειµενοστραφούς Προγραµµατισµού µε C++ - Μάθηµα 8 ο -5-
όπου: // point.h #ifndef POINT_H #define POINT_H class Point public: Point(); Point( int, int ); ~Point(); void setx( int ); int getx() const; void sety( int ); int gety() const; void print() const; protected: int x; int y; ; #endif // point.cpp #include "point.h" // default constructor Point::Point() : x(0), y(0) Point::Point( int xvalue, int yvalue ) : x(xvalue), y(yvalue) Point::~Point() void Point::setX( int xvalue ) x = xvalue; int Point::getX() const return x; Εργαστήριο Αντικειµενοστραφούς Προγραµµατισµού µε C++ - Μάθηµα 8 ο -6-
void Point::setY( int yvalue ) y = yvalue; int Point::getY() const return y; void Point::print() const cout << '[' << x << ", " << y << ']'; Εργαστήριο Αντικειµενοστραφούς Προγραµµατισµού µε C++ - Μάθηµα 8 ο -7-
ΠΑΡΑ ΕΙΓΜΑ Β) ηµιουργία, ανάγνωση και προσθήκη δεδοµένων στο τέλος σε αρχείο κειµένου. using std::cin; using std::ios; #include <fstream> using std::ofstream; using std::ifstream; int main() char filename[80]; char buffer[255]; // for user input cout << "Please enter the file name: "; cin >> filename; //////////////////// Create new file //////////////////// ofstream fout(filename); if (!fout) cout << "Unable to open " << filename << " for writing.\n"; fout << "This line is written directly to the file...\n"; cout << "Enter text for the file: "; cin.ignore(1,'\n'); // ignore the newline after the file name cin.getline(buffer,255); // get the user's input fout << buffer << "\n"; // and write it to the file fout.close(); // close the file, ready for reopen //////////////////// Read created file ////////////////// ifstream fin(filename); if (fin) cout << "Here's the contents of the file: \n"; char ch; while (fin.get(ch)) cout << ch; cout << "\n***end of file contents.***\n"; fin.close(); //////////////////// Append to file ///////////////////// cout << "\nopening " << filename << " in append mode...\n"; fout.open(filename,ios::app); if (!fout) cout << "Unable to open " << filename << " for appending.\n"; Εργαστήριο Αντικειµενοστραφούς Προγραµµατισµού µε C++ - Μάθηµα 8 ο -8-
cout << "\nenter text for the file: "; cin.getline(buffer,255); fout << buffer << "\n"; fout.close(); //////////////////// Reread file after append /////////// fin.clear(); fin.open(filename,ios::in); // reassign existing fin object! if (fin.fail()) cout << "Unable to open " << filename << " for reading.\n"; cout << "\nhere's the contents of the file after appending: \n"; char ch; while (fin.get(ch)) cout << ch; cout << "\n***end of file contents.***\n"; fin.close(); return 0; Εργαστήριο Αντικειµενοστραφούς Προγραµµατισµού µε C++ - Μάθηµα 8 ο -9-