120 likes | 272 Views
Modern Database Techniques Part 1: Object Oriented Databases. 2. Concepts of Object Oriented Programming Revisited. Object oriented Concepts. Classes and objects Inheritance and class hierarchies Aggregation, compound objects Methods Object identity Abstract data types.
E N D
Modern Database TechniquesPart 1: Object Oriented Databases 2. Concepts of Object Oriented Programming Revisited
Object oriented Concepts • Classes and objects • Inheritance and class hierarchies • Aggregation, compound objects • Methods • Object identity • Abstract data types
Classes and Objects: Example in C++ • Similar objects form a class • class Fraction{private: long Numerator; long Denominator; reduce(); public: Fraction (long n, long d = 1) {Numerator = n; Denominator = d; reduce();} Fraction& plus(const Fraction &x) {Numerator = Numerator * x.Denominator + Denom... Denominator = Denominator * x.Denominator; reduce();}
Objects instead of Variables • void print () { cout << Numerator << "/" << Denominator;}}; // End of class definition • Fraction f1 (3, 4);Fraction f2 (1, 7);f1.plus(f2);f1.print (); • 25/28 is printed. • Classes don't form a database: "Search for all objects of class Fraction > 1/4"is not possible.
Inheritance:Classes with Subclasses • class Point{private: int x, y; public: Point (int px = 0, int py = 0) {x=px; y=py;} move (int dx, int dy) {x+=dx; y+=dy;} int draw ();} • class Rectangle: public Point{private: int height, width; public: Rectangle (int x, int y, int height, int width); int draw ();}
Aggregation • Objects may consist of component objects • class Line{private: Point start, end; int linewidth; public: Line (int xs, int ys, int xe, int ye, int lw); int draw ();} • Rectangle is subclass of Point, i. e. the lower left point and additionally height and width. • Line is not a subclass of Point, but is an aggregation of initial point and end-point.
Comparison of Subclasses and Aggregation Line s:initial point Point a: 2; 15 15;12 end-point 18;9 line width: 2 correct: a.x, w.x s.end.x wrong: s.x a special point with height and width Rectangle w: 2; 3; 5; 3
Methods • Methods are functions belonging to a class. • Instance methods: Let a be an object and m be a method of class c. a.m(parameter) sends a message to a, to run method m. • Class methods are independent of instances, e. g. constructor methods. • Advantages: • Less side effects • It is similar to get an attribute or to call a method it's easy to modify a method's implementation, e. g. • Attribute Age or • Method Age: calculate age using the date of birth • Interface definition and implementation are separated
Object Identity • Each object gets a unique identifier: OID • This OID does not change during the whole lifetime of the object. • The OID remains the same even when the object's values change. • OIDs need not be specified explicitly. • OID can't be changed.
Classes Implement Abstract Data Types (ADT) • Abstract data types define the behavior of data, but hide the implementation. • Classes are suitable to define ADTs. • Especially template classes are used. • Example: ADT Stack Use a template class Stack to generate: • stacks for integer numbers • stacks for floating point numbers • stacks for Persons
Example ADT Stack TYPE: Stack [e_Type] METHODS: empty?: Stack [e_Type] BOOLEAN new: Stack [e_Type] push: e_Type Stack [e_Type] Stack [e_Type] pop: Stack [e_Type] Stack [e_Type] top: Stack [e_Type] e_Type CONSTRAINTS: pop (k Stack[e_Type]): NOT empty? (k) top (k Stack[e_Type]): NOT empty? (k) RULES: e e_TypE, k Stack[e_Type]: empty? (New()); NOT empty? (push (e,k)); top (push (e,k)) = e; pop (push (e,k)) = k.