Class example



//============================================================================
// Name        : classExample1.cpp
// Author      : chd
// Version     :
// Copyright   : Your copyright notice
// Description : Simple class example of calculating
//               the integral of a function, C++ Ansi-style
//============================================================================

#include <iostream>
#include <math.h>
using namespace std;

typedef double (*pfnct) (double);

class integration {

private:

	double lower;
	double upper;
	pfnct integrand;

public:

	integration(double a, double b, pfnct function){

		lower=a;
		upper=b;
		integrand=function;

	}


	double lowerbd() const {
		return lower;
	}

	double upperbd() const {
		return upper;
	}

	void alterbds(double a, double b);

	double trapezoidRule(int gridPoints);
	double simpsonRule(int gridPoints);

};


double integration::trapezoidRule(int gridPoints){

	double res=0;
	double h=(upper-lower)/gridPoints;
	double sum=integrand(lower)*0.5;
	for (int i = 1; i < gridPoints; ++i) {

		sum+=integrand(lower+i*h);
	}

	sum+=integrand(upper)*0.5;
	return sum*h;


}

double power(double x){
	return x*x;

}

int main() {


	pfnct tmpf=power;
	//construct an object of the class
	integration integr(0,1,tmpf);
	//calculating the integral
	double result=integr.trapezoidRule(1000);
	cout<<"Result is:"<<result<<endl;

}