200 likes | 337 Views
attributes. A class is a concept of something. Classes:. Vehicle 4 wheels, seats, engine, windows, color…… accelerate, start, stop, turn, lock,. methods. Truck …………. Sedan …………. Focus …………. Matrix …………. SUV …………. class:. Method & Attribute.
E N D
attributes A class is a concept of something Classes: Vehicle 4 wheels, seats, engine, windows, color…… accelerate, start, stop, turn, lock, ............... methods Truck ………… Sedan ………… Focus ………… Matrix ………… SUV …………
class: Method & Attribute Attribute (or, Field):the properties the class Like a noun or adjective. E.g., wheels, seats, engine, windows, color Method (or, member function):behaviors, operation, functionalities. Like a verb: E.g., accelerate, start, stop, turn, lock
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.
Old fashion:struct 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) .... }
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
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
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
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
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
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
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
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
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 p’s private variable Using p’s private member function Using own private member function
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
Constructor Compiler will create a default constructor; but can’t take care of too much detail 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
Explicitly define a Constructor 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
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
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
#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 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 Class implementation file points.cpp
class point { public: point(); ...... private: double my_x; ....... }; Separate Compiling points.h Under Unix $g++ -c points.cpp generates points.o Compiling only, generate object code ..... #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