Abstract Data Types - Separating Interface from Implementation
From: Anon Email (anonemail1_at_fastmail.fm)
Date: 12/22/03
- Next message: Victor Bazarov: "Re: Popup through screensaver?"
- Previous message: cylin: "Re: How to get two bytes form a buffer?"
- Next in thread: Victor Bazarov: "Re: Abstract Data Types - Separating Interface from Implementation"
- Reply: Victor Bazarov: "Re: Abstract Data Types - Separating Interface from Implementation"
- Reply: John Carson: "Re: Abstract Data Types - Separating Interface from Implementation"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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
- Next message: Victor Bazarov: "Re: Popup through screensaver?"
- Previous message: cylin: "Re: How to get two bytes form a buffer?"
- Next in thread: Victor Bazarov: "Re: Abstract Data Types - Separating Interface from Implementation"
- Reply: Victor Bazarov: "Re: Abstract Data Types - Separating Interface from Implementation"
- Reply: John Carson: "Re: Abstract Data Types - Separating Interface from Implementation"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|