title: Operator Overload With Pointers
body: <p>As a part of our exam review package for next week, we are asked to understand pointers in overloaded operators-- something I am finding it very difficult to get and the instructor is refusing to give us any example, and asking his for independent study.</p>
  
  <p>Apparently we will be having one of these problems on the final and I want to be sure I properly understand this practice exercise.</p>
  
  <p>This practice concerns the issue of operator overloading. And we are asked to implement &lt;&lt; and >>. 
  And we are to :</p>
  
  <blockquote>
    <p>Using the FlashDrive class you have been working with, upgrade the class so
    that operator &lt;&lt; and operator >> work well with pointers (that is,
    FlashDrive *). Youll need to re-overload these operators, adding the
    function:</p>
    
    <p>friend std::ostream&amp; operator &lt;&lt;( std::ostream&amp; outs, const FlashDrive * drive ); </p>
    
    <p>friend std::istream&amp; operator >>( std::istream&amp; ins, FlashDrive * &amp; drive );</p>
    
    <p>HINT: Be very careful to test for NULL </p>
    
    <p>NOTE: These ideas will be a part on next weeks assignment!</p>
  </blockquote>
  
  <p>The code I am working with is</p>
  
  <p><strong>FlashDrive.h</strong></p>
  
  <pre><code>#ifndef FLASHDRIVE_H
  #define FLASHDRIVE_H
  
  class FlashDrive {
  public:
  FlashDrive( );
  FlashDrive( int capacity, int used, bool pluggedIn );
  void plugIn( );
  void pullOut( );
  void writeData( int amount );
  void eraseData( int amount );
  void formatDrive( );
  
  int getCapacity( );
  void setCapacity( int amount );
  
  int getUsed( );
  void setUsed( int amount );
  
  bool isPluggedIn( );
  private:
  int my_StorageCapacity; 
  int my_StorageUsed; 
  bool my_IsPluggedIn;
  
  }
  
  
  
  
  
  #endif 
  </code></pre>
  
  <p><strong>FlashDriver.cpp --- Driver Class</strong></p>
  
  <pre><code>#include &lt;iostream&gt;
  #include "FlashDrive.h"
  using namespace cs52;
  
  void main( )
  {
  
  cs52::FlashDrive empty;
  cs52::FlashDrive drive1( 10, 0, false );
  cs52::FlashDrive drive2( 20, 0, false );
  
  drive1.plugIn( );
  drive1.formatDrive( );
  drive1.writeData( 5 );
  drive1.pullOut( );
  
  drive2.plugIn( );
  drive2.formatDrive( );
  drive2.writeData( 1 );
  drive2.pullOut( );
  
  // read in a FlashDrive... 
  // the class designer for FlashDrive (that's you!)
  // gets to decide which fields matter and should be read in
  cs52::FlashDrive sample;
  cin &gt;&gt; sample;
  
  // print out a FlashDrive...
  // the class designer for FlashDrive (that's you!)
  // gets to decide which fields matter and should be printed
  cout &lt;&lt; sample &lt;&lt; endl;
  
  cs52::FlashDrive combined = drive1 + drive2;
  cout &lt;&lt; "this drive's filled to " &lt;&lt; combined.getUsed( ) &lt;&lt; endl;
  
  cs52::FlashDrive other = combined – drive1;
  cout &lt;&lt; "the other cup's filled to " &lt;&lt; other.getUsed( ) &lt;&lt; endl;
  
  if (combined &gt; other) {
    cout &lt;&lt; "looks like combined is bigger..." &lt;&lt; endl;
  }
  else {
    cout &lt;&lt; "looks like other is bigger..." &lt;&lt; endl;
  }
  
  if (drive2 &gt; other) {
    cout &lt;&lt; "looks like drive2 is bigger..." &lt;&lt; endl;
  }
  else {
    cout &lt;&lt; "looks like other is bigger..." &lt;&lt; endl;
  }
  
  if (drive2 &lt; drive1) {
    cout &lt;&lt; "looks like drive2 is smaller..." &lt;&lt; endl;
  }
  else {
    cout &lt;&lt; "looks like drive1 is smaller..." &lt;&lt; endl;
  }
  
  // let's throw some exceptions...
  
  try {
    empty = empty – combined;
    cout &lt;&lt; "something not right here..." &lt;&lt; endl;
  } catch( std::logic_error ) {
  // an exception should get thrown... 
  // so the lines of code here should
  // be run, not the cout statement...
  }
  
  try {
    drive2.writeData( 10000 );
    cout &lt;&lt; "something not right here..." &lt;&lt; endl;
  } catch( std::logic_error ) {
  // an exception should get thrown... 
  // so the lines of code here should
  // be run, not the cout statement...
  }
  
  try {
    cs52::FlashDrive f( -1, -1, false );
    cout &lt;&lt; "something not right here..." &lt;&lt; endl;
  } catch( std::logic_error ) {
  // an exception should get thrown... 
  // so the lines of code here should
  // be run, not the cout statement...
  }
  
  // work with the new stuff added for Unit 16!!!
  
  cs52::FlashDrive * drive3 = NULL;
  // careful...
  cout &lt;&lt; drive3 &lt;&lt; endl;
  
  drive3 = &amp;drive2;
  cout &lt;&lt; drive3 &lt;&lt; endl;
  
  drive3 = new FlashDrive();
  cin  &gt;&gt; drive3;
  cout &lt;&lt; drive3 &lt;&lt; endl;
  
  delete( drive3 );
  
  }
  </code></pre>
  
  <p>I would like to see what a working implementation class would look like so I can reverse engineer that and use that as a study source. I am finding getting some of these pointers to work very challenging</p>
  
  <p>The implementation class I have already coded is below</p>
  
  <p><strong>FlashDrive.cpp</strong></p>
  
  <pre><code>#include "FlashDrive.h"
  
  
  FlashDrive::FlashDrive( ) {
    my_StorageCapacity = 0;
    my_StorageUsed = 0;
    my_IsPluggedIn = false;
  }
  
  
  FlashDrive::FlashDrive( int capacity, int used, bool pluggedIn ) { 
    my_StorageCapacity = capacity;
    my_StorageUsed = used;
    my_IsPluggedIn = pluggedIn;
  }
  
  void FlashDrive::plugIn( ) {
    my_IsPluggedIn = true;
  }
  
  void FlashDrive::pullOut( ) {
    my_IsPluggedIn = false;
  }
  
  void FlashDrive::writeData( int amount ) {
    my_StorageUsed += amount;
  }
  
  void FlashDrive::eraseData( int amount ) {
    my_StorageUsed -= amount;
  }
  
  void FlashDrive::formatDrive( ) {
    my_StorageUsed = 0;
  }
  
  
  int  FlashDrive::getCapacity( ) {
    return( my_StorageCapacity );
  }
  
  void FlashDrive::setCapacity( int amount ) {
    my_StorageCapacity = amount;
  }
  
  int  FlashDrive::getUsed( ) {
    return( my_StorageUsed );
  }
  
  void FlashDrive::setUsed( int amount ) {
    my_StorageUsed = amount;
  }
  
  bool FlashDrive::isPluggedIn( ) {
    return( my_IsPluggedIn );
  }
  </code></pre>
  
  <p><strong>EDIT::::::</strong></p>
  
  <p>I have updated the .h and .cpp
  but I am still unable to properly add the &lt;&lt; and >> operators :-( 
  Any ideas??</p>
  
  <p><strong>.h</strong></p>
  
  <pre><code> #ifndef FLASHDRIVE_H
  #define FLASHDRIVE_H
  namespace cs52
  {
  class FlashDrive {
  
  friend FlashDrive operator+ (FlashDrive used1 , FlashDrive used2);
  friend FlashDrive operator- (FlashDrive used3, FlashDrive used4 );
  
  public:
      friend std::ostream&amp; operator &lt;&lt;( std::ostream&amp; outs, const FlashDrive * drive );
      friend std::istream&amp; operator &gt;&gt;( std::istream&amp; ins, FlashDrive * &amp; drive );
      FlashDrive&amp; FlashDrive::operator=(int);
      FlashDrive::FlashDrive(int);
      FlashDrive(const std::string &amp;name): name_(name){
      }
      FlashDrive&amp; operator = (const FlashDrive&amp; usedtotal){
          my_StorageUsed= usedtotal.my_StorageUsed;
          return *this;
  }
  FlashDrive( );
  FlashDrive( int capacity, int used, bool pluggedIn );
  void plugIn( );
  void pullOut( );
  void writeData( int amount );
  void eraseData( int amount );
  void formatDrive( );
  
  int getCapacity( );
  void setCapacity( int amount );
  
  int getUsed( );
  void setUsed( int amount );
  
  bool isPluggedIn( );
  private:
  int my_StorageCapacity; 
  std::string   name_;
  int my_StorageUsed; 
  bool my_IsPluggedIn;
  
      }extern drive1,drive2;
  
  
  
  inline FlashDrive operator+ (FlashDrive used1, FlashDrive used2 ) {
  
      FlashDrive plus;
      plus.my_StorageUsed = (used1.getUsed()+ used2.getUsed());
      return plus;
  }
  
  inline bool operator&lt; (FlashDrive &amp;lhs,FlashDrive &amp;rhs ) {
     return ( lhs.getUsed() &lt; rhs.getUsed() );
  }
  
  inline bool operator&gt; (FlashDrive &amp;lhs,FlashDrive &amp;rhs ) {
     return ( operator &lt;( rhs, lhs ) );
  }
  
  inline FlashDrive operator - (FlashDrive used3, FlashDrive used4 ){
      FlashDrive minus;
      minus.my_StorageUsed = (used3.getUsed()- used4.getUsed());
      return minus;
  
      }
  
  
  #endif 
  </code></pre>
  
  <p><strong>.cpp</strong></p>
  
  <pre><code>    #include &lt;cstdlib&gt;
  #include &lt;iostream&gt;
  #include "FlashDrive.h"
  using namespace cs52;
  using namespace std;
  
  std::ostream&amp; operator &lt;&lt;(std::ostream&amp; outs, const FlashDrive * drive )
  {
     outs &lt;&lt; drive-&gt;name_;
     return outs;
  }
  
  std::istream&amp; operator &gt;&gt;( std::istream&amp; ins, FlashDrive * &amp; drive )
  {
      ins &gt;&gt; drive-&gt;name_;
      return ins;
  }
  
  FlashDrive::FlashDrive( ) {
   my_StorageCapacity = 0;
   my_StorageUsed = 0;
   my_IsPluggedIn = false;
  }
  
  FlashDrive::FlashDrive( int capacity, int used, bool pluggedIn ) { 
    my_StorageCapacity = capacity;
    my_StorageUsed = used;
    my_IsPluggedIn = pluggedIn;
  }
  
  void FlashDrive::plugIn( ) {
    my_IsPluggedIn = true;
  }
  
  void FlashDrive::pullOut( ) {
    my_IsPluggedIn = false;
  
  }
  void FlashDrive::writeData( int amount ) {
   my_StorageUsed += amount;
  }
  void FlashDrive::eraseData( int amount ) {
    my_StorageUsed -= amount;
  }
  void FlashDrive::formatDrive( ) {
    my_StorageUsed = 0;
  }
  int  FlashDrive::getCapacity( ) {
    return( my_StorageCapacity );
  }
  void FlashDrive::setCapacity( int amount ) {
    my_StorageCapacity = amount;
  }
  int  FlashDrive::getUsed( ) {
    return( my_StorageUsed );
  }
  void FlashDrive::setUsed( int amount ) {
    my_StorageUsed = amount;
  }
  bool FlashDrive::isPluggedIn( ) {
    return( my_IsPluggedIn );
  }
  </code></pre>