Operator Overloading

Hi,

I am taking a C++ class online for summer and I am having trouble with an assignment.
I would like to get some help
or at least can somebody show me where to start.

Here is the assignment:

Using FlashDrive.cpp, enhance the FlashDrive class so that it supports the operators +, -, < and >. A sample pile of driver code is shown below to assist you in this effort. Operators + and - should create a new FlashDrive from the two arguments by combining their contents. If you wind up with a FlashDrive with a value stored that exceeds its capacity, print out an error message. If you wind up with a negative capacity or storage value, print out an error message. Operators < and > must return bool and should compare the holdings of the two arguments to determine which one is bigger.

My strong advice is to work one operator at a time, as these steps are very error-prone and lead to many, many compile errors.

Here is the FlashDrive.cpp mentioned in the question.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#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;   // in kilobytes
	int my_StorageUsed;       // in kilobytes
	bool my_IsPluggedIn;      // am I attached to a computer?
};

#endif 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#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 );
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include "FlashDrive.h"
using namespace std;

void main( )
{
  int i;
  cout << "Welcome to Howie's Flash USB Drive Store!" 
       << endl;
  cout << "How much memory do you need? ";
  cin >> i;

  FlashDrive f( i, 0, false );
  cout << "Let's format the drive..." << endl;
  f.formatDrive();
  cout << "Let's plug in the drive..." << endl;
  f.plugIn();
  cout << "Let's write some data..." << endl;
  f.writeData( 10 ); 
  cout << "Let's unplug the drive..." << endl; 
  f.pullOut();

  cout << "Enjoy your drive!!" << endl;
}


Here is the sample given by our professor:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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( );

int my_StorageCapacity; 
int my_StorageUsed; 
bool my_IsPluggedIn; 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <iostream>
#include "FlashDrive.h"
using namespace std;

void main( )
{
FlashDrive drive1( 10, 0, false );
FlashDrive drive2( 20, 0, false );

drive1.plugIn( );
drive1.formatDrive( );
drive1.writeData( 5 );
drive1.pullOut( );

drive2.plugIn( );
drive2.formatDrive( );
drive2.writeData( 1 );
drive2.pullOut( );

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

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


On the sample given by the professor, program is not taking any input from the user so I just erased the part where it takes input.

I did add drive1 and drive2 as members of the FlashDrive class.
But I am not sure how to overload the operators to add capacities of the two drive.

I know it is supposed to be something like this

friend FlashDrive operator+ (I do not know which parameters to write here);


FlashDrive operator +(parameters for drive1 and drive2) {
//Totally made up this part because I do know which parameters to get

FlashDrive combined;

combined.getUsed = x.getUsed+ y.getUsed;
return combined;
}


So can somebody explain me where to start.
Which data should I get for overloading, is it my_StorageUsed?
If so how am I supposed to get 2 different my_StorageUsed for drive1 and drive2?

Thanks in advance.

For example you could define operators < and > the following way

1
2
3
4
5
6
7
8
9
10
bool operator <( const FlashDrive &lhs, const FlashDrive &rhs )
{
   return ( lhs.getCapacity() < rhs.getCapacity() );
}


bool operator >( const FlashDrive &lhs, const FlashDrive &rhs )
{
   return ( operator <( rhs, lhs ) );
}
Last edited on
@vlad from moscow
Thank you for your answer.
I changed getCapacity to getUsed since it was asking for used data comparison.

I also added other operator overloading to my code but still no luck I am still getting errors.

Anybody want to direct me to the right way?
What am I doing wrong here?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#ifndef FLASHDRIVE_H
#define FLASHDRIVE_H


class FlashDrive {
	friend FlashDrive operator+ (FlashDrive used1 , FlashDrive used2);
	friend FlashDrive operator= (FlashDrive used1);
	friend bool operator <( const FlashDrive &lhs, const FlashDrive &rhs );
	friend bool operator >( const FlashDrive &lhs, const FlashDrive &rhs );
public:
	
	/*void FlashDrive::operator= (FlashDrive usedtotal){
	my_StorageUsed = usedtotal.getUsed();
	}*/

    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;   // in kilobytes
	int my_StorageUsed;       // in kilobytes
	bool my_IsPluggedIn;      // am I attached to a computer?
}drive1,drive2;

FlashDrive operator +(FlashDrive used1, FlashDrive used2 ) {
	
	FlashDrive combined;

	combined = (used1.getUsed()+ used2.getUsed());
	return combined;
}

bool operator <(FlashDrive &lhs,FlashDrive &rhs ) {
   return ( lhs.getUsed() < rhs.getUsed() );
}


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

FlashDrive operator -(FlashDrive used1, FlashDrive used2 ){
	FlashDrive other;
	other= (used1.getUsed()-used2.getUsed());
	return other;
}


#endif 
You need not to define these functions as friends

bool operator <( const FlashDrive &lhs, const FlashDrive &rhs );
bool operator >( const FlashDrive &lhs, const FlashDrive &rhs );

because you are using public members of the class within bodies of these functions.
I did correct it thank you.

I am getting errors for the"=" and "-".
I still can't find why.

Any suggestions?
The copy assignment operator shall be a class member function. Its declaration can be for example as

FlashDrive & operator =( const FlashDrive & );
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#ifndef FLASHDRIVE_H
#define FLASHDRIVE_H


class FlashDrive {
	friend FlashDrive operator+ (FlashDrive used1 , FlashDrive used2);
	friend FlashDrive operator- (FlashDrive used3, FlashDrive used4 );
	
	
	
public:
	
	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;   // in kilobytes
	int my_StorageUsed;       // in kilobytes
	bool my_IsPluggedIn;      // am I attached to a computer?
}drive1,drive2;

FlashDrive operator+ (FlashDrive used1, FlashDrive used2 ) {
	
	FlashDrive plus;

	plus = (used1.getUsed()+ used2.getUsed());
	return plus;
}

bool operator< (FlashDrive &lhs,FlashDrive &rhs ) {
   return ( lhs.getUsed() < rhs.getUsed() );
}


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

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


#endif 


I tried to change the code as you suggested but
I am getting these errors still.
I think I am having troubles with the body of the operator overloading functions.


Error 2 error C2679: binary '=' : no operator found which takes a right-hand operand of type 'int' (or there is no acceptable conversion) flashdrive.h 61


9 IntelliSense: no operator "=" matches these operands flashdrive.h 46


Error 1 error C2679: binary '=' : no operator found which takes a right-hand operand of type 'int' (or there is no acceptable conversion) flashdrive.h 46

please read error messages yourself. They are very ckear. For example in this function

1
2
3
4
5
6
7
FlashDrive operator+ (FlashDrive used1, FlashDrive used2 ) {
	
	FlashDrive plus;

	plus = (used1.getUsed()+ used2.getUsed());
	return plus;
}


you are trying to assign an integer value to an object of type FlashDrive.
Last edited on
I am trying to figure out how to fix.

So I am trying to add the drive1 used data to drive2 used data which will give me the result(3. variable), which is suppose to be the same type of drive1 and drive2 used data.

So with this logic I should be able to say
plus.myStorageUsed = (used1.getUsed()+ used2.getUsed());

plus.myStorageUsed = (used1.my_StorageUsed+ used2.my_StorageUsed);
or
plus.getUsed() = (used1.getUsed()+ used2.getUsed());

however they all give errors.
First two says myStorageUsed is not a member of FlashDrive and the other one says plus should be a modifiable variable.

And at this point I am out of logical answers.

Am I suppose to convert those values to each other with something else, maybe a function in C++ or my logic is totally wrong?

Last edited on
Topic archived. No new replies allowed.