Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Classes: A class is a concept of something Vehicle 4 wheels, seats, engine, windows, color…… accelerate, start, stop, turn, lock,............... Truck.

Similar presentations


Presentation on theme: "1 Classes: A class is a concept of something Vehicle 4 wheels, seats, engine, windows, color…… accelerate, start, stop, turn, lock,............... Truck."— Presentation transcript:

1 1 Classes: A class is a concept of something Vehicle 4 wheels, seats, engine, windows, color…… accelerate, start, stop, turn, lock,............... Truck ………… Sedan ………… SUV ………… Matrix ………… Focus ………… attributes methods

2 2 Method & Attribute class: Method (or, member function): behaviors, operation, functionalities. Like a verb: E.g., accelerate, start, stop, turn, lock Attribute (or, Field): the properties the class Like a noun or adjective. E.g., wheels, seats, engine, windows, color

3 3 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.

4 4 Old fashion: struct struct point { 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).... }

5 5 C++ : class class point { 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

6 6 A better designed class 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

7 7 Using member functions 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); Mutator: a member function that can change the values of member variables Accessor: a member function that provide an access to member variables

8 8 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

9 9 2 Objects of a class P: set_x(.)... set_y(.)... from_org(.).......... 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(); 4 5 my_x my_y q: set_x(.)... set_y(.)... from_org(.).......... 2 3 my_x my_y construct p’s member function call q’s member function call

10 10 Implementation of class 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; } name of the member name of the class

11 11 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

12 12 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 its own member function Using an o’s member function

13 13 Other’s Private member function: They are in the same class 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 own private member function Using p’s private member function Using p’s private variable

14 14 Using the class 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; } (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

15 15 Constructor int main() { point p,q;........... } P: set_x(.)... set_y(.)... from_org(.).......... #$? my_x my_y q: set_x(.)... set_y(.)... from_org(.).......... *&? $#? my_x my_y Compiler will create a default constructor; but can’t take care of too much detail like...

16 16 Explicitly define a Constructor int main() { point p,q;........... } P: set_x(.)... set_y(.)... from_org(.).......... 0 0 my_x my_y q: set_x(.)... set_y(.)... from_org(.).......... 0 0 my_x my_y A better constructor should

17 17 Explicitly define a default Constructor class point { public: point ();...... private:...... }; point::point() { my_x = my_y = 0; } point a; point b(); 0 0 my_x my_y a:..... 0 0 my_x my_y b:.....

18 18 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); 0 0 my_x my_y a:..... 7 4 my_x my_y b:..... point::point(double x, double y) : my_x(x), my_y(y) { } alternative initialization default constructor another constructor

19 19 Separate Compiling 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); }; Header file, class interface points.h #include #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; } Class implementation file points.cpp

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


Download ppt "1 Classes: A class is a concept of something Vehicle 4 wheels, seats, engine, windows, color…… accelerate, start, stop, turn, lock,............... Truck."

Similar presentations


Ads by Google