1 / 22

Procedure v.s. Object

To write a program is to:. Procedure v.s. Object. find a way to manipulate data -- We design procedures Procedure-Oriented Programming Statements + functions  programs. find data to manipulate – We choose data (object) Object-Oriented Programming Classes + Objects  programs

Download Presentation

Procedure v.s. Object

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. To write a program is to: Procedure v.s. Object • find a wayto manipulate data -- • We design procedures • Procedure-Oriented Programming • Statements + functions  programs • find data to manipulate – • We choose data (object) • Object-Oriented Programming • Classes + Objects  programs • (interfaces, methods, attributes…) ITK 279 @ Chung-Chih Li

  2. Vehicle SUV instantiation Chevrolet Tahoe Class Objects Honda Pilot not these Drive this instantiation I J 000-AAA 011-JAV Object Integer I,J; I = new Integer(3); J = new Integer(9); int i,j; i = Integer.parseInt(“1”); j = I.parseInt(“1”); Number Integer Double ITK 279 @ Chung-Chih Li

  3. Class: Method & Attribute Attribute (or, Field):the properties the class Like a noun, its value is an adjective. E.g., wheels, seats, engine, windows, color Method (or, member function):behaviors, operation, functionalities. Like a verb, its definition is the behavior. E.g., accelerate, start, stop, turn, lock ITK 279 @ Chung-Chih Li

  4. Point Class: A Point needs to have: x, y. Or we may want to know: distance from the origin distance to another point, or, shift, get the value of x, get the value of y, change the value of x and y. ITK 279 @ Chung-Chih Li

  5. structpoint { double x; double y; }; void main() { point p,q; double a,b,distance; p.x = 3.0; p.y = 4.0; q.x = q.y = 5.4; a = p.x – q.x; b = p.y – q.y; distance = sqrt(a*a+ b*b); cout << “(“ << p.x << “,” << p.y << “):”; cout << “(“ << q.x << “,” << q.y << “) == ” << dis; p.x = p.x+2; p.y = p.y+3; // move (2,3) .... } Old fashion:struct ITK 279 @ Chung-Chih Li

  6. C++ :class classpoint { public: // public means every one can use the following. double x; double y; double distance(point p); double from_org(); void shift(point v); }; .... void main() { point p,q; p.x = 3.0; p.y = 4.0; q.x = q.y = 5.4; cout << ........ << p.distance(q); p.shift(q); } member variables member functions ITK 279 @ Chung-Chih Li

  7. A better designedclass class point { public: void set_x(double x); void set_y(double y); double get_x(); double get_y(); double distance(point p); double from_org(); void shift(point v); private: double my_x; double my_y; double diff(double x, double y); // this is just a helper }; .... public section private section // private means no one but the class’s function can use ITK 279 @ Chung-Chih Li

  8. Using member functions Mutator:a member function that can change the values of member variables point p,q,v; p.set_x(5); p.set_y(5); q.set_x(2); q.set_y(3); v.set_x(1); v.set_y(-1.2); cout << p.get_x(); cout << q.get_x(); q.move(v); cout << p.from_org() << “:” << q.from_org(); double distance = p.distance(v); ............. distance = q.distnace(p); Accessor: a member function that provide an access to member variables ITK 279 @ Chung-Chih Li

  9. Private variables and functions can’t be used from outside class point { public:.................... private: double my_x; double my_y; double diff(double x, double y); // this is just a helper }; .... void main() { point p,q,v; double a cout << p.my_x; cout << q.my_y; a = p.diff(3.2, 4); } // diff, my_x, and my_y are private ITK 279 @ Chung-Chih Li

  10. point p,q; p.set_x(4); p.set_y(5); q.set_x(2); q.set_y(3); cout << p.from_org() << “:” << q.from_org(); 2 Objects of a class construct construct P: set_x(.)... set_y(.)... from_org(.).. ........ q: set_x(.)... set_y(.)... from_org(.).. ........ my_x my_y my_x my_y p’s member function call q’s member function call ITK 279 @ Chung-Chih Li

  11. class point { public: void set_x(double x); void set_y(double y); double get_x(); double get_y(); double distance(point p); double from_org(); void shift(point v); private: double my_x; double my_y; double diff(double x, double y); // this is just a helper }; void point::set_x(double a) { my_x = a; } double point::get_y() { return my_y; } Implementation ofclass name of the class name of the member ITK 279 @ Chung-Chih Li

  12. Implementation of point void point::set_x(double a) { my_x = a; } void point::set_y(double a) { my_y = a; } double point::get_x() { return my_x; } double point::get_y() { return my_y; } Mutator:a member function that can change the values of member variables Accessor: a member function that provide an access to member variables ITK 279 @ Chung-Chih Li

  13. Private member function double point::distance(point p) { double a,b,c; a = diff(my_x, p.my_x); b = diff(my_y, p.my_y); c = sqrt(a*a+b*b); return c; } double point::from_org() { point o; o.set_x(0); o.set_y(0); return distance(o); } Using its own private member function Using an o’s member function Using its own member function ITK 279 @ Chung-Chih Li

  14. Other’s private variables and functions: They are in the same class, so can be used. double point::from_org() { point p; p.set_x(0); p.set_y(0); double a,b,c; a = p.diff(my_x, p.my_x); a = a*a; b = diff(my_y, p.my_y); b = b*b; c =sqrt(a+b); return c; } Using p’s private variable Using p’s private member function Using own private member function ITK 279 @ Chung-Chih Li

  15. void printp(point p) { cout << "(" << p.get_x() << "," << p.get_y() << ")"; } int main() { point p,q,v; p.set_x(5); p.set_y(6); q.set_x(1); q.set_y(1); printp(p); cout << " to (0,0) :: " << p.from_org() << endl; printp(q); cout << " to (0,0) :: " << q.from_org() << endl << endl; for (int i = 0; i<4; i++) { v.set_x(i); v.set_y(i); q.shift(v); printp(q); cout << " Distance to p = " << q.distance(p) << " = " << p.distance(q) << endl; } return 0; } Using the class (5,6) to (0,0) :: 7.81025 (1,1) to (0,0) :: 1.41421 (1,1) Distance to p = 6.40312 = 6.40312 (2,2) Distance to p = 5 = 5 (4,4) Distance to p = 2.23607 = 2.23607 (7,7) Distance to p = 2.23607 = 2.23607 ITK 279 @ Chung-Chih Li

  16. Constructor Compiler will create a default constructor; but can’t take care of too many details like... int main() { point p,q; ..... ...... } P: set_x(.)... set_y(.)... from_org(.).. ........ q: set_x(.)... set_y(.)... from_org(.).. ........ my_x my_y my_x my_y ITK 279 @ Chung-Chih Li

  17. Explicitly ask the Constructor to do something int main() { point p,q; ..... ...... } A better constructor should P: set_x(.)... set_y(.)... from_org(.).. ........ q: set_x(.)... set_y(.)... from_org(.).. ........ my_x my_y my_x my_y ITK 279 @ Chung-Chih Li

  18. Explicitly define a default Constructor class point { public: point (); ...... private: ...... }; point::point() { my_x = my_y = 0; } point a; point b(); a: ..... ..... my_x my_y b: ..... ..... my_x my_y ITK 279 @ Chung-Chih Li

  19. Define two Constructors class point { public: point (); point (double x, double y); ...... private: ...... }; point::point() { my_x = my_y = 0; } point::point(double x, double y) { my_x = x; my_y = y; } point a; point b(7,4); a: ..... ..... b: ..... ..... my_x my_y my_x my_y default constructor another constructor point::point(double x, double y) : my_x(x), my_y(y) { } alternative initialization ITK 279 @ Chung-Chih Li

  20. #include <iostream> #include <cmath> #include "points.h" using namespace std; point::point() :my_x(0), my_y(0) {} point::point(double x, double y) :my_x(x), my_y(y) {} void point::set_x(double a) { my_x = a; } void point::set_y(double a) { my_y = a; } double point::get_x() { return my_x; } double point::get_y() { return my_y; } Separate Compiling We need this line class point { public: point(); point(double x, double y); void set_x(double x); void set_y(double y); double get_x(); double get_y(); double distance(point p); double from_org(); void shift(point v); private: double my_x; double my_y; double diff(double x, doubley); }; points.h Header file, (class interface) points.cpp Class implementation file ITK 279 @ Chung-Chih Li

  21. ... #include "points.h" ............. ............. point::point() ............. ............. ............. class point { public: point(); ...... private: double my_x; ....... }; Separate Compiling Under Unix 1 points.cpp points.h $g++ -c points.cpp generates points.o Compiling only, generate object code 2 ..... #include “points.h” ...... int main() { point p; ........ } $g++ points.oprog.cpp Compile progr.cpp and link its object code with points.o to create an executable program (a.out). prog.cpp ITK 279 @ Chung-Chih Li

  22. What we won’t cover in this class: • Inheritance • Virtual functions • Polymorphisms • The heart of OOP But we will discuss more on: • Copy constructor • Destructors • Assignment operators Whom to blame: Pointers and dynamic arrays ITK 279 @ Chung-Chih Li

More Related