Abstract Data Types - Separating Interface from Implementation

From: Anon Email (anonemail1_at_fastmail.fm)
Date: 12/22/03


Date: 21 Dec 2003 17:29:05 -0800

Hi people,

I'm learning about header files in C++. The following is code from
Bartosz Milewski:

// Code

const int maxStack = 16;

class IStack
{
public:
    IStack () :_top (0) {}
    void Push (int i);
    int Pop ();
private:
    int _arr [maxStack];
    int _top;
};

--------------
// Header file

#include "stack.h"
#include <cassert>
#include <iostream>
using std::cout;
using std::endl;

//compile with NDEBUG=1 to get rid of assertions

void IStack::Push (int i)
{
    assert (_top < maxStack);
    _arr [_top] = i;
    ++_top;
}

int IStack::Pop ()
{
    assert (_top > 0);
    --_top;
    return _arr [_top];
}

--------------

I know this is very basic code. I also know that it's a non-standard
header filename/ file. I'm merely using this for demonstration
purposes. Let's pretend that in C++ it's possible to completely
separate the interface from the implementation. Would the header file
then look something like this?:

//compile with NDEBUG=1 to get rid of assertions

const int maxStack = 16;

class IStack
{
public:
        IStack () :_top (0) {}
            void Push (int i);
            int Pop ();

        void Push (int i)
        {
                assert (_top < maxStack);
                    _arr [_top] = i;
                ++_top;
        }

        int Pop ()
        {
                 assert (_top > 0);
                   --_top;
                  return _arr [_top];
        }

            
private:
            int _arr [maxStack];
            int _top;

};

In other words, everything that defines the class would go in the
class header, and the only code written in the implementation file
would be a call or calls to the constructor or member functions?

Cheers,

Deets



Relevant Pages

  • Re: integer and floating-point bit pattern
    ... > to include the proper header file for any ... Prior to the current standard you did not need a prototype or a ... declaration to call a function that returned int and accepted a fixed ...
    (comp.lang.c)
  • Re: [PATCH -mm 0/3] sysv ipc: increase msgmnb with the number of cpus
    ... recompute_msgmnbto a header file, so we have a window in which the ... we don't want such tunables to be recomputed anymore upon memory ... switch { ... extern int ipcns_notify; ...
    (Linux-Kernel)
  • Re: Why linking?
    ... What the header file contains is mostly "declarations" ... int * inflate; ... then this will tell the compiler that this function returns ...
    (comp.unix.programmer)
  • Re: Why linking?
    ... I guess zlib.h isn't a header file you wrote but that comes with ... some packages. ... int * inflate; ... then this will tell the compiler that this function returns ...
    (comp.unix.programmer)
  • Re: DeviceResolutionAware.h and wide string constants
    ... The header file DeviceResolutionAware.h include the header file altcecrt.h ... Device win32 project, default settings) ... int WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPWSTR lpCmdLine, int ...
    (microsoft.public.pocketpc.developer)