You are on page 1of 20

# Review

Classes

S CIENTIFIC C OMPUTING
C LASSES

Dr. Johnson

School of Mathematics

Semester 1 2012

Dr. Johnson MATH49111

Review
Classes

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.