[英]Operator Overload With Pointers
作為下周考試復習計划的一部分,要求我們了解重載操作符中的指針-我發現這很難理解,導師拒絕提供任何示例,並要求他進行獨立學習。
顯然,我們將在決賽中遇到這些問題之一,我想確保我正確理解了這個練習練習。
這種做法涉及操作員超載的問題。 並且要求我們實現<<和>>。 我們將:
使用您一直在使用的FlashDrive類,升級該類,以使運算符<<和operator >>與指針(即FlashDrive *)良好配合。 您需要重新加載這些運算符,並添加以下功能:
朋友std :: ostream&運算符<<(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,但仍然無法正確添加<<和>>運算符:-(任何想法?
。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 );
}
我想看看工作的實現類是什么樣子
干得好:
#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);
}
我所說的“鏈接”是什么意思:
std::cout << llama
將首先執行。 這將返回一個std::ostream
引用:在這種情況下,該引用是std::cout
本身。 std::cout << llama
替換為std::cout
,因為1。 std::cout << " is the ..."
將被執行。 然后std::cout << std::endl
。 這就是為什么返回對std::ostream
的引用很重要的原因。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.