Question

Help please with a Project in C++ In C++, many of the keyboard symbols that are used between two ...

Help please with a Project in C++

In C++, many of the keyboard symbols that are used between two variables can be given a new meaning. This feature is called operator overloading and it is one of the most popular C++ features. Any class can choose to provide a new meaning to a keyboard symbol. Not every keyboard letter is re-definable in this way, but many of the ones we have encountered so far are like +, -, *, /, >and <for example. It is a class’ choice to do this, so mostly it is viewed as a driver code convenience to support them.

But because so many of us have an assumption that +should do addition but also perform string concatenation when working with textual data. Each operator becomes a friendfunction in C++ that your class can implement. The arguments to most of the operator overloads are defined as const FlashDrive &. This syntax is a new kind of parameter passing called const-reference parameters.

For a class type, like FlashDrive, const &signifies a read-only argument that cannot be changed by the function that receives this object. The compiler enforces this restriction and, for classes, const &simulates a pass-by-value mechanism without the overhead of copying the object, which might take a tremendous amount of time away from our program. Read the book and demo source examples carefully to see how this is done. Trust me, the first time you work with operators in C++, it is a very error-prone process. My best advice to you is to complete one operator at a time.

Flashdrive 2.0

Rules of the flashdrive comparisons:

  1. The addition operator ( +) combines two flashdrives by created a new flashdrive that has the larger capacity of the two operand flashdrives (e.g. if you have a 20GB flash drive and add it to a 10GB flashdrive, the resulting flashdrive has 20GB of space, not 10GB - capacity is the max()operation). Then, the operator should mark that the new flash drive has used the sum of the two operand’s flashdrives (for instance, if one of the operands used 5GB and the other used 12GB, the resulting flashdrive has used 17GB).
  2. The subtraction operator ( -) substracts two flashdrives by created a new flashdrive that has the larger capacity of the two operand flashdrives (e.g. if you have a 20GB flash drive and subtract a 10GB flashdrive, the resulting flashdrive has 20GB of space, not 10GB - capacity is the max()operation). Then, the operator should remove the right operand’s storage from the left operand (for instance, if the left operand has used 20GB and the other used 12GB, the resulting flashdrive has used 8GB).
  3. The less than operator ( <) should compare the if the left operand’s storage used is less than the right operand’s storage used.
  4. The greater than operator ( >) should compare the if the left operand’s storage used is greater than the right operand’s storage used.

Using flashdrive_2_0.cpp, enhance the FlashDriveclass 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 FlashDrivefrom the two arguments by combining their contents. If you wind up with a FlashDrivewith 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 booland 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.

Finally, I would also like you to place FlashDrivein namespace cs52which will affect the resulting driver code as well as the class’ .hand .cppfiles. * Note:your code will not compile until you define the cs52namespace

FlashDrive 2.0

FlashDrive( );
FlashDrive( int capacity, int used, bool pluggedIn );

void plugIn( );

bool isPluggedIn( );
void pullOut( );

void writeData( int amount );
void eraseData( int amount );

void formatDrive( );

void setCapacity( int amount );
void setUsed( int amount );

int getCapacity( );
int getUsed( );

Sample Driver Code

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( );

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

cs52::FlashDrive other = drive1 - drive2;
cout << "the other drive's filled to " <<
other.getUsed( ) << endl;
cout << "the other drive's capacity is " <<
other.getCapacity() << 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;
}

Sample Output

this drive's filled to 6 
this drive's capacity is 20 
the other drive's filled to 4 
the other drive's capacity is 20 
looks like combined is bigger... 
looks like other is bigger... 
looks like drive2 is smaller...
0 0
Add a comment Improve this question Transcribed image text
Answer #1

code

solution //Flash Drive.h # include<algorithm> class Flash Drive private: int capacityvalue; int used; bool plugged_In; public//operator overloads Flash Drive operatort (const Flash Drive& other) Flash Drive operat。r-(c。nst Flash Drive & other) bool oreturn false; void Flash_Drive: :pullOut() f pluggedIn - false; //Write data void Flash_Drive: write_Data(int amount) i used+//operator overload Flash_Drive Flash_Drive: :operatort (cost Flash_Drive& other) I Flash_Drive f; f.set_capacity (std: :max//Main.cpp #include <iostream> #include Flash Drive. h int main () /lobjects Flash Drive drivel (10, 0, false); Flash Driveif (combined > other) I cout <<looks like combined is bigger... << endl; else cout << looks like other is bigger... << en

output

this drives filled to 6 this drives capacityvalue is 20 the other drives filled to 4 the other drives capacityvalue is 20

//copyable code

solution

//Flash_Drive.h

#include<algorithm>

class Flash_Drive {

private:

