Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Object Oriented Programming. 2 What’s the Object? Global name space for functions in C Global name space for functions in C Every function is available.

Similar presentations


Presentation on theme: "1 Object Oriented Programming. 2 What’s the Object? Global name space for functions in C Global name space for functions in C Every function is available."— Presentation transcript:

1 1 Object Oriented Programming

2 2 What’s the Object? Global name space for functions in C Global name space for functions in C Every function is available everywhere Every function is available everywhere No structural association between functions and data No structural association between functions and data No hiding of private functions or data No hiding of private functions or data Code re-use is difficult Code re-use is difficult

3 3 Object Oriented Programming in C++ Classes as units of encapsulation Classes as units of encapsulation Information Hiding Information Hiding Inheritance Inheritance Polymorphism and dynamic dispatching Polymorphism and dynamic dispatching Storage management Storage management

4 4 Classes Encapsulation of type and related operations Encapsulation of type and related operations class point { double x,y; // private data members double x,y; // private data members public: public: point (int x0, int y0); // public methods point (int x0, int y0); // public methods point () { x = 0; y = 0;}; // a constructor point () { x = 0; y = 0;}; // a constructor void move (int dx, int dy); void move (int dx, int dy); void rotate (double alpha); void rotate (double alpha); int distance (point p); int distance (point p);}

5 5 A Class is a type : Objects are instances Create a global or local variable Create a global or local variable point p1(10, 20); // Calls constructor point(int x0, int y0) point p2; // Calls default constructor (no arguments) Create an object on the heap Create an object on the heap point p3 = new point; point is a type point is a type p1, p2, p3 are instances of the type p1, p2, p3 are instances of the type p1, p2, p3 are objects p1, p2, p3 are objects Definitions of Object: Definitions of Object: C++ An object is a region of storage C++ An object is a region of storage Java An object is a class instance or array Java An object is a class instance or array

6 6 Implementing methods Class definition is similar to ADA package spec, and usually appears in a header file Class definition is similar to ADA package spec, and usually appears in a header file No equivalent of ADA package body: each method can be defined separately No equivalent of ADA package body: each method can be defined separately void point::rotate (double alpha) { void point::rotate (double alpha) { x = x * cos (alpha) - y * sin (alpha); x = x * cos (alpha) - y * sin (alpha); y = y * cos (alpha) + x * cos (alpha); y = y * cos (alpha) + x * cos (alpha); }; }; x and y are data members of the object on which the x and y are data members of the object on which the method is being called. method is being called. Methods can be inlined Methods can be inlined Must be defined in the class declaration Must be defined in the class declaration “inline” keyword is a suggestion for the compiler “inline” keyword is a suggestion for the compiler

7 7 Constructors One of the best innovations of C++ One of the best innovations of C++ Special method (s) invoked automatically when an object of the class is declared Special method (s) invoked automatically when an object of the class is declared point (int x1, int x2); point (int x1, int x2); point (); point (); point (double alpha; double r); point (double alpha; double r); point p1 (10,10), p2; p3 (pi / 4, 2.5); point p1 (10,10), p2; p3 (pi / 4, 2.5); Allows initialization of instance Allows initialization of instance Setting default values Setting default values Initializing data structures Initializing data structures Name of method is name of class Name of method is name of class Declaration has no return type. Declaration has no return type.

8 8 Class Methods Member functions are called with a special syntax Member functions are called with a special syntax p1.move(3, -12); // p1 is an instance of class point Method Name Mangling Method Name Mangling point_move$i$i_v(point* this, int x0, int y0) Name includes class name and types of arguments Name includes class name and types of arguments Resolves identically named methods in different classes Resolves identically named methods in different classes Allows overloading of names within a class Allows overloading of names within a class Allows linking with programs written in C and other languages Allows linking with programs written in C and other languages And the instance is passed as a hidden argument And the instance is passed as a hidden argument point_move$i$i_v(p1, 3, -12)

9 9 Target of operation The implicit parameter in a method call can be retrieved through this: The implicit parameter in a method call can be retrieved through this: class Collection { class Collection { Collection& insert (thing x) { // return reference Collection& insert (thing x) { // return reference … modify data structure … modify data structure return *this; // to modified object return *this; // to modified object }; }; my_collection.insert (x1).insert (x2); my_collection.insert (x1).insert (x2); Common Java Idiom Common Java Idiom point (int x0, y0) { point (int x0, y0) { this.x0 = x0; this.x0 = x0; this.y0 = y0;} this.y0 = y0;}

10 10 Static Members Need to have computable attributes for class itself, independent of specific object; e.g. number of objects created. Need to have computable attributes for class itself, independent of specific object; e.g. number of objects created. Static qualifier indicates that entity is unique for the class – There is only one, shared by all class instances. Static qualifier indicates that entity is unique for the class – There is only one, shared by all class instances. static int num_objects = 0; static int num_objects = 0; point () { num_objects++;}; // ditto for other constructors point () { num_objects++;}; // ditto for other constructors Can access static data using class name or object name: Can access static data using class name or object name: if (point.num_objects != p1.num_objects) error (); if (point.num_objects != p1.num_objects) error ();

11 11 Classes and Private Types If all data members are private, class is identical to a private type: visible methods, including assignment. If all data members are private, class is identical to a private type: visible methods, including assignment. A struct is a class with all public members A struct is a class with all public members How much to reveal is up to programmer How much to reveal is up to programmer What about private constructors? What about private constructors? define functions to retrieve (not modify) private data define functions to retrieve (not modify) private data int xcoord () { return x;}; int xcoord () { return x;}; int ycoord () { return y;}; int ycoord () { return y;}; p2.x = 15; // error, data member x is private p2.x = 15; // error, data member x is private

12 12 Destructors If constructor allocates dynamic storage, need to reclaim it If constructor allocates dynamic storage, need to reclaim it class stack { class stack { int* contents; int sz; int* contents; int sz; public: public: stack (int size) { contents = new int [ sz = size];}; stack (int size) { contents = new int [ sz = size];}; void push (); void push (); int pop (); int pop (); int size () { return sz;}; } int size () { return sz;}; } stack my_stack (100); // allocate storage dynamically stack my_stack (100); // allocate storage dynamically // when is my_stack.contents released? // when is my_stack.contents released?

13 13 Destructors (cont.) User cannot deallocate data if data member is private: system must do it User cannot deallocate data if data member is private: system must do it stack ( ) {contents = new ContentType[n];}; ~stack ( ) {delete[ ] contents;}; inventive syntax: negation of constructor inventive syntax: negation of constructor Symmetry of ctor & dtor makes program easier to understand Symmetry of ctor & dtor makes program easier to understand Called: Called: automatically when object goes out of scope automatically when object goes out of scope indirectly by delete indirectly by delete Almost never called explicitly Almost never called explicitly Many uses: Many uses: Linked list maintenance Linked list maintenance Files: open in constructor, close in destructor Files: open in constructor, close in destructor

14 14 Copy and Assignment point p3 (10,20); point p3 (10,20); point p5 = p3; // componentwise copy point p5 = p3; // componentwise copy This can lead to unwanted sharing: This can lead to unwanted sharing: stack stack1 (200); stack stack1 (200); stack stack2 = stack1; // stack1.contents shared stack stack2 = stack1; // stack1.contents shared stack2.push (15); // stack1 is modified stack2.push (15); // stack1 is modified Need to redefine assignment and copy Need to redefine assignment and copy

15 15 Copy Constructors stack (const stack& s) { // reference to existing object contents = new int [ sz = s.size()]; contents = new int [ sz = s.size()]; for (int I = 0; I <sz; I++) contents [I] = s.contents [I]; for (int I = 0; I <sz; I++) contents [I] = s.contents [I]; } stack s1 (100); stack s1 (100); … stack s2 = s1; // invokes copy constructor stack s2 = s1; // invokes copy constructor

16 16 Redefining Assignment assignment can also be redefined to avoid unwanted sharing assignment can also be redefined to avoid unwanted sharing operator returns a reference, so it can be used efficiently in chained assignments: one = two = three; operator returns a reference, so it can be used efficiently in chained assignments: one = two = three; stack & operator= (const stack& s) { stack & operator= (const stack& s) { if (this != &s) { // beware of self-assignment if (this != &s) { // beware of self-assignment delete [] contents; // discard old value delete [] contents; // discard old value contents = new int [sz = s.size ()]; contents = new int [sz = s.size ()]; for (int I = 0; I <sz; I++) contents [I] = s.contents [I]; for (int I = 0; I <sz; I++) contents [I] = s.contents [I]; } return *this; } return *this; } stack s1 (100), s2 (200); … s1 = s2; // transfer contents stack s1 (100), s2 (200); … s1 = s2; // transfer contents

17 17 Operator Overloading: indexing Useful to create range-checked structures: Useful to create range-checked structures: class four_vect { class four_vect { double stor[4]; // private member, actual contents of vector. double stor[4]; // private member, actual contents of vector. … double& operator[ ] (int j) { double& operator[ ] (int j) { if (j 3) throw index_error; // defined elsewhere if (j 3) throw index_error; // defined elsewhere return stor[j]; return stor[j]; }; }; Note: return type is a reference, so can be used on l.h.s

18 18 Extending the meaning of subscripting An associative array: An associative array: class Assoc { // a map from strings to numbers struct Pair { // an inner class struct Pair { // an inner class char* name; char* name; double val; double val; Pair (char* n = “”, double v = 0): name (n), val (v) { }; }; Pair (char* n = “”, double v = 0): name (n), val (v) { }; }; pair * contents; // Assoc is a set of pairs pair * contents; // Assoc is a set of pairs public: public: Assoc () { }; // default constructor Assoc () { }; // default constructor double& operator[ ] (const char *); // map string => number double& operator[ ] (const char *); // map string => number};

19 19 Efficiency vs. privacy Linear algebra package: vector class for 4-vectors, matrix class for 4-by-4 matrices Linear algebra package: vector class for 4-vectors, matrix class for 4-by-4 matrices vector class redefines [ ] to do check index vector class redefines [ ] to do check index matrix class redefines [ ] to check both indices matrix class redefines [ ] to check both indices want to implement matrix * vector: want to implement matrix * vector: v1 [j] =  m[j][k] * v [k]; v1 [j] =  m[j][k] * v [k]; 4 range-checks for every component, all redundant. 4 range-checks for every component, all redundant.

20 20 Relaxing privacy: friends A friend function can access private members of a class A friend function can access private members of a class a friend is declared in the class (cannot sneak in) a friend is declared in the class (cannot sneak in) class vector {... class vector {... friend vector operator* (const matrix&, const vector&); friend vector operator* (const matrix&, const vector&); a function can be the friend of several classes a function can be the friend of several classes class matrix {… class matrix {… friend vector operator*(const matrix&, const vector&); friend vector operator*(const matrix&, const vector&); A class can be a friend, so all its function members are. A class can be a friend, so all its function members are. class quaternion { friend class matrix; …} class quaternion { friend class matrix; …} A friend in not a class member A friend in not a class member

21 21 Efficient code class matrix { vector row [4]; public: … }; class matrix { vector row [4]; public: … }; vector operator * (const matrix&m, const vector v) { vector operator * (const matrix&m, const vector v) { vector res; vector res; for (int i = 0; i < 4; i ++) { for (int i = 0; i < 4; i ++) { res.stor[i] = 0; // stor is private data in a vector res.stor[i] = 0; // stor is private data in a vector for (int j = 0; j < 4; j++) for (int j = 0; j < 4; j++) res.stor[j] += m.row[i].stor[j] * v.stor[j]; // row is private res.stor[j] += m.row[i].stor[j] * v.stor[j]; // row is private }; }; return res; return res; }; };

22 22 Packages and private information In Ada, declare both types in same package. Package body has access to full declarations for both: In Ada, declare both types in same package. Package body has access to full declarations for both: package linear_algebra is package linear_algebra is type vector is private; type vector is private; type matrix is private; type matrix is private; function “*” (m : matrix; v : vector) return vector; function “*” (m : matrix; v : vector) return vector; private private type vector is array (1..4) of long_float; type vector is array (1..4) of long_float; type matrix is array (1..4) of vector; type matrix is array (1..4) of vector; -- code in body can use array representation of both. -- code in body can use array representation of both. end linear_algebra; end linear_algebra;

23 23 Inheritance General mechanism for extending existing classes. General mechanism for extending existing classes. Specialization: an X is an A Specialization: an X is an A A mammal is a vertebrate A mammal is a vertebrate a feline is a mammal a feline is a mammal a cat is a feline a cat is a feline a persian is a cat a persian is a cat A persian has all the attributes of a feline, a mammal, and a vertebrate A persian has all the attributes of a feline, a mammal, and a vertebrate A class hierarchy implements a sequence of Is-A relations A class hierarchy implements a sequence of Is-A relations

24 24 Advantages of inheritance Factorization: Common properties are grouped in a single class Factorization: Common properties are grouped in a single class code reuse : Descendant class (derived class) is defined incrementally over parent. code reuse : Descendant class (derived class) is defined incrementally over parent. incremental programming : New derived classes can be added without changing existing parents and siblings. incremental programming : New derived classes can be added without changing existing parents and siblings. Is inheritance Polymorphism? Is inheritance Polymorphism? Can describe collections of diverse objects belonging to a class hierarchy. Can describe collections of diverse objects belonging to a class hierarchy. Just saves copying common factors in related classes Just saves copying common factors in related classes

25 25 Derivation class point { // base class class point { // base class int x, y; int x, y; public: public: point () { x = 0; y = 0}; // basic constructor point () { x = 0; y = 0}; // basic constructor void move (int dx, int dy) { x += dx; y += dy}; } void move (int dx, int dy) { x += dx; y += dy}; } void display ( ) ; // put black dot on the screen. void display ( ) ; // put black dot on the screen. } class color_point: public point { // derived class class color_point: public point { // derived class int R, G, B; // in addition to hidden members x, y int R, G, B; // in addition to hidden members x, y public: public: color_point ():point () {R = 50; G = 50; B = 50;}; // call parent constr. color_point ():point () {R = 50; G = 50; B = 50;}; // call parent constr. void lighten (int w) { R -= w; G -=w; B -= w;}; void lighten (int w) { R -= w; G -=w; B -= w;}; void display ( ); // put colored dot on the screen void display ( ); // put colored dot on the screen // move is inherited, applies to color_points as well // move is inherited, applies to color_points as well }; };

26 26 Substitution rule An object from a derived class can be used wherever an object from the base class is expected. An object from a derived class can be used wherever an object from the base class is expected. point* p1 = new color_point ( ); point* p1 = new color_point ( ); works because a descendant has all the attributes of the parent (possibly with different meanings). works because a descendant has all the attributes of the parent (possibly with different meanings). color_point* wrong = new point ( ); color_point* wrong = new point ( ); // error: incomplete object // error: incomplete object but p1 can be coerced (cast) to a color_point, because it is one. but p1 can be coerced (cast) to a color_point, because it is one.

27 27 inheritance and privacy Private members of the parent class are not visible in (the methods of) the derived class. Private members of the parent class are not visible in (the methods of) the derived class. int color_point::abcisa ( ) {return x; }; // error int color_point::abcisa ( ) {return x; }; // error Constructor for parent is used to initialize parent data. Constructor for parent is used to initialize parent data. Derived (…) : Parent (…) { …}; // rest of construction Derived (…) : Parent (…) { …}; // rest of construction Protected members are visible in descendants, but not outside. Protected members are visible in descendants, but not outside.

28 28 Polymorphism Because of substitution rule, can have collection of various kinds of points: Because of substitution rule, can have collection of various kinds of points: point* figure [100]; point* figure [100]; To display collection, apply the appropriate display method to each: To display collection, apply the appropriate display method to each: point::display () point::display () color_point::display () color_point::display () blinkling_point::display () blinkling_point::display () Could add discriminant to each object, to recognize its class Could add discriminant to each object, to recognize its class But don’t do it! But don’t do it! Best to use virtual methods. Best to use virtual methods.

29 29 Virtual methods class parent { class parent { virtual void change (int x) … virtual void change (int x) … class child: public parent { class child: public parent { virtual void change (int x) … // overrides parent operation virtual void change (int x) … // overrides parent operation class grandchild: public child { class grandchild: public child { virtual void change (int x)… // overrides child operation virtual void change (int x)… // overrides child operation parent* someone = … // can be any member of family parent* someone = … // can be any member of family someone-> change () // the proper one for someone’s class someone-> change () // the proper one for someone’s class

30 30 Dynamic dispatching If M is virtual, the call ptr -> m (..) must determine the actual nature of ptr, and invoke the proper method If M is virtual, the call ptr -> m (..) must determine the actual nature of ptr, and invoke the proper method Each class is described by a table of virtual methods (the vtable) Each class is described by a table of virtual methods (the vtable) Each object carries a pointer to the vtable. Each object carries a pointer to the vtable. Each derived class inherits the table Each derived class inherits the table each overriding modifies an entry in the table each overriding modifies an entry in the table Dynamic dispatching is an indirect call through an entry at a known position in the vtable. Dynamic dispatching is an indirect call through an entry at a known position in the vtable.

31 31 Overloading and dynamic dispatching Overloading resolution is a compile-time process that applies to an expression in a context: Overloading resolution is a compile-time process that applies to an expression in a context: that = this_one + the_other; // find right numeric type that = this_one + the_other; // find right numeric type dynamic dispatching is a run-time activity that applies to a reference: dynamic dispatching is a run-time activity that applies to a reference: some_relative-> display (); // indirect call some_relative-> display (); // indirect call tommy.display (); // static resolution tommy.display (); // static resolution overloading resolution may fail if call is ambiguous overloading resolution may fail if call is ambiguous dynamic dispatching succeeds because derived classes have all the virtual methods of ancestors. dynamic dispatching succeeds because derived classes have all the virtual methods of ancestors.

32 32 The profile of an overriding method class parent { virtual void method (int x); virtual void method (int x); virtual void relation (parent x); virtual void relation (parent x); class child: public parent { virtual void method (int x); // overrides inherited method virtual void method (int x); // overrides inherited method virtual void method (double x); // different method virtual void method (double x); // different method Binary operations are not symmetric: virtual void relation (child x); virtual void relation (child x); // does not override parent relation // does not override parent relation virtual void relation (parent x); // overrides virtual void relation (parent x); // overrides

33 33 Abstract classes Base class may not have enough attributes to create meaningful object: Base class may not have enough attributes to create meaningful object: class shape { class shape { point anchor; point anchor; int color [3]; int color [3]; virtual void display (); // can’t write anything here virtual void display (); // can’t write anything here }; }; shape blob; // can’t do anything with it shape blob; // can’t do anything with it If methods are declared abstract, cannot create objects of class: If methods are declared abstract, cannot create objects of class: virtual void display ( ) = 0; // must be overridden virtual void display ( ) = 0; // must be overridden

34 34 A useful pattern: the factory Need to construct different objects based on run-time values Need to construct different objects based on run-time values Construct objects based on user input Construct objects based on user input Handle messages of different types Handle messages of different types class MessageFactory { class MessageFactory { Message messageHandler(byte[] data) { switch (data[0]) { switch (data[0]) { case 1: return new BuyMessage(data); case 2: return new SellMessage(data); case 3: return new CloseMessage(data);... } }; };

35 35 Multiple inheritance Class can inherit from several parents: Class can inherit from several parents: class abstract_stack { // algebraic definition class abstract_stack { // algebraic definition public: public: void push (int x) = 0; void push (int x) = 0;...... }; }; class vector {…}; // sequential allocation class vector {…}; // sequential allocation class stack: public abstract_stack, public vector { class stack: public abstract_stack, public vector { public: public: void push (int x) {.. }; // use vector primitives void push (int x) {.. }; // use vector primitives } Mix-in Inheritance Mix-in Inheritance

36 36 Multi-methods What if we want a function to be dynamically dispatched in two dimensions? What if we want a function to be dynamically dispatched in two dimensions? Shape.render(device) vs. Device.render(shape) Shape.render(device) vs. Device.render(shape) void render(virtual device& d, virtual shape& s); ??? void render(virtual device& d, virtual shape& s); ??? d.s.render(); ??? d.s.render(); ??? Multi-Methods allow multidimensional polymorphism Multi-Methods allow multidimensional polymorphism Dylan supports multi-methods Dylan supports multi-methods Dispatch mechanism must accommodate all possible combinations Dispatch mechanism must accommodate all possible combinations Can require dispatch table: Cartesian product of types – very expensive Can require dispatch table: Cartesian product of types – very expensive Other Solutions Other Solutions Overloading Overloading Double dispatch Pattern Double dispatch Pattern Device::render(Shape& s) { s.render(this); // but shape has to know about different devices s.render(this); // but shape has to know about different devices}


Download ppt "1 Object Oriented Programming. 2 What’s the Object? Global name space for functions in C Global name space for functions in C Every function is available."

Similar presentations


Ads by Google