50 lines
1.1 KiB
C++
50 lines
1.1 KiB
C++
|
Fig01_05.cpp - Simplest IntCell class, with a test program
|
||
|
|
||
|
#include <iostream.h>
|
||
|
|
||
|
/**
|
||
|
* A class for simulating an integer memory cell.
|
||
|
*/
|
||
|
class IntCell
|
||
|
{
|
||
|
public:
|
||
|
/**
|
||
|
* Construct the IntCell.
|
||
|
* Initial value is 0.
|
||
|
*/
|
||
|
IntCell( )
|
||
|
{ storedValue = 0; }
|
||
|
|
||
|
/**
|
||
|
* Construct the IntCell.
|
||
|
* Initial value is initialValue.
|
||
|
*/
|
||
|
IntCell( int initialValue )
|
||
|
{ storedValue = initialValue; }
|
||
|
|
||
|
/**
|
||
|
* Return the stored value.
|
||
|
*/
|
||
|
int read( )
|
||
|
{ return storedValue; }
|
||
|
|
||
|
/**
|
||
|
* Change the stored value to x.
|
||
|
*/
|
||
|
void write( int x )
|
||
|
{ storedValue = x; }
|
||
|
|
||
|
private:
|
||
|
int storedValue;
|
||
|
};
|
||
|
|
||
|
int main( )
|
||
|
{
|
||
|
IntCell m;
|
||
|
|
||
|
m.write( 5 );
|
||
|
cout << "Cell contents: " << m.read( ) << endl;
|
||
|
|
||
|
return 0;
|
||
|
}
|