Recent Changes - Search:

Coding C++

Computing

Object: A simple Function Object

Trail Index | Objects | Objects

Objects integrate data and related procedures. Really, it's that simple.

Step 1 - Simple and clean

/**
 * One function class with parameters and method
 * Now this is shorter and has no duplication!
 */

#include <iostream>
#include <cmath>

/**
 * "Object" contains both data and operation(s).
 * Still only a definition, not declaration.
 *
 * Note that it's customary to call the definition
 * of an object a "class" or "struct", and a
 * instance and "object".
 */

struct Function {
        double k1;
        double k2;
        /**
         * now that f() is part of struct,
         * we do not to give it explicitly the parameters!
         * k1,k2 refer to the struct members.
         *
         * Note that f(..) const {..} clarifies
         * that this method does not modify the structure.
         * Use it as much as you can!
         */

        double f(const double x) const {
                return sqrt(k1)*x+k2*x*x;
        }
};

/**
 * Much better than Function_4, now we call instance.f()
 * We have improved on the structure, removed redundancy,
 * and practically eliminated all possibilities of
 * mis-spelling of variables.
 * Only thing, f1.f(x) is not as nice as f1(x) was...
 */

int main() {
        {
                Function f1;
                f1.k1=2.;
                f1.k2=3.;
                const double y1=f1.f(3.);
                std::cout<< "f1("<<3.<<")="<<y1<<"\n";
        }
        /*
         * now using separate scopes, it's
         * impossible to confuse f1 and f2
         */

        {
                Function f2;
                f2.k1=4.;
                f2.k2=5.;
                const double y2=f2.f(3.);
                std::cout<< "f2("<<3.<<")="<<y2<<"\n";
        }
}

Concentrate for a moment, and make sure you realize one important point: the methods operate on the values of the current object instance.

Step 2 - Functor, more complex, but simpler to use

/**
 * Objects: functor, constructor
 * this is even shorter
 */

#include <iostream>
#include <cmath>

/**
 * object with constructor and operator
 */

struct Function {
        double k1;
        double k2;
        /**
         * Constructor (ctor): initializes the class,
         * in particular the data members.
         * With the constructor you don't need separate initializations
         */

        Function(double newk1,double newk2):
                k1(newk1), k2(newk2) {};
        /**
         * overloading of operators - a bit of magic...
         * Objects with operator() are often called "functors"
         * http://en.wikipedia.org/wiki/Function_object
         */

        double operator() (const double x) const {
                return sqrt(k1)*x+k2*x*x;
        }
};

/**
 * this is finally really better than the first version,
 * even for the function calling syntax!
 *
 * But note that I can easily give a wrong value
 * for the parameters!
 */

int main() {
        Function f1(2.,3.);
        const double y1=f1(3.);
        std::cout<< "f1("<<3.<<")="<<y1<<"\n";
       
        Function f2(4.,5.);
        const double y2=f2(3.);
        std::cout<< "f2("<<3.<<")="<<y2<<"\n";
       
        f1.k1=-1.;
        std::cout<< "now f1.k1="<<f1.k1<<" f1.k2="<<f1.k2<<std::endl;
        const double y3=f1(3.);
        std::cout<< "f1("<<3.<<")="<<y3<<"\n";
       
        /*
         * the life of an object instance can be very short...
         * and it doesn't even need to get a name!
         */

        std::cout<< "f?("<<3.<<")="<<Function(3.,4.)(3.)<<"\n";
       
}

< Functions | Trail Index | Containers >

Edit - History - Print - Recent Changes - Search
Page last modified on April 10, 2007, at 11:48 pm