2

作为我们下周考试复习包的一部分,我们被要求理解重载运算符中的指针——我发现这很难得到,而且导师拒绝给我们任何例子,并要求他独立学习。

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

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

使用您一直在使用的 FlashDrive 类,升级该类,以便 operator << 和 operator >> 与指针(即 FlashDrive *)一起工作。您需要重新重载这些运算符,添加函数:

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

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

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

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

我正在使用的代码是

闪存盘.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 );

}

我想看看一个工作实现类会是什么样子,这样我就可以对其进行逆向工程并将其用作研究源。我发现让其中一些指针发挥作用非常具有挑战性

我已经编码的实现类如下

闪存盘.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 但我仍然无法正确添加 << 和 >> 运算符 :-( 有什么想法吗??

。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 );
}
4

1 回答 1

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. 现在,我们可以“替换”std::cout << llamastd::cout,因为 1。
  3. std::cout << " is the ..."将被执行。然后std::cout << std::endl

这就是为什么返回对 的引用std::ostream很重要。

于 2013-08-05T08:31:31.727 回答