Week Five Lecture PDF
Week Five Lecture PDF
Inheritance
The Preprocessor
S CIENTIFIC C OMPUTING
I NHERITANCE
Dr. Johnson
School of Mathematics
Semester 1 2012
Topics:
Objects, data and functions together;
Constructors and destructors;
Access qualifiers;
Operator overloading.
Aims - week 4:
Understand concept of data and functions as members of
an object;
Create and use simple objects (Points, Intergrand function);
Overload operators to enable easy code writing.
Topics:
Inheritance - runtime polymorphism
Templates - efficient coding
Error handling and analysis
Aims - week 5:
Be able to use inheritance to overload function operators
Use templates to create efficient codes and minimise code
reuse
Error checking within your code
I NHERITANCE
I NHERITANCE
OVERLOADING F UNCTIONS
OVERLOADING F UNCTIONS
OVERLOADING F UNCTIONS
OVERLOADING F UNCTIONS
The output is
distance to origin is 5.38516
V IRTUAL F UNCTION
V IRTUAL F UNCTION
J UST A POINTER
J UST A POINTER
A N E XAMPLE
Lets go back to the Integrand, and make it virtual:
class Intergrand{
public:
virtual double operator()(double x)=0;
};
Nothing else in your codes needs to be changed.
A N E XAMPLE
Lets go back to the Integrand, and make it virtual:
class Intergrand{
public:
virtual double operator()(double x)=0;
};
Nothing else in your codes needs to be changed.
Need a derived class to implement the function call...
This could be:
class OnePlusXX: public Intergrand{
public:
double operator()(double x) {
return 1 + x*x;}
};
Dr. Johnson MATH49111
Review The Derived Class
Inheritance Virtual Functions
The Preprocessor Pure Virtual Functions
A N E XAMPLE
A N E XAMPLE
I NLINE F UNCTIONS
I NLINE F UNCTIONS
I NLINE F UNCTIONS
F UNCTION T EMPLATES
Using virtual function causes a small overhead while
finding the function at run time
An alternative is to use templates, which can be written like
// integrate function
template<class T>
double trapezium(double a,double b,int n,T f)
{
// stepsize
double h = (b-a)/n;
// store running sum
double sum =(f(a) + f(b))/2.;
for(int i=1;i<n;i++)sum += f(a + i*h);
return sum*h;
}
Dr. Johnson MATH49111
Review Inline for Efficiency
Inheritance Generic Coding
The Preprocessor Error Checking
F UNCTION T EMPLATES
The C++ compiler generate new code for each function call
with a different class
This allows the compiler to rewrite the functions inline
This is most useful when the algorithms in the function are
short and generic
They must not require different behaviour for different
classes
F UNCTION T EMPLATES
The C++ compiler generate new code for each function call
with a different class
This allows the compiler to rewrite the functions inline
This is most useful when the algorithms in the function are
short and generic
They must not require different behaviour for different
classes
You don’t want it to generate too much code!!!
F LAGS
Error checking is something that we may wish to turn on
and off
Checking for errors takes time, making programs slow
One way to turn it on and off is to use preprocessor flags...
// check if denominator is zero
#ifdef DEBUG
if(g(x)==0.) // g=0 then throw exception
{ cout « ” denominator = 0 ”;throw; }
#endif
return f(x)/g(x);
The two lines of code here will only be compiled if DEBUG
has been defined in the code...
#define DEBUG
Dr. Johnson MATH49111
Review Inline for Efficiency
Inheritance Generic Coding
The Preprocessor Error Checking
Topics:
Inheritance - runtime polymorphism
Templates - efficient coding
Error handling and analysis
Aims - week 5:
Be able to use inheritance to overload function operators
Use templates to create efficient codes and minimise code
reuse
Error checking within your code