    int capacityvalue;

    int used;

    bool plugged_In;

public:

    Flash_Drive();

    Flash_Drive(int capacityvalue, int used, bool plugged_In);

    void plug_In();

    bool isplugged_In();

    void pullOut();

    //Write data

    void write_Data(int amount);

    //Erase data

    void erase_Data(int amount);

    //Clear

    void format_Drive();

    //set capacityvalue

    void set_capacity(int amount);

    //Set used part

    void set_Used(int amount);

    //Get capacityvalue

    int get_capacity();

    //Get usage

    int getUsed();

    //Operator overloads

    Flash_Drive operator+(const Flash_Drive& other);

    Flash_Drive operator-(const Flash_Drive& other);

    bool operator<(const Flash_Drive& other);

    bool operator>(const Flash_Drive& other);

};

//Flash_Drive.cpp

#include "Flash_Drive.h"

//Default constructor

Flash_Drive::Flash_Drive() {

    capacityvalue = 0;

    used = 0;

    plugged_In = false;

}

Flash_Drive::Flash_Drive(int capacityvalue, int used, bool plugged_In) {

    this->capacityvalue = capacityvalue;

    this->used=used;

    this->plugged_In=plugged_In;

}

void Flash_Drive::plug_In() {

    plugged_In = true;

}

bool Flash_Drive::isplugged_In() {

    if (plugged_In == true) {

        return true;

    }

    return false;

}

void Flash_Drive::pullOut() {

    plugged_In = false;

}

//Write data

void Flash_Drive::write_Data(int amount) {

        used+= amount;

}

//delete data

void Flash_Drive::erase_Data(int amount) {

        used -= amount;

}

//Clear data

void Flash_Drive::format_Drive() {

        used=0;

}

//Setters

void Flash_Drive::set_capacity(int amount) {

        capacityvalue+= amount;

}

void Flash_Drive::set_Used(int amount) {

        used+=amount;

}

//Getters

int Flash_Drive::get_capacity() {

        return capacityvalue;

}

int Flash_Drive::getUsed() {

        return used;

  

}

//Operator overload

Flash_Drive Flash_Drive::operator+(const Flash_Drive& other) {

    Flash_Drive f;

    f.set_capacity(std::max(this->capacityvalue, other.capacityvalue));

    f.set_Used(this->used + other.used);

    return f;

}

Flash_Drive Flash_Drive::operator-(const Flash_Drive& other) {

    Flash_Drive f;

    f.set_capacity(std::max(this->capacityvalue, other.capacityvalue));

    f.set_Used(this->used - other.used);

    return f;

}

bool Flash_Drive::operator<(const Flash_Drive& other) {

    if (this->used < other.used) {

        return true;

    }

    return false;

}

bool Flash_Drive::operator>(const Flash_Drive& other) {

    if (this->used> other.used) {

        return true;

    }

    return false;

}

//Main.cpp

#include <iostream>

#include "Flash_Drive.h"

using namespace std;

int main()

{

    //Objects

    Flash_Drive drive1(10, 0, false);

    Flash_Drive drive2(20, 0, false);

    drive1.plug_In();

    drive1.format_Drive();

    drive1.write_Data(5);

    drive1.pullOut();

    drive2.plug_In();

    drive2.format_Drive();

    drive2.write_Data(1);

    drive2.pullOut();

    Flash_Drive combined = drive1 + drive2;

    cout << "this drive's filled to " << combined.getUsed(

    ) << endl;

    cout << "this drive's capacityvalue is " <<

        combined.get_capacity() << endl;

   Flash_Drive other = drive1 - drive2;

    cout << "the other drive's filled to " <<

        other.getUsed() << endl;

    cout << "the other drive's capacityvalue is " <<

        other.get_capacity() << 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;

    }

}

