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:
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...
code
output
//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;
}
}
Help please with a Project in C++ In C++, many of the keyboard symbols that are used between two ...
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 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. 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 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. 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 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: --------------------------------------------------------------------------------------------------------------------------------- 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 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 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 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);...