DOC PREVIEW
UMBC CMSC 341 - C++ and OOP

This preview shows page 1-2-3 out of 9 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 9 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 9 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 9 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 9 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

CMSC 341CMSC 341C++ and OOPIntcell.H#ifndef _IntCell_H_#define _IntCell_H_// A class for simulating an integer memory cell. class IntCell{public:explicit IntCell( int initialValue = 0 );Intcell(const Intcell & ic)~Intcell();const Intcell & operator =(const Intcell & rhs);int read( ) const;void write( int x );private:int _storedValue;};#endifIntCell.C (part 1)#include "IntCell.h"// Construct the IntCell with initialValueIntCell::IntCell( int initialValue ) :storedValue(initialValue){// no code}//copy constructorIntcell::Intcell(const Intcell & ic) {write (ic.read());}// destructorIntcell::~Intcell() { // no code}IntCell.C (part 2)//assignment operatorIntcell::operator=(const IntCell & rhs) {if (this != &rhs)write(rhs.read());return *this;}// Return the stored value (accessor)nt IntCell::read( ) const{return _storedValue;}// Store x (mutator)void IntCell::write( int x ){_storedValue = x;}TestIntCell.C #include <iostream.h>#include "IntCell.h"int main( ) {IntCell m; // Or, IntCell m( 0 ); but not IntCell m( );Intcell n;n = m;m.write( 5 );cout << "Cell m contents: " << m.read( ) << endl;cout << "Cell n contents: " << n.read( ) << endl;return 0;}MemCell.H#ifndef _MEMCELL_H#define _MEMCELL_H// A class for simulating a memory cell.template <class Object>class MemCell {public:explicit MemCell(const Object &initialValue =Object( ) );MemCell(const MemCell & mc); const MemCell & operator=(const MemCell & rhs);~MemCell(); const Object & read( ) const;void write( const Object & x );private:Object _storedValue;};//Because separate compilation doesn't always work#include "MemCell.C" #endifMemCell.C(part 1)#include "MemCell.h“// Construct the MemCell with initialValuetemplate <class Object>MemCell<Object>::MemCell( const Object & initialValue ): _storedValue( initialValue ){// no code}//copy constructortemplate <class Object>MemCell<Object>::MemCell(const MemCell<Object> & mc){write(mc.read( ));}//assignment operatortemplate <class Object>const MemCell<Object> & MemCell<Object>::operator=(const MemCell<Object> & rhs){write(rhs.read( ));}MemCell.C (part 2)// destructortemplate <class Object>MemCell<Object>::~MemCell( ) {// no code}// Return the stored value.template <class Object>const Object & MemCell<Object>::read( ) const{return _storedValue;}// Store x.template <class Object>void MemCell<Object>::write( const Object & x ) {storedValue = x;}TestMemCell.C#include <iostream.h>#include "MemCell.h"#include "string.h"#include <stdlib.h>int main( ){MemCell<int> m1;MemCell<string> m2( "hello" );m1.write( 37 );string str = m2.read();str += " world";m2.write(str);cout << m1.read( ) << endl << m2.read( ) << endl;return


View Full Document

UMBC CMSC 341 - C++ and OOP

Download C++ and OOP
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view C++ and OOP and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view C++ and OOP 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?