C++ Class Construct  «Prev  Next»
Lesson 5 External member functions
Objective Rewrite Person class using external member function

External Member Functions in C++

In C++, the term "external member function" is not a standard nomenclature in the language's specification. However, the term "external" in conjunction with functions can be related to the "extern" keyword which specifies linkage information about a function or a variable.
Let's dive deep into this concept to elucidate its purpose and application.
  1. "extern" Keyword and Linkage: In C++, the `extern` keyword denotes that a particular function or variable is defined elsewhere, possibly in another source file or a library. This keyword primarily deals with linkage – that is, connecting references of an entity to its definition.
  2. Member Functions and `extern`:
    When we speak of member functions of a class, they are typically defined either:
    1. Inline: Defined within the class definition itself. These functions are implicitly inline, which may suggest to the compiler to inline-expand the function's code at the point of invocation.
    2. Outside the Class: Defined outside the class using the `ClassName::FunctionName` syntax. This approach separates the declaration from the definition, allowing for more modular code.
    3. However, the `extern` keyword is not typically associated with member functions. Instead, it's more common with global functions or variables to denote external linkage.
  3. What's an "External Member Function"?
    If someone uses the term "external member function," they might be referring to a member function that's defined outside the class definition. Such a separation is done for various reasons:
    1. Readability: Separating lengthy function definitions from the class declaration can make the class declaration easier to read and understand.
    2. Compilation: The separation allows for independent compilation of class definitions and member function implementations, which can reduce compile times in large projects.
    3. Encapsulation: While the function's prototype is visible in the class declaration, its implementation details remain hidden when defined in a source file.

C++ Member Function Example

// MyClass.h
class MyClass {
    void myFunction(); // Declaration of the member function

// MyClass.cpp
#include "MyClass.h"
void MyClass::myFunction() { 
    // Implementation of the member function

In the above example, `myFunction` can be viewed as an "external member function" because it's declared inside `MyClass` but defined externally in another source file.
While the term "external member function" isn't standard jargon in C++, it may be used informally to describe member functions defined outside their class declaration. Proper understanding of this distinction aids in writing organized, maintainable, and efficient C++ programs.

Use an external member function to print out the member data of the Person class.
To define a member function outside the class, the scope resolution operator is used. To illustrate this, let us revisit the ch_stack class from the previous module. We can change the declaration of push, inside the declaration of class ch_stack, to just a function prototype and define push elsewhere. When we define it, we write the name out fully using the scope resolution operator. In this case, the function is not implicitly inline.

const int max_len = 40;
class ch_stack {
   void  reset() { top = EMPTY; }
   void  push(char c);
   char  pop() {
      assert(top!= EMPTY);
      return s[top--];
   char  top_of() {
      assert(top!= EMPTY);
      return s[top];
   bool  empty()
     { return (top == EMPTY); }
   bool  full()
     { return (top == FULL); }
   char  s[max_len]; 
   int   top;
   enum { EMPTY = -1, FULL = max_len - 1 };

void ch_stack::push(char c) //not inline { 
assert(top != FULL); 
top++; s[top] = c; 

External Member Function - Exercise

Click the Exercise link below to rewrite the person class so it uses an external member function to print out its member data.
External Member Function - Exercise