Add a comment
Know the answer?
Add Answer to:
Help please with a Project in C++ In C++, many of the keyboard symbols that are used between two ...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Project 1 - Operator Overloading (FlashDrive 2.0) In C++, many of the keyboard symbols that are...

    Project 1 - Operator Overloading (FlashDrive 2.0) In C++, many of the keyboard symbols that are used between two variables can be given a new meaning. This feature is called operator overloading and it is one of the most popular C++ features. Any class can choose to provide a new meaning to a keyboard symbol. Not every keyboard letter is re-definable in this way, but many of the ones we have encountered so far are like +, -, *, /,...

  • I have provided you with a sample class named FlashDrive which has been diagrammed below. Using...

    I have provided you with a sample class named FlashDrive which has been diagrammed below. Using the FlashDrive class provided earlier, upgrade the class so that it supports various operators. Make operator+ combine together the contents of two FlashDrive, as long as the contents does not exceed the size. Make operator- subtract one FlashDrive contents from another, as long as the size or contents don't go negative. Support the >> and << operators to allow instances to be read from...

  • Background: This assignment deals with inheritance. Inheritance is one of the major principles of object-oriented programming....

    Background: This assignment deals with inheritance. Inheritance is one of the major principles of object-oriented programming. In C++, one of the biggest goals is "code reuse". Inheritance accomplishes this. In order to get inheritance working in C++, you must get both the structure of your .h files as well as the implementation of your constructor correct. Constructor implementations must use an initialization list. Please review the book and the online content on these issues so you know how it works...

  • Please zoom in so the pictures become high resolution. I need three files, FlashDrive.cpp, FlashDrive.h and...

    Please zoom in so the pictures become high resolution. I need three files, FlashDrive.cpp, FlashDrive.h and user_main.cpp. The programming language is C++. I will provide the Sample Test Driver Code as well as the codes given so far in text below. Sample Testing Driver Code: 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(2); drive2.pullOut(); cs52::FlashDrive combined = drive1 + drive2; // std::cout << "this drive's filled to " << combined.getUsed( )...

  • Background: The purpose of this assignment is to practice dealing with exception handling and textual data....

    Background: The purpose of this assignment is to practice dealing with exception handling and textual data. Exception handling is a very important part of being an object-oriented programming. Rather returning some kind of int return value every time you tickle an object, C++ programmers expect methods to focus on their task at hand. If something bad happens, C++ programmers expect methods to throw exceptions. When caught, exceptions can be processed. When uncaught, they cause a program to terminate dead in...

  • Project 2 - Flashdrive 1.0 I have provided you with a sample class named FlashDrive which...

    Project 2 - Flashdrive 1.0 I have provided you with a sample class named FlashDrive which has been diagrammed below and described in the online course content. For this unit, I'd like you to identify additional member variables (data) that would be appropriate for the class FlashDrive. I would also like you to identify additional methods (functions) that would be appropriate for the class FlashDrive. Submit a .h file for FlashDrive with the new possible methods and members you have...

  • NEED ASAP PLEASE HELP Task: --------------------------------------------------------------------------------------...

    NEED ASAP PLEASE HELP Task: --------------------------------------------------------------------------------------------------------------------------------- Tasks to complete: ---------------------------------------------------------------------------------------------------------------------------------------- given code: ----------------------------------------------------------------------------------------------------------------------------------------------- main.cpp #include <iostream> #include <iomanip> #include "fractionType.h" using namespace std; int main() { fractionType num1(5, 6); fractionType num2; fractionType num3; cout << fixed; cout << showpoint; cout << setprecision(2); cout << "Num1 = " << num1 << endl; cout << "Num2 = " << num2 << endl; cout << "Enter the fraction in the form a / b: "; cin >> num2; cout << endl; cout <<...

  • please help with the operator overloading lab (intArray) in c++ will provide what it is being...

    please help with the operator overloading lab (intArray) in c++ will provide what it is being required and the code that was given from the book. the code that was provided is below ------------------------------------------------------------------------------------------------------------------------- // iadrv.h #ifndef _IADRV_H #define _IADRV_H #include "intarray.h" int main(); void test1(); void test2(); void test3(); void test4(); void test5(); void test6(); void test7(); void test8(); void test9(); void test10(); void test11(); void test12(); void test13(); void test14(); void test15(); void test16(); void test17(); void test18();...

  • c++ Error after oveloading, please help? LAB #8- CLASSES REVISITED &&    Lab #8.5 …      Jayasinghe...

    c++ Error after oveloading, please help? LAB #8- CLASSES REVISITED &&    Lab #8.5 …      Jayasinghe De Silva Design and Implement a Program that will allow all aspects of the Class BookType to work properly as defined below: class bookType { public:    void setBookTitle(string s);            //sets the bookTitle to s    void setBookISBN(string ISBN);            //sets the private member bookISBN to the parameter    void setBookPrice(double cost);            //sets the private member bookPrice to cost    void setCopiesInStock(int noOfCopies);            //sets the private member copiesInStock to...

  • If you have already answered this question, please do not repost your old solutions, you will be thumbs downed. I'm...

    If you have already answered this question, please do not repost your old solutions, you will be thumbs downed. I'm looking for NEW solutions only! Frac.h: // a Fraction object holds one Fraction number, one fraction #ifndef FRAC_H #define FRAC_H #include <iostream> using namespace std; //Creaing a Fraction class class Fraction { public: Fraction(int = 0, int = 1); // Function Declarations which performs operations on Fraction class Fraction add(const Fraction &); Fraction subtract(const Fraction& a); Fraction multiply(const Fraction& a);...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT