0 views

Uploaded by Abraham Sauvingnon

save

- 1
- Unit GeoUtils Delphi Unit
- 3. Xamarin Android Resources
- ch1_cpp (1).pptx
- Practical Foundations of Debugging Chapter6 2
- Java Prac File Updated
- OBJECT ORIENTED PARAGIGM
- Java-to-C++
- Oracle 2
- Cpphtp5 14 IM
- comp322_07
- Chapter 7
- List of Practicals
- 6
- ComPort Library Ver
- programming elements-unit -1.ppt
- C Sharp Notes
- 1 100 Questions CS
- Total Java Notes
- ADXL3xx
- 对于hypermesh二次开发有用的材料
- Lecture 05
- NS2-Persian Manual
- Com Net Interop
- Medrano Esparza Victor Alfonso Laboratorio 2
- Download
- Custom STL Allocators
- CSF213-L7L8
- Javaand.NETCodeSamplesforSAPLogonTicketVerification
- Symbian OS Coding Conventions v1 0 En
- absolute_stability.pdf
- intro_pdes.pdf
- cfd_notes.pdf
- euler_method.pdf
- higher_order_methods.pdf
- von-neumann-analysis.pdf
- bvp_finite_difference.pdf
- bvp_multiple_shooting.pdf
- boundary_value_probs.pdf
- parabolic_equations.pdf
- parabolic_second_order.pdf
- bvp_multiple_shooting.pdf
- elliptic_iteration_handout.pdf
- stability_handout.pdf
- parabolic_stability_handout.pdf
- absolute_stability.pdf
- cfd_slides.pdf
- von-neumann-analysis.pdf
- boundary_value_probs.pdf
- cfd_slides.pdf
- parabolic_equations.pdf
- stability_handout.pdf
- cfd_notes.pdf
- elliptic_pdes_handout.pdf
- convergence.pdf
- parabolic_second_order.pdf
- boundary_value_probs.pdf
- bvp_finite_difference.pdf
- parabolic_stability_handout.pdf
- differencing_for_PDEs.pdf

You are on page 1of 20

Classes

Operator Overloading

S CIENTIFIC C OMPUTING

C LASSES

Dr. Johnson

School of Mathematics

Semester 1 2012

Dr. Johnson MATH49111

Review

Classes

Operator Overloading

L AST W EEK ...

Topics:

Initial value problems for ODEs

Euler’s Method for ODEs

Stability

Aims - week 3:

Be able to solve an initial value ODE problem

Understand where truncation errors come from

Be aware of the concept of stability in a numerical method

Convert mathematical algorithms into code

Dr. Johnson MATH49111

Dr. data and functions together. Aims . Overload operators to enable easy code writing. Topics: Objects.week 4: Understand concept of data and functions as members of an object. Operator overloading. Access qualifiers... Intergrand function). Constructors and desctructors. Johnson MATH49111 . Create and use simple objects (Points. Review Classes Operator Overloading T HIS W EEK .

along with functions that act on that data. An object is collection of data. Dr. The data and functions may have restricted access. access specifier: data and functions }object list. . . Review Constructing a Class Classes Using Classes Operator Overloading D ECLARING A C LASS The C++ class is used to define an object. Johnson MATH49111 . We declare classes in the following way: class class_name { private data and functions access specifier: data and functions .

y. Johnson MATH49111 . // public access data double distance_to_origin(void) {return sqrt(x*x + y*y). Review Constructing a Class Classes Using Classes Operator Overloading E XAMPLE : P OINT class Point{ // class name public: int x.} }. // finish a class with a semi-colon Dr.

cout « " Distance to origin ". and access data or functions with a dot: a.b. Dr.x = 1.c. cout « a.y = 1. Review Constructing a Class Classes Using Classes Operator Overloading E XAMPLE : P OINT We can declare a new Point in the main code with the command Point a.distance_to_origin() « endl.a. Johnson MATH49111 .

Johnson MATH49111 . Dr. protected .full access outside the class. There are three types of access-specifiers: private . everything is declared as public.access inside derived classes. public . Review Constructing a Class Classes Using Classes Operator Overloading U SING CLASSES In the previous example there is no restrictions on access to data.no access outside the class.

public . protected .full access outside the class.access inside derived classes. Johnson MATH49111 . everything is declared as public. We can think of this as read and write access to data Dr.no access outside the class. There are three types of access-specifiers: private . Review Constructing a Class Classes Using Classes Operator Overloading U SING CLASSES In the previous example there is no restrictions on access to data.

} // write to x void set_y(int y_){y=y_.} double distance_to_origin(void) const {return sqrt(x*x + y*y). Review Constructing a Class Classes Using Classes Operator Overloading P ROTECTING YOUR DATA There are many situations where you will not want your data to be changed accidentally. Protecting data can help stop bugs in your code.y.} }.} int get_y(void) const {return y. Johnson MATH49111 . // private access data public: void set_x(int x_){x=x_. // finish a class with a semi-colon Dr. class Point{ // class name int x.} // write to y // const function members read only int get_x(void) const {return x.

