220 likes | 312 Views
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
E N D
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
#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
... #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
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