Download presentation
Presentation is loading. Please wait.
Published byJoseph Allen Modified over 8 years ago
1
Modern Database Techniques Part 1: Object Oriented Databases 2. Concepts of Object Oriented Programming Revisited
2
2 Object oriented Concepts Classes and objects Inheritance and class hierarchies Aggregation, compound objects Methods Object identity Abstract data types
3
3 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();}
4
4 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/28is printed. Classes don't form a database: "Search for all objects of class Fraction > 1/4" is not possible.
5
5 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 (); }
6
6 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.
7
7 Comparison of Subclasses and Aggregation a special point with height and width 15;12 Line s: initial point end-point 18;9 line width: 2 Rectangle w: 2; 3;5; 3 Point a: 2; 15 correct: a.x, w.x s.end.x wrong:s.x
8
8 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
9
9 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.
10
10 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
11
11 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.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.