C++ allows for defaults commands to be carried out. or run functions to act on the data.. Review Constructing a Class Classes Using Classes Operator Overloading C ONSTRUCTING YOUR DATA When a new peice of data is created.. Dr.. Johnson MATH49111 . or pass in parameters as arguments.. This allows us to initialise the data default values.

C ONSTRUCTOR S YNTAX : class_name(arguments):initialisation list { function calls } Dr. C++ allows for defaults commands to be carried out. This allows us to initialise the data default values. The function run on deletion is a deconstructor.. Review Constructing a Class Classes Using Classes Operator Overloading C ONSTRUCTING YOUR DATA When a new peice of data is created. or run functions to act on the data... or pass in parameters as arguments. Johnson MATH49111 .. The function run on initialisation is a constructor.

Johnson MATH49111 .} int get_x(void) const {return x.} void set_y(int y_){y=y_. Review Constructing a Class Classes Using Classes Operator Overloading C ONSTRUCTING YOUR DATA class Point{ // class name int x.y(y_){} ∼Point(){} double distance_to_origin(void) const {return sqrt(x*x + y*y).y. // finish a class with a semi-colon Dr.} Point():x(0). // private access data public: void set_x(int x_){x=x_.int y_):x(x_).} int get_y(void) const {return y.y(0){} // default constructor Point(int x_.} }.

Dr. Review Constructing a Class Classes Using Classes Operator Overloading D ECONSTRUCTING YOUR DATA Deconstructors are used to prevent memory leaks. Using std libraries to store data should mean that you don’t need to use deconstructors. Johnson MATH49111 . This shouldn’t be a problem unless you use pointers for dynamic storage.

*ptr_a. // we have a Point and a pointer to Point There is special syntax -> to access data via the pointer ptr_a=&a. Johnson MATH49111 . Review Constructing a Class Classes Using Classes Operator Overloading P OINTING TO AN O BJECT We can create a pointer to an object in the same way as standard data types.5). // access data from a It is best to always pass an object to a function as a reference: a_function(const Point& a.const Point& b) { // some function on a and b } Dr. // ptr_a is the location of a ptr_a->get_x(). Point a(2.

Review Classes Operator Overloading OVERLOADING A FUNCTION We use overloading to enable a function with the same name to act differently in different situations.} 2 Take in two Points and return a Point Point add(const Point& a.get_x()). One example is a simple add function: 1 Take in two ints and return an int int add(int a. temp.set_y(a.const Point& b){ Point temp.set_x(a.int b){return (a + b).get_y()). Johnson MATH49111 . temp.get_y() + b. return (temp). // Note we must return a Point } Dr.get_x() + b.

Review Classes Operator Overloading OVERLOADING AN OPERATOR For some of your classes. we may want to be able to write: Point a(2. Johnson MATH49111 . For instance. c = a + b.1).c.b(3.4). multiplication etc may mean something. Dr. addition.

return (temp).4).set_y(a. temp. multiplication etc may mean something. C++ allows us to overload operators.get_x()).get_y() + b.c.b(3. So for the above to work we must include the function: Point operator+(const Point& a.get_x() + b. } Dr.set_x(a. addition.const Point& b){ Point temp.1). c = a + b. Review Classes Operator Overloading OVERLOADING AN OPERATOR For some of your classes. we may want to be able to write: Point a(2. For instance. Johnson MATH49111 .get_y()). temp.

u = 1. Can be used like this double u.).v=1. Dr. Johnson MATH49111 . Intergrand f.} }.. + f(v) + f(10. Review Classes Operator Overloading OTHER USEFUL OPERATORS Another useful operator is the () operator This can be used to make function calls. for example: class Intergrand{ public: double operator()(double x) const { return 1 + x*x.

sum = sum + f(x). double sum=f(a)/2.i++){ x=a+i*h. return sum*h.const Integrand& f){ double h=(b-a)/n. int n. Review Classes Operator Overloading G ENERIC ROUTINES We saw that there were problems if the arguments to a function need to be changed Good codes will provide generic interfaces.double b.} sum = sum + f(b)/2.i<n.. the trapezium rule may be written: double trapezium(double a. Johnson MATH49111 . } Dr. for(int i=1. For example..

Create and use simple objects (Points. Johnson MATH49111 . Access qualifiers. Operator overloading. Intergrand function). Overload operators to enable easy code writing. Aims .week 4: Understand concept of data and functions as members of an object. data and functions together. Constructors and desctructors. Review Classes Operator Overloading S UMMARY Topics: Objects. Dr.

- 1Uploaded byVikram Kumar
- Unit GeoUtils Delphi UnitUploaded byT_Seis
- 3. Xamarin Android ResourcesUploaded byEric Song
- ch1_cpp (1).pptxUploaded byAnnie Liu
- Practical Foundations of Debugging Chapter6 2Uploaded byArpna Shrivastava
- Java Prac File UpdatedUploaded byShaurya Kapoor
- OBJECT ORIENTED PARAGIGMUploaded byapi-3753047
- Java-to-C++Uploaded byBala Ramalingam
- Oracle 2Uploaded byThulasi Ram
- Cpphtp5 14 IMUploaded byClaudio De Conti
- comp322_07Uploaded byPranshu Jaiswal
- Chapter 7Uploaded byNagendra Kumar
- List of PracticalsUploaded byHarshit Mandhyani
- 6Uploaded byManikandan Manikandan
- ComPort Library VerUploaded byYugi Abdul Lathif
- programming elements-unit -1.pptUploaded bysriramkumar_excel
- C Sharp NotesUploaded byShailendra Sharma
- 1 100 Questions CSUploaded bytangiau2110
- Total Java NotesUploaded byKarthik Smile
- ADXL3xxUploaded byresolution8878
- 对于hypermesh二次开发有用的材料Uploaded byCp Zhao
- Lecture 05Uploaded byrasromeo
- NS2-Persian ManualUploaded byMohammad Shojafar
- Com Net InteropUploaded byAyush Chatterjee
- Medrano Esparza Victor Alfonso Laboratorio 2Uploaded byVictor Medrano
- DownloadUploaded byKamarul Zaman Abd Rahman
- Custom STL AllocatorsUploaded byGobara Dhan
- CSF213-L7L8Uploaded byNilayan Ahmed
- Javaand.NETCodeSamplesforSAPLogonTicketVerificationUploaded byPreethi Gopalan
- Symbian OS Coding Conventions v1 0 EnUploaded bylousyengineer

- absolute_stability.pdfUploaded byAbraham Sauvingnon
- intro_pdes.pdfUploaded byAbraham Sauvingnon
- cfd_notes.pdfUploaded byAbraham Sauvingnon
- euler_method.pdfUploaded byAbraham Sauvingnon
- higher_order_methods.pdfUploaded byAbraham Sauvingnon
- von-neumann-analysis.pdfUploaded byAbraham Sauvingnon
- bvp_finite_difference.pdfUploaded byAbraham Sauvingnon
- bvp_multiple_shooting.pdfUploaded byAbraham Sauvingnon
- boundary_value_probs.pdfUploaded byAbraham Sauvingnon
- parabolic_equations.pdfUploaded byAbraham Sauvingnon
- parabolic_second_order.pdfUploaded byAbraham Sauvingnon
- bvp_multiple_shooting.pdfUploaded byAbraham Sauvingnon
- elliptic_iteration_handout.pdfUploaded byAbraham Sauvingnon
- stability_handout.pdfUploaded byAbraham Sauvingnon
- parabolic_stability_handout.pdfUploaded byAbraham Sauvingnon
- absolute_stability.pdfUploaded byAbraham Sauvingnon
- cfd_slides.pdfUploaded byAbraham Sauvingnon
- von-neumann-analysis.pdfUploaded byAbraham Sauvingnon
- boundary_value_probs.pdfUploaded byAbraham Sauvingnon
- cfd_slides.pdfUploaded byAbraham Sauvingnon
- parabolic_equations.pdfUploaded byAbraham Sauvingnon
- stability_handout.pdfUploaded byAbraham Sauvingnon
- cfd_notes.pdfUploaded byAbraham Sauvingnon
- elliptic_pdes_handout.pdfUploaded byAbraham Sauvingnon
- convergence.pdfUploaded byAbraham Sauvingnon
- parabolic_second_order.pdfUploaded byAbraham Sauvingnon
- boundary_value_probs.pdfUploaded byAbraham Sauvingnon
- bvp_finite_difference.pdfUploaded byAbraham Sauvingnon
- parabolic_stability_handout.pdfUploaded byAbraham Sauvingnon
- differencing_for_PDEs.pdfUploaded byAbraham Sauvingnon