操作员指针过载
Operator Overload With Pointers
作为下周考试复习包的一部分,我们被要求理解重载运算符中的指针——我发现这很难理解,老师拒绝给我们举任何例子,并要求他进行独立学习。
显然,我们在决赛中会遇到这样的问题之一,我想确保我正确理解这个练习。
这种做法涉及操作员过载的问题。并且我们被要求实现<lt;和>>。我们要:
使用您一直使用的FlashDrive类,升级该类该运算符<lt;和运算符>>与指针(即,FlashDrive*)。您需要重新重载这些运算符,添加功能:
朋友std::ostream&运算符<lt;(std::ostream&outs,const FlashDrive*驱动器);
朋友std::istream&operator>>(std::istream&ins,FlashDrive*&drive);
提示:非常小心测试空
注意:这些想法将成为下周作业的一部分!
我正在使用的代码是
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;和>>运算符:-(有什么想法吗??
.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);
}
我的意思是"链接":
- 首先执行CCD_ 1。这将返回一个
std::ostream
引用:在这种情况下,这个引用就是std::cout
本身 - 现在,我们可以用
std::cout
"替换"std::cout << llama
,因为1 - 将执行
std::cout << " is the ..."
。然后是CCD_ 7
这就是为什么返回对std::ostream
的引用是重要的。
相关文章:
- 如何定义指针铸件操作员
- 当直接分配时,为什么此功能指针分配起作用,而不是与有条件的操作员一起使用
- 基本操作员具有非指针类型
- 操作员的地址可以启动指针,但填充值是垃圾
- 静态指针指向会员数组的安全操作员[]访问
- 提取操作员在使用来自其他类名的指针时超载
- 创建库以覆盖迭代器的操作员*() - 风险悬挂指针
- 调用指针参数从const函数指向的对象的操作员
- 如何迫使新操作员返回的指针将是32字节对齐
- (C 14)操作员&lt;&lt;超负荷无法正如智能指针向量所预期的那样工作
- C - 操作员 - =在指针上
- 操作员超载和 *该指针的麻烦
- 在操作员过载的情况下处理悬空指针/参考
- Ostream操作员Strangley不接受我的堆栈指针
- 我何时应该将指针从操作员函数中返回对象(而不是对象)
- 操作员=用双指针超载用于分数数学
- 带有超载操作员 的无访问地址的指针返回
- 带指针的奇怪操作员
- C /参考操作员和指针中的插座连接()函数说明
- munmap_chunk 当操作员调用时指针无效,然后调用 destroctor