Creating Functional Objects

Lesson Introduction

Welcome to the lesson on creating functional objects in C++! In this session, we'll explore why functional objects are vital in modern C++ programming. You'll learn how they encapsulate behavior within objects for more modular, reusable code. Our aim is to understand what functional objects are and how to create and use them through practical examples.

What Are Functional Objects?

Functional objects are objects you can call like functions. Essentially, they encapsulate a function's logic within an object. This is particularly useful for passing functions as arguments, storing them, or configuring them with state information.

Unlike standard functions, which cannot hold state, functional objects can maintain state because they are object-oriented. This allows them to keep information across function calls, enabling more complex behavior than a simple function pointer or lambda expression can provide.

Designing Functional Objects

Let's start by designing a basic class for our functional object. We'll set up a constructor for state initialization and define the callable operator, operator(), to make the object callable like a function. Note that in C++ it is common to call such objects "functors". We will call them this way through this course as well.

However, in general functional programming theory, "functor" is usually used to describe a more general object, which we will talk about in the next course of this path.

Consider creating a functor to filter people based on age:

#include <string>

class older_than {
public:
    // Constructor to initialize the age limit state
    older_than(int limit) : m_limit(limit) {}

    // Callable operator method that compares the person's age to the limit
    bool operator()(const person_t& person) const {
        return person.age() > m_limit;
    }

private:
    int m_limit; // State to store the age limit
};

Here:

  • The constructor older_than(int limit) initializes the age limit state to a specified value.
  • The operator() method makes the object callable like a function and contains the logic to check if a person's age is greater than the limit.

Code Example Breakdown: Part 1

Now, let’s break down the functional objects and their supporting classes in a full example. First, define a class to represent a person:

#include <string>

class person_t {
public:
    // Constructor to initialize person's name and age
    person_t(std::string name, int age) : m_name(name), m_age(age) {}

    // Getter method for the age attribute
    int age() const { return m_age; }

    // Getter method for the name attribute
    std::string name() const { return m_name; }

private:
    std::string m_name; // Private member to store the person's name
    int m_age;          // Private member to store the person's age
};

This class has:

  • A constructor person_t(std::string name, int age) to initialize the person’s name and age attributes.
  • Getter methods, age() and name(), to access these attributes, ensuring encapsulation.
Sign up

Join the 1M+ learners on CodeSignal

Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal