Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
C++ Simulates Int Memory
#include "IntCell.h"
IntCell::IntCell( int initialValue ) : storedValue{ initialValue }
{
}
int IntCell::read( ) const
{
return storedValue;
}
void IntCell::write( int x )
{
storedValue = x;
}
#ifndef IntCell_H
#define IntCell_H
/**
* A class for simulating an integer memory cell.
*/
class IntCell
{
public:
explicit IntCell( int initialValue = 0 );
int read( ) const;
void write( int x );
private:
int storedValue;
};
#endif
#include <iostream>
#include "IntCell.h"
using namespace std;
int main( )
{
IntCell m; // Or, IntCell m( 0 ); but not IntCell m( );
m.write( 5 );
cout << "Cell contents: " << m.read( ) << endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment