Last active
July 31, 2018 18:37
-
-
Save paullewallencom/1101471f239af098b3d61f85c85dd3b8 to your computer and use it in GitHub Desktop.
C++ Simulates Int Memory
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "IntCell.h" | |
IntCell::IntCell( int initialValue ) : storedValue{ initialValue } | |
{ | |
} | |
int IntCell::read( ) const | |
{ | |
return storedValue; | |
} | |
void IntCell::write( int x ) | |
{ | |
storedValue = x; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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