diff --git a/ch21/pure_virtual_function.c b/ch21/pure_virtual_function.c new file mode 100644 index 0000000..a7cc37b --- /dev/null +++ b/ch21/pure_virtual_function.c @@ -0,0 +1,39 @@ +#include +#include + +using namespace std; + +class Function { + public: + // This is pure virtual + virtual double eval(double x) = 0; + + // This calls the pure one + double derivative(double x, double e) { + return (eval(x+e) - eval(x-e))/(2*e); + } +}; + +class Oscillating : public Function { + public: + Oscillating(double aa, double bb, double cc) + :a(aa), b(bb), c(cc) {} + double eval(double x) { + return a*sin(b*x+c); + } + private: + double a, b, c; +}; + +int main(int argc, char **argv) { + Oscillating f(1, 1, 0); + cout << f.derivative(0, 0.1) << endl; + cout << f.derivative(0, 0.01) << endl; + cout << f.derivative(0, 0.001) << endl; + Function f1; + // let's mess the complier! + std::cout << f1.eval(3.0) << std::endl; + return 0; +} + + diff --git a/test.cc b/test.cc index 4f9d1bd..1face39 100644 --- a/test.cc +++ b/test.cc @@ -7,5 +7,6 @@ int main(int argc, char **argv) { cout << "the patch test of git" << endl; std::cout << "test of commit --amend" << std::endl; + std::cout << "show diff" << std::endl; return 0; }