7.4. Interfaces and implementation

A class defines a type. When you design a type, some parts are hidden (private), while others are visible (public). The public parts of your type represent an interface. The interface to your class determine how other functions will interact with it.

Consider a simple date class. This class does not allow direct access to y, m, and d. These class members can only be set using the 3 arg constructor provided and can only be retrieved using the functions provided.

class date {
  int y, m, d;

  public:
    date ()
      : y{1776}, m{7}, d{4}
    { }
    // Declare other constructors & functions
    date (int y, int m, int d);
    std::tuple<int,int,int> as_tuple ();
    int year ();
    int month ();
    int day ();
};

Public member functions define the class interface. Everything hidden are implementation details. No one needs to know (or care) how things are actually implemented.

Keeping interface specifications separate from the implementation is considered a best practice:

Declare interfaces in your header files (date.h):

#pragma once

#include <tuple>

class date {
    int y {1776};
    int m {7};
    int d {4};
  public:
    date () = default;
    date (int y, int m, int d);
    std::tuple<int,int,int> as_tuple ();
    int year ();
    int month ();
    int day ();
};

Implement interfaces in your cpp files (date.cpp):

#include "date.h"
#include <tuple>

date::date (int year, int mon, int day)
  : y{year}, m{mon}, d{day}
{ }

std::tuple<int,int,int> date::as_tuple () {
  return std::make_tuple(y, m,d);
}

int date::year ()  { return y; }
int date::month () { return m; }
int date::day ()   { return d; }

7.4.1. File extensions

All source files are simply text files, however, by convention, different kinds of files have different extensions:

  • Header files should end in .h, .hpp, or .hxx.

    The extension you use is mostly a matter of preference, but some organizations define explicit guidelines.

    Some code editors may assume .h headers are for C code and that .hpp headers are C++ code and may apply different syntax highlighting rules.

    It won’t matter to your compiler.

  • C++ Source files should end in .cpp and

  • C Source files should end with .c.

C and C++ source files are handled differently by your compiler. That is when you compile your code, you need to compile the C files differently from the C++ files. Having a simple convention to distinguish them is important.


More to Explore

You have attempted of activities on this page