We gained an introduction to the immutable object from the preceding discussion. As you learned in the previous chapter, we can take advantage of the first-class function and pure function to create an immutable programming approach. Let's borrow the code from Chapter 2, Manipulating Functions in Functional Programming, that is first_class_1.cpp
. We will have the addition()
, subtraction()
, multiplication()
, and division()
methods in our following first_class_pure_immutable.cpp
code. We will then invoke the pure function on the class and assign the result to the variable. The code is written as follows:
/* first_class_pure_immutable.cpp */ #include <iostream> using namespace std; // MyValue class stores the value class MyValue { public: const int value; MyValue(int v) : value(v) { } }; // MyFunction class stores the methods class MyFunction...