操作符超载指针

时间:2013-08-05 07:26:15

标签: c++ pointers operator-overloading

作为我们下周的考试复习包的一部分,我们被要求理解超载操作员的指示 - 我发现很难得到的东西,导师拒绝给我们任何一个例子,并要求他独立研究。

显然我们将在决赛中遇到其中一个问题,我想确保我正确理解这个练习。

这种做法涉及运算符重载的问题。我们被要求实施<<和>>。 我们要:

  

使用您一直在使用的FlashDrive类,升级该类   运算符<<和运算符>>适用于指针(即,   FlashDrive *)。你需要重新加载这些运算符,添加   功能:

     

朋友std :: ostream& operator<<(std :: ostream& outs,const FlashDrive * drive);

     

朋友std :: istream&运算符>>(std :: istream& ins,FlashDrive *& drive);

     

提示:要非常小心地测试NULL

     

注意:这些想法将成为下周工作的一部分!

我正在使用的代码是

FlashDrive.h

#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 

FlashDriver.cpp ---驱动程序类

#include <iostream>
#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 >> sample;

// print out a FlashDrive...
// the class designer for FlashDrive (that's you!)
// gets to decide which fields matter and should be printed
cout << sample << endl;

cs52::FlashDrive combined = drive1 + drive2;
cout << "this drive's filled to " << combined.getUsed( ) << endl;

cs52::FlashDrive other = combined – drive1;
cout << "the other cup's filled to " << other.getUsed( ) << endl;

if (combined > other) {
  cout << "looks like combined is bigger..." << endl;
}
else {
  cout << "looks like other is bigger..." << endl;
}

if (drive2 > other) {
  cout << "looks like drive2 is bigger..." << endl;
}
else {
  cout << "looks like other is bigger..." << endl;
}

if (drive2 < drive1) {
  cout << "looks like drive2 is smaller..." << endl;
}
else {
  cout << "looks like drive1 is smaller..." << endl;
}

// let's throw some exceptions...

try {
  empty = empty – combined;
  cout << "something not right here..." << 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 << "something not right here..." << 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 << "something not right here..." << 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 << drive3 << endl;

drive3 = &drive2;
cout << drive3 << endl;

drive3 = new FlashDrive();
cin  >> drive3;
cout << drive3 << endl;

delete( drive3 );

}

我想看看一个有效的实现类是什么样的,所以我可以对它进行逆向工程并将其用作学习源。我发现其中一些指针非常具有挑战性

我已编码的实现类位于

之下

FlashDrive.cpp

#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 );
}

修改::::::

我更新了.h和.cpp 但我仍然无法正确添加&lt;&lt;和&gt;&gt;运营商:-( 任何想法??

·H

 #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& operator <<( std::ostream& outs, const FlashDrive * drive );
    friend std::istream& operator >>( std::istream& ins, FlashDrive * & drive );
    FlashDrive& FlashDrive::operator=(int);
    FlashDrive::FlashDrive(int);
    FlashDrive(const std::string &name): name_(name){
    }
    FlashDrive& operator = (const FlashDrive& 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< (FlashDrive &lhs,FlashDrive &rhs ) {
   return ( lhs.getUsed() < rhs.getUsed() );
}

inline bool operator> (FlashDrive &lhs,FlashDrive &rhs ) {
   return ( operator <( rhs, lhs ) );
}

inline FlashDrive operator - (FlashDrive used3, FlashDrive used4 ){
    FlashDrive minus;
    minus.my_StorageUsed = (used3.getUsed()- used4.getUsed());
    return minus;

    }


#endif 

的.cpp

    #include <cstdlib>
#include <iostream>
#include "FlashDrive.h"
using namespace cs52;
using namespace std;

std::ostream& operator <<(std::ostream& outs, const FlashDrive * drive )
{
   outs << drive->name_;
   return outs;
}

std::istream& operator >>( std::istream& ins, FlashDrive * & drive )
{
    ins >> drive->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 );
}

1 个答案:

答案 0 :(得分:4)

  

我想看看工作实现类的外观   像

你走了:

#include <iostream>

class Llama
{
private:
  std::string   name_;

public:
  Llama(const std::string &name): name_(name)
  {}

  friend std::ostream &operator<<(std::ostream &o, const Llama *);
};

std::ostream &operator<<(std::ostream &o, const Llama *llama)
{
  o << llama->name_;
  return o; /* this is important, because it allows chaining */
}


int     main(int, char **)
{
  Llama *llama = new Llama("Meh");

  std::cout << llama << " is the name of my llama" << std::endl;
  return (0);
}

我的意思是“链接”:

  1. std::cout << llama将首先执行。这将返回std::ostream引用:在这种情况下,此引用本身为std::cout
  2. 现在,由于1,我们可以std::cout << llama“替换”std::cout
  3. std::cout << " is the ..."将被执行。然后是std::cout << std::endl
  4. 这就是为什么返回std::ostream的引用很重要。