Δυναμική μνήμη με πίνακες και λίστες
Ατζέντα ονομάτων Οι πίνακες βοηθάνε στην εύκολη προσπέλαση, στην σειριοποίηση των δεδομένων για αποθήκευση ή μετάδοση. Απαιτούν ωστόσο είτε προκαταβολική δέσμευση μνήμης είτε υπολογιστικά χρονοβόρα συντήρηση struct contact *mycontactlist; Ευρετήρια μπορούν να χρησιμοποιηθούν για να αναπαραστήσουν την ταξινόμηση struct contactptrlistelement struct contact *ptrtomatrixcell; struct contactptrlistelement *next; struct contactptrlistelement *head;
Σχεδιαστική εφαρμογή (2Δ) Επεξεργασία 2Δ σχημάτων. Οπτικοποίηση (visualization). Εισαγωγή, διαγραφή, τροποποίηση. Δομή δεδομένων print = οπτικοποίηση. (θεμελιώδεις εντολές γραφικών, π.χ. σημείο, γραμμή κύκλος) new = δημιουργία νέας καταχώρησης update = εύρεση και ενημέρωση υπάρχουσας καταχώρησης Λίστα από σχήματα και κύκλους
Μόνο ευθυγραμμισμένα παραλληλόγραμμα Π.χ. από το σημείο (50,60) στο σημείο (120,130). Input: command line vs GUI
Μόνο ευθυγραμμισμένα παραλληλόγραμμα struct point float x,y; ; struct rect struct point topleft, bottomright; ; struct rectlistelement struct rect *rectangledata; struct rectlistelement *next; ; struct rect *A; // new matrix struct rectlistelement *head; // new list
Εντολές Print/Draw: προσπέλασε τη λίστα και εκτύπωσε / ζωγράφισε κάθε παραλληλόγραμμο. Ζωγράφισε = βιβλιοθήκη γραφικών. Η σειρά προσπέλασης καθορίζει πιο σχήμα θα είναι «πάνω από το άλλο» Επιλογή και διαγραφή (Find and Delete) Η επιλογή περιέχει εύρεση. Στο 2Δ είναι μέσω σημείου (π.χ. από το ποντίκι). Η σειρά προσπέλασης καθορίζει πιο σχήμα θα βρεθεί πρώτο. Νέο σχήμα. New element. Τροποποίηση σχήματος Set element.
New element void newelement(listel *head, point p0, point p1) rect newrect; // new rect newrect.topleft.x = p1.x; newrect.topleft.y = p1.y; newrect.bottomright.x = p2.x; newrect.bottomright.y = p2.y; // add rect to list (make a new node, link it etc)
Set element void setelement(listel *head, point target, point p0, point p1) for (x=head;x->next!null;x=x->next) // each recatngle if target is inside current rectangle //update x->topleft->x = p1.x; x->topleft->y = p1.y; x->bottomright->x = p2.x; x->bottomright->y = p2.y; break;
New element void newelement(listel *head, point p0, point p1) Point p1 = new Point(10,20); Point p2 = new Point(120,130) Rect r1 = new Rect(p1,p2); // add rect to list (make a new node, link it etc) ListEl *newnode = (ListEl *) malloc(sizeof(listel)); // join ptrs for new node
Set element void setelement(listel *head, point target, point p0, point p1) for (x=head;x->next!null;x=x->next) // each recatngle if target is inside current rectangle //update Point p1 = new Point(10,20); Point p2 = new Point(120,130) x->setrect(p1,p2); break;
Structs my_birthday: y // simple Date d m // guarantee initialization with constructor // provide some notational convenience class Date int y,m,d; // year, month, day Date(int y, int m, int d); // constructor: check for valid date and initialize void add_day(int n); // increase the Date by n days ; // Date my_birthday; // error: my_birthday not initialized Date my_birthday(12, 30, 1950); // oops! Runtime error Date my_day(1950, 12, 30); // ok my_day.add_day(2); // January 1, 1951 my_day.m = 14; // ouch! (now my_day is a bad date)
Structs my_birthday: y // simple Date d m // guarantee initialization with constructor // provide some notational convenience class Date int y,m,d; // year, month, day Date(int y1, int m1, int d1); // constructor: check for valid date and initialize void add_day(int n); // increase the Date by n days ; Date(int y, int m, int d) this.y=y; this.m=m; this.d=d;
Classes // simple Date (what can we do in case of an invalid date?) class Date public: class Invalid ; // to be used as exception Date(int y, int m, int d); // private: int y,m,d; bool check(int y, int m, int d); ; Date:: Date(int yy, int mm, int dd) if (!check(y,m,d)) throw Invalid(); // check for valid date and initialize // year, month, day // is (y,m,d) a valid date? // check for validity
Πλεονεκτήματα οντοκεντρικού προγραμματισμού class Rect public: ; private: Rect(Point p1, Point p2); void setrect(point p1, Point p2); void printrect(); float getp1(); float getp2(); float x,y; Point p1 = new Point(10,20); Point p2 = new Point(120,130) Rect r1 = new Rect(p1,p2);
Πλεονεκτήματα οντοκεντρικού προγραμματισμού Point p1 = new Point(10,20); Point p2 = new Point(120,130) Rect r1 = new Rect(p1,p2); for (x=head;x->next!=null;x=x->next) x->printrect(); for (x=head;x->next!=null;x=x->next) if // point is inside rect // delete this rect (x)
Κλάση για 2Δ σημείο class Point public: Point(float x, float y); void setpoint(float x, float y); void printpoint(); float getx(); float gety(); private: float x,y; ; Point p1 = new Point(10,20);
Κλάση για 2Δ σημείο Point::setPoint(float x, float y) this.x = x; this.y = y; Point::Point(float x, float y) setpoint(x,y); Point::printPoint() cout << x << << y << endl;
Κλάση για 2Δ σημείο float Point::getX() return x; // this float Point::getY() return y; // this
Κλάση για 2Δ ευθυγραμισμένο παραλληλόγραμμο class Rect public: Rect(Point p1, Point p2); ~Rect(); void setrect(point p1, Point p2); void printrect(); float getp1(); float getp2(); char *name; private: float x,y; ;
Constructor Rect::Rect(Point p1, Point p2, char *inputname) name = (char *) malloc((strlen(inputname)+1)*sizeof(char)); strcpy(name,inputname); setrect(p1,p2); Rect::setRect(Point p1, Point p2, char *newname) free(name); name = (char *) malloc((strlen(newname)+1)*sizeof(char)); strcpy(name,newname); setrect(p1,p2);
Destructor Rect::~Rect() free(name);
class Line public: void setlength( double len ); double getlength( void ); Line(); // This is the constructor declaration ~Line(); // This is the destructor: declaration private: double length; ; // Member functions definitions including constructor Line::Line(void) cout << "Object is being created" << endl; Line::~Line(void) cout << "Object is being deleted" << endl;
void Line::setLength( double len ) length = len; double Line::getLength( void ) return length; int main( ) Line line; line.setlength(6.0); cout << "Length of line : " << line.getlength() <<endl; return 0; Object is being created Length of line : 6 Object is being deleted
Graphics
2Δ Πίνακας - ASCII
2Δ Πίνακας - ASCII
2Δ Πίνακας - ASCII
2Δ Πίνακας - ASCII
class Rect public: Rect(Point p1, Point p2); void setrect(point p1, Point p2); void printrect(); float getp1(); float getp2(); float get Area(); void drawrect(char **img, int rows, int columns); private: float x,y; ;
Inheritance / Virtual class