Home > Articles

Modularity in C++ 17

  • Print
  • + Share This

The creator of C++ provides an overview of modularity as it applies to C++ 17.

Save 35% off the list price* of the related book or multi-format eBook (EPUB + MOBI + PDF) with discount code ARTICLE.
* See informit.com/terms

This chapter is from the book

This chapter is from the book

Don’t interrupt me while I’m interrupting.

– Winston S. Churchill

  • Introduction

  • Separate Compilation

  • Modules

  • Namespaces

  • Error Handling

    Exceptions; Invariants; Error-Handling Alternatives; Contracts; Static Assertions

  • Function Arguments and Return Values

    Argument Passing; Value Return; Structured Binding

  • Advice

3.1 Introduction

A C++ program consists of many separately developed parts, such as functions (§1.2.1), user-defined types (Chapter 2), class hierarchies (§4.5), and templates (Chapter 6). The key to managing this is to clearly define the interactions among those parts. The first and most important step is to distinguish between the interface to a part and its implementation. At the language level, C++ represents interfaces by declarations. A declaration specifies all that’s needed to use a function or a type. For example:

double sqrt(double);     // the square root function takes a double and returns a double

class Vector {
     Vector(int s);
     double& operator[](int i);
     int size();

     double* elem; // elem points to an array of sz doubles
     int sz;

The key point here is that the function bodies, the function definitions, are “elsewhere.” For this example, we might like for the representation of Vector to be “elsewhere” also, but we will deal with that later (abstract types; §4.3). The definition of sqrt() will look like this:

double sqrt(double d)        // definition of sqrt()
     // ... algorithm as found in math textbook ...

For Vector, we need to define all three member functions:

Vector::Vector(int s)                  // definition of the constructor
     :elem{new double[s]}, sz{s}       // initialize members

double& Vector::operator[](int i)      // definition of subscripting
     return elem[i];

int Vector::size()                     // definition of size()
     return sz;

We must define Vector’s functions, but not sqrt() because it is part of the standard library. However, that makes no real difference: a library is simply “some other code we happen to use” written with the same language facilities we use.

There can be many declarations for an entity, such as a function, but only one definition.

  • + Share This
  • 🔖 Save To Your Account